Silly as it might seem, I cannot figure out exactly how "WindowKeyPressFcn" can be used to detect key presses (specifically, pressing the left/righ arrow keys) in a Figure window - this is because Matlab's help doesn't provide any example of how exactly you are supposed to use this callback in context.
Can anyone help? Many thanks!
function reading (src,evnt)
if strcmp(evnt.Key, 'rightarrow')==1
answer = 'right';
elseif strcmp(evnt.Key, 'leftarrow')==1
answer = 'left';
end
end
The only problem is that I need this code not as a function but as normal code (i.e. I don't want to call the function from within my script but I simply want its code to be executed).
"Catalin Eberhardt" <longt...@gmail.com> wrote in message
news:ih6jvk$9ba$1...@fred.mathworks.com...
> I figured out this code, which allows me to read arrow keypresses:
>
> function reading (src,evnt)
> if strcmp(evnt.Key, 'rightarrow')==1
> answer = 'right';
> elseif strcmp(evnt.Key, 'leftarrow')==1
> answer = 'left';
> end
> end
>
> The only problem is that I need this code not as a function but as normal
> code
A function IS what I would call "normal code" (at least until it starts
operating on very, very small numbers. Then it's denormal code.)
> (i.e. I don't want to call the function from within my script but I simply
> want its code to be executed).
I don't know what you're trying do so from this description. If you want
this to be your WindowKeyPressFcn, then just make it the WindowKeyPressFcn
for your figure.
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com
My question was how exactly do I use the keyword WindowKeyPressFcn into the code to achieve this (detecting left/right arrow keypresses).
Thanks!
> My question was how exactly do I use the keyword WindowKeyPressFcn into
> the code to achieve this (detecting left/right arrow keypresses).
WindowKeyPressFcn is not a keyword; it is a property that can be set for
figures. When a key press is detected on the figure, Matlab will
automatically invoke the routine indicated by that property.
set(gcf, 'WindowKeyPressFcn', @reading)
> I meant I do not want the code that detects the keypress to be inside
> a function that does just that (detecting keypresses).
You can do whatever you want in that callback routine.
I suspect you are thinking in terms of coding something like
while true
key_read = read_a_key();
if strcmp(key_read, 'right')
%do something
elseif strcmp(key_read, 'left')
%do the other thing
end
end
And what you need to understand is that detecting key presses on figures
is not a polled model (like the above), but is instead an event-driven
model: the named routine will be called each time a key _is_ pressed,
and the named routine has to trigger whatever action needs to be done.
If you want to use a polled model instead, then look on the Matlab File
Exchange (FEX) as there are a few different contributions that deal with
reading key presses.