Using WindowKeyPressFcn with uicontrol

54 ビュー (過去 30 日間)
Ara
Ara 2014 年 8 月 16 日
コメント済み: Le Dung 2019 年 1 月 18 日
From my code
snap = uicontrol(f,'Style','pushbutton','String','Recognize',... 'units','characters',... 'Position',[5 1.5 30 5],'Callback',{@snapshot_callback});
How to push button by using key press from keyboard and if I want to push button by spacebar how to specify it?
Thank you so much :)
  2 件のコメント
Geoff Hayes
Geoff Hayes 2014 年 8 月 16 日
Ara - what is it exactly that you want to happen? Do you want a pressed key on the keyboard to simulate the pressing of a button on the GUI?
Ara
Ara 2014 年 8 月 17 日
Yes

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

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 8 月 18 日
Ara - if you want the pressing of the spacebar to simulate the pressing of the snap button, you can do the following: in your code that generates the GUI, assign a callback to the figure for the WindowKeyPressFcn as
set(f,'WindowKeyPressFcn',@keyPressCallback);
where f is your figure. The keyPressCallback will be called whenever the figure/GUI has focus and a key on the keyboard is pressed. Now define the callback (within the same file) as
function keyPressCallback(source,eventdata)
% determine the key that was pressed
keyPressed = eventdata.Key;
if strcmpi(keyPressed,'space')
% the key that was pressed was the space bar so set focus to
% the snap button
uicontrol(snap);
% invoke the callback for the snap button
snapshot_callback(snap,[]);
end
end
In the above, we use uicontrol to set focus to the snap button, and then we call its callback. I had to do this in my environment, so make sure that it is necessary for you to do the same. (Using uincontrol didn't automatically call the callback for the button.)
Try the above and see what happens!
  6 件のコメント
Walter Roberson
Walter Roberson 2019 年 1 月 17 日
you already have an open question on this where people have been giving you code.
Le Dung
Le Dung 2019 年 1 月 18 日
Yes. Thank you so much.
I know. but i don't know how to use KeyPressFcn for my problem

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by