matlabtimeresponsegetcharpsychtoolbox

using getchar in PTB in MATLAB


    while 1

    ch = GetChar
    KbWait
    if (ch>48) && (ch<53) 
         trial(j).RT =  GetSecs - startTime ;
       break;
    end
    end

here is my sample code, I am working on an Stroop Task, I am expecting to gatjer reaction time. Reaction time starts with stimulus presentation and end with key press. I use the code above to supress whole keyboard expect the numbers of 1-2-3-4. however, sometimes all keys can be pressed rather than just 1-2-3-4. I hav tried many times, it some times supresses keys sometimes it doesn't. I really do not understand the reason.


Solution

  • The GetChar() function waits for a keypress, or retrieves previously pressed keys from a queue: http://docs.psychtoolbox.org/GetChar . What may be happening is that you have previous keypresses in the queue which are being read by GetChar, even if they are not the most recent press.

    However, it is recommended by the Psychtoolbox developers to not use the GetChar() function to collect response times. This is due to the timing prevision of GetChar() versus other functions such as KbCheck().

    The following snippet could be used to poll the keyboard for a response time:

    % find the keyscan codes for the first four number keys
    % (top of the keyboard keys, number pad keys have different codes)
    keysToWaitFor = [KbName('!1'), KbName('2@'), KbName('3#'), KbName('4$')];
    
    responded = 0;
    while responded == 0
    
        [tmp,KeyTime,KeyCode] = KbCheck(-3);
    
        if KeyCode(keysToWaitFor)
            trial(j).RT =  KeyTime - startTime;
            responded = 1;
        end
    
        % time between iterations of KbCheck loop
        WaitSecs(0.001);
    end