Key presses not being logged with kbCheck

18 views (last 30 days)
Jade T
Jade T on 18 Mar 2023
Commented: Jade T on 19 Mar 2023
I have been mostly programming this task on the computer in my lab but I was trying to finalize and do debugging. On the lab computer, my keypresses were being logged with mostly the same code (~95%, I've made some placement changes). Now, (on my macOS laptop) it is not working. The variable keyIsDown reports no key is pressed (0) but somehow it is advancing through my loop. I have spent a couple hours trying to look at the documentation and perusing the internet, but nothing provides me to a solution. I know psychtoolbox has a neverending list of issues with functionality using macOs but Im wondering if I am just overlooking something in the code.
while KbCheck;
[keyIsDown, secs, keyCode] = KbCheck(-1);
if keyIsDown
if keyCode(accept_key) == 1
pchoice = 1;
trialMatrix(t,1)= cellstr(pchoice_name{pchoice});
WaitSecs(2);
FlushEvents('keyDown');
%%% some conditional statements for other keypresses follow but are not relevant to my question, so they have been removed (it's alot) %%%
else keyCode(reject_key); % this is what I have been pressing
pchoice = 2;
Screen('TextSize',ptb.win, 50);
DrawFormattedText(ptb.win, balance_string ,'center', 'center', [0 0 0]);
Screen('Flip', ptb.win);
WaitSecs(2);
FlushEvents('keyDown');
cd('/Users/janedoe/Documents/Task Code'); % if choice is reject...
fix=imread('fixationcross.jpg');
fixc= Screen('MakeTexture', ptb.win, fix);
Screen('DrawTexture', ptb.win, fixc,[],[]);
Screen('Flip', ptb.win);
end
trialMatrix(t,1)= cellstr(pchoice_name{pchoice});
end
end
  5 Comments
Walter Roberson
Walter Roberson on 19 Mar 2023
If I understand what you are saying earlier, on the Mac each time you execute
[keyIsDown, secs, keyCode] = KbCheck(-1);
then you get back keyIsDown is 0? And if I understand correctly, you are saying that despite KeyIsDown being 0, that the code is "advancing through your loop" ?
Could you clarify what it means for your code to be "advancing through your loop" ? Is there a statement inside of the if keyIsDown section that is being executed even though your debugging appears to show that KeyIsDown is 0?
Or is the point that keyIsDown never seems to be non-zero even though you are pressing keys? Because that would be a quite different question than if keyIsDown is 0 but some statement inside the if KeyIsDown executing anyhow.
Or is the problem that keyIsDown is non-zero even though no keys have been pressed?
Some users of Laptops experienced the problem of “stuck keys”: Some keys
are always reported as “down”, so KbWait returns immediately and KbCheck
always reports keyIsDown == 1. This is often due to special function keys.
These keys or system functionality are assigned vendor specific
key codes, e.g., the status of the Laptop lid (opened/closed) could be
reported by some special keycode. Whenever the Laptop lid is open, this key
will be reported as pressed. You can work around this problem by passing
a list of keycodes to be ignored by KbCheck and KbWait. See
help DisableKeysForKbCheckon how to do this.
Jade T
Jade T on 19 Mar 2023
Well the kb check is nested within a for loop ( this is what I mean about advancing through) so for example:
one trial contains some images and requires user input, then it should advance to the next one- also with user input. When the specific key press happens (ie reject) ...pchoice = 2 should be stored and saved, but it is not being saved. Therefore, I am not sure if its because keyIsDown is 0, therefore it will not go to the next conditional statement. I have modified my code now so that it looks like this:
while KbCheck
end
go = 0;
while go == 0
[keyIsDown, secs, keyCode] = KbCheck(-1);% read the current status of the keyboard; secs is the time of the key press
if keyIsDown % keyIsDown is a variable that is a 1 if a key is pressed and 0 if no key is pressed
if keyCode(accept_key) % logic test, if the value of keyCode corresponding to 'accept_key' is pressed
pchoice = 1;
else
keyCode(reject_key)
pchoice = 2;
trialMatrix(t,1)= cellstr(pchoice_name{pchoice});
end
end
end
%------conditional if statement folows for pchoice= 1
% if choice is reject...
WaitSecs(1);
Screen('TextSize',ptb.win, 50);
DrawFormattedText(ptb.win, balance_string ,'center', 'center', [0 0 0]);
Screen('Flip', ptb.win);
WaitSecs(1);
% cd('/Users/jt394/Documents/Task Code');
fix=imread('fixationcross.jpg');
fixc= Screen('MakeTexture', ptb.win, fix);
Screen('DrawTexture', ptb.win, fixc,[],[]);% double brackets signifies default drawing to center of the screen
Screen('Flip', ptb.win);
end

Sign in to comment.

Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!