How do I program a selective Key Press?

10 ビュー (過去 30 日間)
Marie West
Marie West 2016 年 7 月 12 日
コメント済み: Geoff Hayes 2016 年 7 月 18 日
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 件のコメント
Konark Kelaiya
Konark Kelaiya 2016 年 7 月 13 日
編集済み: Konark Kelaiya 2016 年 7 月 13 日
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 2016 年 7 月 18 日
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)?

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by