How do I use KeyPressFcn? How do I time the space bar press?

10 views (last 30 days)
I don't understand how to use the KeyPressFcn. I'm not making a GUI. I'm using an image on an axis. This is the relevant code:
imshow(imscreen);
imscreen = gcf;
a = 1;
set(gcf, 'KeyPressFcn', 'a = get(gcf, ''CurrentCharacter'')');
a = double(a);
*and this is where I set the conditions for x values (trying to move the ball horizontally):*
if a == 28
xclick = x2 - 2;
elseif a == 29
disp('Right')
xclick = x2 + 2;
end
and then I set the ball to the new xclick value. This doesn't seem to work. What should I do?

Answers (1)

Walter Roberson
Walter Roberson on 1 Dec 2015
When you use a string as a callback, the string is executed in the context of the base workspace.
You need to set the KeyPressFcn to be the handle of a function that will test the key and perform the appropriate action. Or you need to switch to one of the File Exchange contributions such as GetKey or GetKeyWait

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!