How to use correctly KbQueue

I am trying to understand how to correctly use KbQueue.

Assuming that I have a infinite loop (while 1) I want to break the loop if the space key is pressed.

Is the following correct?

    KbWait; % press any key to start the movement

    % create keyboard monitoring queue with target buttons
    keyFlags = zeros(1,256); % an array of zeros
    keyFlags(44)=1; % monitor only spaces
    dvc = GetKeyboardIndices;
    KbQueueCreate(dvc, keyFlags); % initialize the Queue
    EXIT_SIGNAL = 0;
    while ~EXIT_SIGNAL
        KbQueueStart;% start keyboard monitoring
        # some moving stimuli here

        [pressed, firstPress, firstRelease, lastPress, lastRelease]=KbQueueCheck;% check if space was pressed
        if pressed
            if any(strcmp(KbName(find(firstPress)), 'space'))%if space was pressed
                EXIT_SIGNAL = 1;

Is it correct to put KbQueueStart inside the loop? Or maybe outside?
Any better more “correct” way to do that?

Put start, stop and release outside the while loop. Also, instead of hardcoding 44, use kbname to figure out what number corresponds to the slace bar, this may not always be the same across systems/platforms

Thanks for the tips. Indeed, hard coding 44 is not a good idea.

Concerning the queue, you suggest to put start before the loop, close after the end of the loop (if break) and the release also at the end?

Exactly, you want the queue running all the time as long as you are waiting for a response, you can read from it while its running

1 Like

thanks for the tips. got it!