Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
'KeyPressFcn' not working inside a function.

Subject: 'KeyPressFcn' not working inside a function.

From: Zeph Davies

Date: 8 Dec, 2011 12:08:08

Message: 1 of 3

I have a code to register if a new key is pressed. When a key is pressed, it sets "keypressed" to 1. Once displayed, it sets it to zero, ready for the next press.

As a script (comment out the "function" and last "end" line), this code works well. But when in a function, the code stops working. I belive this is due to the "set(gcf,'KeyPressFcn','keypressed=1;');", which does work (check by removing the semicolon after the 1), but the value of "keypressed" does not change within the function "FunctionName", and remains zero.

function FunctionName
  keypressed = 0;
  set(gcf,'KeyPressFcn','keypressed=1;');
  while 1
    if keypressed==1
      fprintf(1,'%i\n',keypressed)
      keypressed = 0;
    else
      fprintf(1,'%i\n',keypressed)
    end
    pause(0.1);
  end
end

I am unable to get this script working within a function, as above. Please could anyone help?

Thanks,

Zeph

Subject: 'KeyPressFcn' not working inside a function.

From: Steven_Lord

Date: 8 Dec, 2011 14:21:50

Message: 2 of 3



"Zeph Davies" <matlab@zephmail.co.uk> wrote in message
news:jbq9b7$bbo$1@newscl01ah.mathworks.com...
> I have a code to register if a new key is pressed. When a key is pressed,
> it sets "keypressed" to 1. Once displayed, it sets it to zero, ready for
> the next press.
>
> As a script (comment out the "function" and last "end" line), this code
> works well. But when in a function, the code stops working. I belive this
> is due to the "set(gcf,'KeyPressFcn','keypressed=1;');", which does work
> (check by removing the semicolon after the 1), but the value of
> "keypressed" does not change within the function "FunctionName", and
> remains zero.

That's correct. Functions operate in their own workspaces; when you specify
the KeyPressFcn as a string it's evaluated as though you typed it in the
command window, and keypressed is created/updated in the BASE workspace, NOT
in the workspace of the FunctionName function.

When FunctionName was a script, it operated in the workspace of its caller;
if you call it from the command line, that means it operates in the base
workspace.

The easiest way to do this is to have the KeyPressFcn set the value in the
UserData property of the figure or the application data using SETAPPDATA and
have your loop query that property or the application data with GETAPPDATA.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: 'KeyPressFcn' not working inside a function.

From: Zeph Davies

Date: 8 Dec, 2011 15:37:08

Message: 3 of 3

"Steven_Lord" <slord@mathworks.com> wrote in message <jbqh5u$7ek$1@newscl01ah.mathworks.com>...
>
>
> "Zeph Davies" <matlab@zephmail.co.uk> wrote in message
> news:jbq9b7$bbo$1@newscl01ah.mathworks.com...
> > I have a code to register if a new key is pressed. When a key is pressed,
> > it sets "keypressed" to 1. Once displayed, it sets it to zero, ready for
> > the next press.
> >
> > As a script (comment out the "function" and last "end" line), this code
> > works well. But when in a function, the code stops working. I belive this
> > is due to the "set(gcf,'KeyPressFcn','keypressed=1;');", which does work
> > (check by removing the semicolon after the 1), but the value of
> > "keypressed" does not change within the function "FunctionName", and
> > remains zero.
>
> That's correct. Functions operate in their own workspaces; when you specify
> the KeyPressFcn as a string it's evaluated as though you typed it in the
> command window, and keypressed is created/updated in the BASE workspace, NOT
> in the workspace of the FunctionName function.
>
> When FunctionName was a script, it operated in the workspace of its caller;
> if you call it from the command line, that means it operates in the base
> workspace.
>
> The easiest way to do this is to have the KeyPressFcn set the value in the
> UserData property of the figure or the application data using SETAPPDATA and
> have your loop query that property or the application data with GETAPPDATA.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


That makes complete sense. Thank you for clarifying that!

Here's the working function I wrote following your guide:

function FunctionName
  set(gcf,'UserData',false);
  set(gcf,'KeyPressFcn','set(gcf,''UserData'',true);');
  while 1
    keypressed = get(gcf,'UserData');
    if keypressed
      fprintf(1,'%i\n',keypressed)
      set(gcf,'UserData',false);
    else
      fprintf(1,'%i\n',keypressed)
    end
    pause(0.1);
  end
end

Is this accurate to your solution?

(I could use "if get(..." directly, if i didn't print for testing)

Thanks Steve!

Zeph

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us