GUI for keyboard pressed representing the push button

24 ビュー (過去 30 日間)
Yeoh
Yeoh 2011 年 2 月 16 日
コメント済み: Walter Roberson 2023 年 1 月 4 日
Hi,
Does it any way to represent the keyboard pressed for the callback of pushbutton? For example, when 'A' in computer keyboard is pressed, MATLAB GUI for pushbutton1 will be triggered automatically and do the corresponding callback. When 'B' from keyboard is pressed, then GUI will do the callback for pushbutton2.
This is especially want to develop for the blind people so that they can control the GUI by keyboard without the help of computer mouse.
Hope there is some suggestion and example of coding. Thank you.
  2 件のコメント
Amir Mohammad Alizadeh
Amir Mohammad Alizadeh 2023 年 1 月 3 日
What I am trying to do is opposite of that, I want to associate a button push in GUI with "esc" key. Any suggestions please? Thank you
Walter Roberson
Walter Roberson 2023 年 1 月 4 日
You can modify the escape key detection routine to directly call the callback function you would like associated with the button push. Or, alternately, you could modify the escape key detection routine to use the Java "Robot" class to simulate a button push.

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

採用された回答

Matt Fig
Matt Fig 2011 年 2 月 16 日
Yes. Simply put the callbacks for the correct pushbuttons in the keypressfcn of the figure. You may need an enormous switchyard if you have one button for each key on the keyboard.
function [] = fig_keypressfcn(varargin)
% The keypressfcn for the figure.
switch varargin{2}.Key
case 'a'
case 'b'
...
  1 件のコメント
Yeoh
Yeoh 2011 年 2 月 17 日
May I know what is the 'figure' that you mean? Because the push buttons for each key on the keyboard will generate the alphabets on the text box in GUI without any graphs or any figure.
Actually I have to create a new function? I mean where should I put the keypressfcn, in pushbutton callback? or the text field?
Thank you very very very much..

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

その他の回答 (4 件)

Jiro Doke
Jiro Doke 2011 年 2 月 17 日
I assume you are using GUIDE to create your GUI. In that case, create a WindowKeyPressFcn for the figure (the figure is the main GUI window). Right click on the background of your GUI in GUIDE, and there should be a "WindowKeyPressFcn" callback for that. Then in the callback code, use switch statement to deal with different key presses:
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
switch eventdata.Key
case 'a'
<callback function corresponding to "a">
case 'b'
<callback function corresponding to "b">
...
end
You may also want to compare the uses of KeyPressFcn for the figure and for the uicontrol. Depending on how you want the key presses to behave when different things have focus, you may choose one over the other.
  4 件のコメント
Yeoh
Yeoh 2011 年 2 月 18 日
Oh, at first I call the function (ENTER_A_callback) in the function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
such as
switch eventdata.Key
case 'a'
ENTER_A_Callback();
case 'b'
ENTER_B_Callback();
end
May be in the WindowKeyPressFcn, it can't call the sub-function.
Then I try in this way:
switch eventdata.Key
case 'a'
set(handles.output,'String','A');
case 'b'
set(handles.output,'String','B');
end
Yeah, it works! The text box will display "A" when I press keyboard "A", so as for B.
Thank you very much.
Muhammad Faheem Irfan
Muhammad Faheem Irfan 2012 年 5 月 15 日
Good answer.............

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


Oleg Komarov
Oleg Komarov 2011 年 2 月 16 日
A simple example which uses KeyPressFcn:
function [] = gui()
S.fh = figure('units','pixels',...
'position',[500 200 200 100],...
'menubar','none',...
'name','gui',...
'numbertitle','off',...
'resize','off');
S.pb = uicontrol('style','push',...
'units','pix',...
'position',[10 10 180 40],...
'fontsize',14,...
'string','Hi!');
S.tx = uicontrol('style','text',...
'units','pix',...
'position',[10 55 180 40],...
'string','goodbye',...
'fontsize',23);
set(S.pb,'callback' ,{@pb_call,S})
% Check if 'p' is pressed when focus on button and exec callback
set(S.pb,'KeyPressFcn',{@pb_kpf ,S});
% Check if 'p' is pressed when focus on figure and exec callback
set(S.fh,'KeyPressFcn',{@pb_kpf ,S});
% Callback for pushbutton, prints Hi! in cmd window
function pb_call(varargin)
S = varargin{3}; % Get the structure.
set(S.tx,'String', get(S.pb,'String'))
% Do same action as button when pressed 'p'
function pb_kpf(varargin)
if varargin{1,2}.Character == 'p'
pb_call(varargin{:})
end
Oleg
  3 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 2 月 17 日
I'm not trying to position the mouse. I'm using the KeyPressFcn callback.
I modified the code above to update a statis text box. Try to run it.
Yeoh
Yeoh 2011 年 2 月 17 日
Yeah, it works.. But I'm using GUIDE to create my GUI. So can I add those coding in my M-file? Thank you.

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


Jonathan
Jonathan 2011 年 9 月 22 日
I got a question here if I want to do a case where I press 'CTRL+a' how do I write it in the case?
  1 件のコメント
Walter Roberson
Walter Roberson 2011 年 9 月 22 日
You need to check that strcmp(eventdata.Modifier,'control') is true. That cannot be done directly in the case label for 'a' but it can be done within the body... or you could test the modifier before the switch and use different switch trees for the different modifiers.

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


SN MJTHD
SN MJTHD 2014 年 7 月 16 日
Dear Matlab Experts
I am doing a similar work but there is a difference. For the moment, I`m using two Push Buttons in my GUI with names LEFT and RIGHT. but I have Three Conditions like Left, Right and NoResponse. NoResponse means non of Left or Right Push Buttons are pushed.
Now I`m trying to use both keyboard and push buttons for my purpose. For example "S" key as Left Push Button and "L" key as Right Push Button and if non of them are pushed NoResponse be done.
I tried many ways but I encountered different errors.
I appreciate any help.

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by