How can I evaluate a script using a WindowKeyPressFcn?

7 views (last 30 days)
MATLAB experts,
I'm using GUIDE to generate a GUI that has 5 push buttons, each of which runs a script upon being pressed. I'd also like to bind a key to each of those scripts. It seems like WindowKeyPressFcn is what I'm looking to use, but I'm not sure how to use that to make a key either press the push button or just run the script (either will work perfectly well!).
If this is a redundant question, please direct me somewhere where this has been answered, most threads I've already seen have rather complicated explanations.
Thanks in advance.

Accepted Answer

Walter Roberson
Walter Roberson on 14 Aug 2015
Create a single WindowKeyPressFcn callback for your figure, and have it check which key was pressed. There is an example at http://uk.mathworks.com/matlabcentral/answers/210241#answer_175778
  2 Comments
Joseph O'Keefe
Joseph O'Keefe on 14 Aug 2015
Edited: Walter Roberson on 15 Aug 2015
I feel like I'm getting close. I'm just trying to do basic commands upon pushing the button to make sure everything's going through. I'm getting an error message though.
Current code:
function pushbutton1_Callback(hObject, eventdata, handles)
disp(1)
function pushbutton2_Callback(hObject, eventdata, handles)
disp(2)
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
switch eventdata.Key
case 'a'
pushbutton1_Callback(handles.pushbutton1, [], handles);
case 's'
pushbutton2_Callback(handles.pushbutton2, [], handles);
end
When I try to press a key from the GUI window, I get this:
Error while evaluating figure WindowKeyPressFcn
Error using edit (line 66)
Undefined function or variable 'hObject'.
Error in helpUtils.errorDocCallback (line 23)
if ~edit(editTopic)
Any ideas?
Walter Roberson
Walter Roberson on 15 Aug 2015
Please show the line in which you set the WindowKeyPressFcn callback. Also please use the Property Inspector on the figure and see what is configured for WindowKeyPressFcn and show what it is (it might be a string)

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!