I would like to somehow disable the GetMouse function so that the program stops tracking the mouse position and clicks once a certain condition has been met

8 Ansichten (letzte 30 Tage)
I have the following code in which if the user clicks 1 correct box out of 20 boxes, something happens. However, if they press the 21st box, they can start typing a response. The problem is that if they click on the 21st box and start typing, but then start clicking on any of the other 20 boxes, the program can freeze. I am hoping that there may be a few potential solutions: 1) To disable the mouse after pressing the 21st box, 2) somehow stop monitoring mouse position (i.e., turn off GetMouse), or 3) move the typing bit of code (i.e., when p = 21) outside of one of the while loops. What do you think? Here is the abbreviated code:
while block < (blockMax+1)
c = 1;
resp = 1;
mamaLoop = 0;
while ab < 21
selected=0;
while selected==0
%Record mouse events to move pics
[z,w,buttons]=GetMouse(windowPtr);
in_bounds = (z > [Object.LX{:}] & z < [Object.RX{:}]) & (w > [Object.LY{:}] & w < [Object.RY{:}]);
if any(in_bounds) && buttons(1) == 1
p= find(in_bounds, 1);
if p < 21
if strcmp(list(p).cue,list2(21,ab).cue) s
elected = 1;
else
end
elseif p == 21
selected = 1;
FlushEvents('keyDown');% Flush GetChar queue to remove stale characters
done = 0;
while done == 0 % Loop until <return> or <enter> is hit
[char,t]=GetChar; %%% HERE'S THE INPUT ACTION!!!! THIS ONE LINE IS WHERE A KEY PRESSED BY THE SUBJECT IS INTERPRETED AS A CHARACTER. This is a standard Psychtoolbox (PTB) function.
switch(abs(char))
case {13, 3, 10} %if they hit enter or return, store their response
if isempty(subresponse)
done = 1;
selected = 1;
break
else
ab = ab + 1;
mamaLoop = 1;
done = 1;
selected = 1;
end
case {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}
if length(subresponse)== 12
else
subresponse=[subresponse char];
for k = 1:size(bound,1)
if k == p
else
FilledRect(windowPtr, gray, bound(k,:));
end
end
for k = 1:size(bound,1)
FramedRect(windowPtr,black,bound(k,:));
end
for k = 1:size(npPos)
WriteLine(windowPtr,list(k).cueStem,black,50,npPos(k,1),npPos(k,2));
end
WriteCentered(windowPtr, [list2(21,ab).cue], r(1)+200, r(2)+50, black);
WriteLeft(windowPtr, emDash, r(1)+300, r(2)+47 , black);
WriteLeft(windowPtr, subresponse, r(1)+350,r(2)+60, black);
Screen('Flip', windowPtr);
end
end
end
else
end
end
end
end
Screen('Flip', windowPtr);

Antworten (1)

Abhas
Abhas am 25 Mär. 2024
Hi Geoffrey,
To address the issue of the program freezing when the user switches from typing after selecting the 21st box back to clicking on any of the previous 20 boxes, we'll implement a solution that effectively "disables" mouse input after the 21st box is selected. The following steps can be followed to achieve so:
  1. Introduce a Flag to Control Mouse Input: First, introduce a boolean flag, 'mouseEnabled', which will control whether mouse inputs are processed. This flag will be set to true at the beginning of each block of your program, allowing mouse input. Once the 21st box is selected, this flag will be set to false, effectively ignoring further mouse inputs.
  2. Modify Mouse Input Processing: With the 'mouseEnabled' flag in place, we modify the section of the code that handles mouse inputs. Before processing any mouse events (like checking if the mouse is within bounds of an object), we check if 'mouseEnabled' is 'true'. If it's 'false', we skip the mouse input processing steps.
  3. Re-enable Mouse Input as Needed: Depending on the structure of the program and what happens after typing is completed, you might need to re-enable mouse input by setting 'mouseEnabled' back to 'true'. This step is crucial if your program enters another phase where mouse input should be processed again.
Here's the MATLAB code to reflect the above steps:
block = 1; % Assuming block starts at 1
blockMax = 10; % Example value for blockMax
ab = 1; % Assuming ab starts at 1
windowPtr = 1; % Placeholder for actual window pointer
% Placeholders for other variables you might have like Object, list, etc.
while block < (blockMax+1)
c = 1;
resp = 1;
mamaLoop = 0;
mouseEnabled = true; % Enable mouse input at the start of each block
while ab < 21
selected=0;
while selected==0
if mouseEnabled
% Record mouse events to move pics
[z,w,buttons]=GetMouse(windowPtr);
in_bounds = (z > [Object.LX{:}] & z < [Object.RX{:}]) & (w > [Object.LY{:}] & w < [Object.RY{:}]);
if any(in_bounds) && buttons(1) == 1
p= find(in_bounds, 1);
if p < 21
if strcmp(list(p).cue,list2(21,ab).cue)
selected = 1;
else
% Handle the case where it's not a match if needed
end
elseif p == 21
mouseEnabled = false; % Stop processing mouse clicks
selected = 1;
FlushEvents('keyDown');% Flush GetChar queue to remove stale characters
done = 0;
subresponse = []; % Assuming subresponse needs to be initialized
while done == 0 % Loop until <return> or <enter> is hit
[char,t]=GetChar; %%% HERE'S THE INPUT ACTION!!!!
switch(abs(char))
case {13, 3, 10} %if they hit enter or return, store their response
if isempty(subresponse)
done = 1;
selected = 1;
break
else
ab = ab + 1;
mamaLoop = 1;
done = 1;
selected = 1;
end
case {97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122}
if length(subresponse)== 12
else
subresponse=[subresponse char];
% Your code here for drawing
end
Screen('Flip', windowPtr);
end
end
end
end
end
end
end
Screen('Flip', windowPtr);
block = block + 1; % Ensure block is incremented to avoid infinite loop
end
The above code will prevent the program from freezing by not processing further mouse clicks after the 21st box is selected, allowing the typing input to proceed without interruption.
You may refer to the following documentation links to have a better understanding of GetMouse functions and Psychtoolbox:
  1. https://www.mathworks.com/matlabcentral/fileexchange/76411-psychtoolbox-3
  2. http://psychtoolbox.org/docs/GetMouse

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by