how can we control push button in matlab gui by keyboard instead of mouse click?

3 ビュー (過去 30 日間)
heisenberg
heisenberg 2015 年 2 月 21 日
回答済み: hari kantona 2019 年 12 月 13 日
how to use keypressFcn to assign the key for push button.?

採用された回答

Geoff Hayes
Geoff Hayes 2015 年 2 月 22 日
heisenberg - if you are using GUIDE to create your GUI, then you could assign a WindowKeyPressFcn callback to your figure (GUI) and then, based on the key pressed while the figure has focus, you could call the appropriate button.
Suppose that your GUI/figure is named figure1 and you have one push button (named pushbutton1) that you wish to fire when you press the x key. Your callback to capture key presses could look something like
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
% determine the key that was pressed
keyPressed = eventdata.Key;
if strcmpi(keyPressed,'x')
% set focus to the button
uicontrol(handles.pushbutton1);
% call the callback
pushbutton1_Callback(handles.pushbutton1,[],handles);
end
In the above we check for when the x is pressed and then simulate the pressing of the button by calling the appropriate callback. You may not need to set focus to the button using uicontrol but I don't think that it hurts to do this as well.
  5 件のコメント
heisenberg
heisenberg 2015 年 2 月 23 日
i used separate keypressfcn for 2 pushbuttons.
Geoff Hayes
Geoff Hayes 2015 年 2 月 24 日
Please post some of your code so that I can get an idea of what you have implemented.

サインインしてコメントする。

その他の回答 (2 件)

Wajih  Khan
Wajih Khan 2017 年 12 月 6 日
when I press a key it just types in the command window and nothing else happens... help
  1 件のコメント
Geoff Hayes
Geoff Hayes 2017 年 12 月 11 日
Wajih - please post this as a new question rather than as an answer. Include any relevant code.

サインインしてコメントする。


hari kantona
hari kantona 2019 年 12 月 13 日
anusefull jmbt

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by