MATLAB Programming/Psychtoolbox/GetMouse AllowMultipleHack

Back to MATLAB Programming/Psychtoolbox/Work arounds

A simple hack I made to get around the limitation on the number of mice. It gets around the problem by ignoring that there are multiple mice.

This is useful if you have a trackpad on a laptop and need to use an external mouse.

function [x,y,buttons] = GetMouse_AllowMultipleHack(windowPtrOrScreenNumber) % [x,y,buttons] = GetMouse([windowPtrOrScreenNumber]) % % Returns the current (x,y) position of the cursor and the up/down state % of the mouse buttons. "buttons" is a 1xN matrix where N is the number of % mouse buttons. Each element of the matrix represents one mouse button. % The element is true (1) if the corresponding mouse button is pressed and % false (0) otherwise. % % % Test if any mouse button is pressed. % if any(buttons) %  fprintf('Someone''s pressing a button.\n'); % end % % % Test if the first mouse button is pressed. % if buttons(1) %  fprintf('Someone''s pressing the first button!\n'); % end % % % Test if the second mouse button is pressed. % if length(buttons)>=2 & buttons(2) %  fprintf('Someone''s pressing the second button!\n'); % end % % length(buttons) tells you how many buttons there are on your mouse. % % The cursor position (x,y) is "local", i.e. relative to the origin of % the window or screen, if supplied. Otherwise it's "global", i.e. relative % to the origin of the main screen (the one with the menu bar). % % NOTE: If you use GetMouse to wait for clicks, don't forget to wait % for the user to release the mouse button, ending the current click, before % you begin waiting for the next mouse press. % % fprintf('Please click the mouse now.\n'); % [x,y,buttons] = GetMouse; % while any(buttons) % if already down, wait for release % 	[x,y,buttons] = GetMouse; % end % while ~any(buttons) % wait for press % 	[x,y,buttons] = GetMouse; % end % while any(buttons) % wait for release % 	[x,y,buttons] = GetMouse; % end % fprintf('You clicked! Thanks.\n'); % % NOTE: GetMouse no longer supports this obsolete usage: % xy = GetMouse([windowPtrOrScreenNumber]) % where xy is a 1x2 vector containing the x, y coordinates. % % OS X: ___________________________________________________________________ % % Even if your mouse has more than three buttons, GetMouse will return as % many values as your mouse has buttons, % % GetMouse can not cope if your computer has more than one mouse connected. % It exits with an error. % _________________________________________________________________________ % % See also: GetClicks, SetMouse %

% 4/27/96 dhb  Wrote this help file. % 5/12/96 dgp  Removed confusing comment about columns. %              Added query about coordinates. % 5/16/96 dhb  Modified MEX file to conform to above usage, answered %              query about coordinates. % 5/29/96 dhb  Flushing mouse button events added by dgp. % 8/23/96 dhb  Added support for windowInfo argument. % 2/24/97 dgp	Updated. % 2/24/97 dgp	Updated comments about flushing mouse button events. % 3/10/97 dgp	windowPtrOrScreenNumber % 3/23/97 dgp	deleted obsolete comment about flushing mouse button events. % 5/9/00  dgp  Added note about waiting for release before waiting for next click. % 8/5/01  awi  Added examples and modified to document new size of returned button matrix %				on windows. % 8/6/01  awi  Added See also line for GetClicks and note about prior Windows version. % 4/13/02 dgp  Cosmetic. % 5/16/02 awi  Changed Win GetMouse to return variable number of button values and updated %              help accordingly. % 5/20/02 dgp  Cosmetic. % 5/22/02 dgp  Note that obsolete usage is no longer supported. % 6/10/01 awi  Added SetMouse to see also. % 7/12/04 awi  ****** OS X-specific fork from the OS 9 version ******* %              Added note that this is not supported in OS X.  When the %              new OS X mouse functions are in place this will have to be updated. %              Check to see if the OS 9 GetMouse source would work in %               Carbon on OS X so that we could still support this. % 11/18/04 awi Added support for OS X % 04/25/05 robkohr  A simple hack I made to get around the limitation on %               the number of mice. It gets around the problem by %              ignoring that there are %              multiple mice.

% Exit with an error if we don't find the a mex file on Windows or OS 9. AssertMex('MAC2', 'PCWIN');

% On OS X we execute this script, otherwise either MATLAB found the mex file % file and exuted this, or else this file was exucuted and exited with % error on the AssertMex command above.

%get the number of mouse buttons from PsychHID mousedices=GetMouseIndices; numMice = length(mousedices);

%%%%%% %changed to avoid multiple mouse problem %%%%%% mousedices = mousedices(1); % % if numMice > 1 %    error('GetMouse detected more than one mouse connected to your computer and got confused'); % elseif numMice == 0 %    error('GetMouse could not find any mice connected to your computer'); % end

if numMice == 0 error('GetMouse could not find any mice connected to your computer'); end

%%%%%%% %%End Change %%%%%%%

allHidDevices=PsychHID('Devices'); numMouseButtons=allHidDevices(mousedices).buttons;

%read the mouse position and buttons [globalX, globalY, rawButtons]=Screen('GetMouseHelper', numMouseButtons); buttons=logical(rawButtons);

%renormalize to screen coordinates from display space if(nargin==1) screenRect=Screen('GlobalRect',windowPtrOrScreenNumber); x=globalX-screenRect(RectLeft); y=globalY-screenRect(RectTop); else x=globalX; y=globalY; end