Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Detecting presses of left/right arrow using WindowKeyPressFcn

849 views
Skip to first unread message

Catalin Eberhardt

unread,
Jan 19, 2011, 5:46:06 AM1/19/11
to
Hi everyone,

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!

Catalin Eberhardt

unread,
Jan 19, 2011, 7:04:04 AM1/19/11
to
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 (i.e. I don't want to call the function from within my script but I simply want its code to be executed).

Steven_Lord

unread,
Jan 19, 2011, 9:43:41 AM1/19/11
to

"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

Catalin Eberhardt

unread,
Jan 19, 2011, 12:17:06 PM1/19/11
to
I meant I do not want the code that detects the keypress to be inside a function that does just that (detecting keypresses).

My question was how exactly do I use the keyword WindowKeyPressFcn into the code to achieve this (detecting left/right arrow keypresses).

Thanks!

Think blue, count two.

unread,
Jan 19, 2011, 12:29:03 PM1/19/11
to
On 19/01/11 11:17 AM, Catalin Eberhardt wrote:

> 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.

Eyal

unread,
Sep 11, 2013, 10:20:07 AM9/11/13
to
"Catalin Eberhardt" wrote in message <ih6jvk$9ba$1...@fred.mathworks.com>...
Just for "better" programming, you might wanna use:
switch(event.Key)
case 'rightarrow'
blabla1
case 'leftarrow'
blabla2
end

instead of doing all those strcompares ..

Eyal

Incredible Innovators

unread,
Oct 20, 2013, 2:48:07 AM10/20/13
to
"Eyal" wrote in message <l0pu6n$s2h$1...@newscl01ah.mathworks.com>...
Thanks a lot for your message.
But I'm totally new to programming.
So, please tell me what to insert in 'event'?
Thank you

Oscar

unread,
Sep 2, 2015, 10:31:11 AM9/2/15
to
"AwedBy Matlab" wrote in message <ih6jvk$9ba$1...@fred.mathworks.com>...

Oscar

unread,
Sep 2, 2015, 10:33:09 AM9/2/15
to
Does anybody knows what are the input argumnts of this funcion. This function needs some comments to allow people to use it.

Steven Lord

unread,
Sep 3, 2015, 10:43:47 AM9/3/15
to


"Oscar" <oscar...@hotmail.com> wrote in message
news:ms71b0$ql3$1...@newscl01ah.mathworks.com...
> Does anybody knows what are the input argumnts of this funcion. This
> function needs some comments to allow people to use it.

You don't call the WindowKeyPressFcn directly. You set that property of the
figure object and MATLAB will call the function you specify when a key press
occurs while the figure or one of its children has focus. As for the
signature it must have, see the documentation for this property,
specifically the "How to Specify Callback Property Values" section linked
therein.

http://www.mathworks.com/help/matlab/ref/figure-properties.html?searchHighlight=WindowKeyPressFcn

--
Steve Lord
sl...@mathworks.com
0 new messages