How do I program a selective Key Press?

10 visualizaciones (últimos 30 días)
Marie West
Marie West el 12 de Jul. de 2016
Comentada: Geoff Hayes el 18 de Jul. de 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 comentarios
Konark Kelaiya
Konark Kelaiya el 13 de Jul. de 2016
Editada: Konark Kelaiya el 13 de Jul. de 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 el 18 de Jul. de 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)?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre App Building en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by