how to detect a keyboard key in a while loop using a figure
11 views (last 30 days)
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.
Tommy on 8 Jul 2020
You could use the figure's KeyPressFcn, possibly combined with some of the ideas below:
myFig = figure('KeyPressFcn', @myKeyPressFcn,...
a = 0;
a = a + rand;
drawnow % <- important so that myKeyPressFcn() gets called when a key is pressed
function myKeyPressFcn(src, evt)
if strcmp(evt.Key, 'space')
src.UserData = true;