Operating based on specific letters presses

Hello,

I’m trying to write a code that tests if one of several specific letters was pressed on the keyboard and pending on that, does one of two things. Sounds simple, I know, but I’m having problems with this as specified:

The following example should define that cor=1 if ‘a’, ‘b’, ‘c’, or ‘d’ were pressed, and that cor=0 if any other key was pressed. However, while a single press on the ‘a’ key does produce cor=1, several presses on any other key are required in order for the code to proceed (and then the cor is correctly defined). I don’t understand why this is the case and couldn’t solve it. Any help will be appreciated!

Note that this is just an example and that in the final code this operation should be applied multiple times on different letters each time.

letters=[‘a’ ‘b’ ‘c’ ‘d’]
FlushEvents(‘Down’)
KeyIsDown=0;
[KeyIsDown,secs,KeyCode] = KbCheck;

while 1
if KeyIsDown
if KbName==letters(1) || KbName==letters(2)
cor=1;
break;
else
cor=0;
break;
end
end
end
return

Thank you,
Roy Shoval

FlushEvents is not needed here, it only affects GetChar() not KbCheck() or KbName(), drop it.

The line with KbCheck() must be inside your while loop or it will only check once.
While KbName() would wait for a keypress and tell you the key name, it isn’t meant for testing for keypresses, that’s what KbCheck, KbStrokeWait, KbPressWait etc. are for.

Look at KbDemo.m for how to do this stuff properly, also KbQueueDemo.m for more advanced ways.

For any further paid advice from myself → help PsychPaidSupportAndServices.
-mario