Matlab PTB not recognising labchart stimulated key presses
1 view (last 30 days)
Show older comments
Hi all,
A labchart macro presses the i and e keys in the backrgound when certain thresholds are met. When no program is running, the command window picks up the i and e keys. However, when I run my PTB experiement, it can't find these key presses. If i manually press i and e, however, it works. Can someone advise on what's going on or how to solve this?
Section of script:
% please wait screen + countdown
function displayBlankScreenForDuration(params)
KbQueueCreate();
KbQueueStart();
Screen('FillRect', params.window, params.black);
totalTime = 60;
startTime = GetSecs();
endTime = startTime + totalTime;
inhaleTimes = [];
exhaleTimes = [];
breathingCycles = [];
while GetSecs < endTime
[pressed, firstPress] = KbQueueCheck();
if pressed
if firstPress(KbName('i'))
inhaleTime = firstPress(KbName('i'));
inhaleTimes(end+1) = inhaleTime;
disp('Inhale detected');
end
if firstPress(KbName('e'))
exhaleTime = firstPress(KbName('e'));
exhaleTimes(end+1) = exhaleTime;
disp('Exhale detected');
if ~isempty(inhaleTimes)
% Calc
breathingCycles(end+1) = exhaleTimes(end) - inhaleTimes(end);
end
end
end
remainingTime = round(endTime - GetSecs());
message = sprintf('Please wait - %d seconds remaining.', remainingTime);
Screen('FillRect', params.window, params.black);
Screen('TextSize', params.window, 40);
DrawFormattedText(params.window, message, 'center', 'center', params.white);
Screen('Flip', params.window);
WaitSecs(0.01);
end
0 Comments
Answers (1)
Manikanta Aditya
on 28 Feb 2024
Hey Danielle,
It seems that the issue you are facing is related to the interaction between your labchart macro and your PTB experiment. When the labchart macro is running, it sends key presses in the background, which are picked up by the command window. However, when your PTB experiment is running, it is unable to detect these key presses.
One possible solution is to modify your PTB script to directly listen for key presses using the 'KbCheck' function instead of using the 'KbQueue' function. Here's an example of how you can modify your script:
% please wait screen + countdown
function displayBlankScreenForDuration(params)
KbQueueRelease(); % Release the KbQueue if it was created previously
Screen('FillRect', params.window, params.black);
totalTime = 60;
startTime = GetSecs();
endTime = startTime + totalTime;
inhaleTimes = [];
exhaleTimes = [];
breathingCycles = [];
while GetSecs < endTime
[keyIsDown, secs, keyCode] = KbCheck();
if keyIsDown
if keyCode(KbName('i'))
inhaleTime = secs;
inhaleTimes(end+1) = inhaleTime;
disp('Inhale detected');
end
if keyCode(KbName('e'))
exhaleTime = secs;
exhaleTimes(end+1) = exhaleTime;
disp('Exhale detected');
if ~isempty(inhaleTimes)
% Calc
breathingCycles(end+1) = exhaleTimes(end) - inhaleTimes(end);
end
end
end
remainingTime = round(endTime - GetSecs());
message = sprintf('Please wait - %d seconds remaining.', remainingTime);
Screen('FillRect', params.window, params.black);
Screen('TextSize', params.window, 40);
DrawFormattedText(params.window, message, 'center', 'center', params.white);
Screen('Flip', params.window);
WaitSecs(0.01);
end
end
I hope this helps!
0 Comments
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!