How do I program a selective Key Press?

10 Ansichten (letzte 30 Tage)
Marie West
Marie West am 12 Jul. 2016
Kommentiert: Geoff Hayes am 18 Jul. 2016
I'm trying to program an experiment that advances if a certain key is pressed. I can't figure out how to do this however, and the trial advances when any key is pressed. I have spent so much time comparing my code to other people's and I can't figure out what is making mine function differently. Please help!
%%Set up keyboard
% Make KbName use shared name mapping of keys between PC and Mac
KbName('UnifyKeyNames');
space_key = KbName('space');
z_key = KbName('z');
m_key = KbName('m');
q_key = KbName('q');
% Lets write three lines of text, the first and second right after one
% another, and the third with a line space in between. To add line spaces
% we use the special characters "\n"
line1 = 'Are you ready to begin the experiment?';
line2 = '\n (press the space bar to continue)';
% Draw all the text in one go
Screen('TextSize', window, 70);
DrawFormattedText(window, [line1 line2],...
'center', screenYpixels * 0.25, white);
while 1
[~, ~, key] = KbCheck;
if strcmp(KbName(key),'space')
break
else
end
end
% Clear the screen
sca;
  3 Kommentare
Konark Kelaiya
Konark Kelaiya am 13 Jul. 2016
Bearbeitet: Konark Kelaiya am 13 Jul. 2016
Well keypressfcn of UI will do all this, even you don't need to create a GUI. Only create dummy figure with visibility "off" and use keypressfcn callback to complete your task.
Otherwise MATLAB is not the right candidate for Key press functions. you have to create a big function to do all jobs like thread / pause / polling for 5/10 sec and if no activity from keyboard-> assign error.
you can check this link
however it use fundamental of Java in MATLAB
Geoff Hayes
Geoff Hayes am 18 Jul. 2016
Marie - please clarify where in your code the "trial advancement" is occurring. Your code writes out a line prompting the user to press the spacebar to continue, so presumably that happens and so the condition
strcmp(KbName(key),'space')
is true and so the break is called and the while loop is exited. Are you saying that if the user presses any key other than the space bar, then the above condition is still somehow true and the trial "advances" (i.e. the while loop terminates)?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by