how to detect a keyboard key in a while loop using a figure

25 ビュー (過去 30 日間)
Ahsan Ayyaz
Ahsan Ayyaz 2020 年 7 月 7 日
コメント済み: Matt 2023 年 10 月 27 日
I am using arduino nano and checking push buttons in a while loop. I want to detect a keyboard key, say 'spacebar' and simulate a push button. A figure with a push button is the desired output. Everytime the figure is in focus and the 'spacebar' is pressed on the keyboard, it should turn on and off a digital output of arduino board.

回答 (1 件)

Tommy
Tommy 2020 年 7 月 8 日
You could use the figure's KeyPressFcn, possibly combined with some of the ideas below:
myFig = figure('KeyPressFcn', @myKeyPressFcn,...
'UserData', false);
a = 0;
while true
a = a + rand;
if myFig.UserData
close(myFig)
break
end
drawnow % <- important so that myKeyPressFcn() gets called when a key is pressed
end
disp(a)
function myKeyPressFcn(src, evt)
if strcmp(evt.Key, 'space')
src.UserData = true;
end
end
  3 件のコメント
Tommy
Tommy 2020 年 7 月 10 日
Glad to hear you got it working!
Matt
Matt 2023 年 10 月 27 日
I am trying to implement something similar for a spacebar clicker replica. I want to add a time interval to your code so that spacebar press is emulated after a set interval. can you please help me out here? thanks in advance.

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

カテゴリ

Help Center および File ExchangeTiming and presenting 2D and 3D stimuli についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by