Using WindowKeyPressFcn with uicontrol

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일

1 개 추천

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

Ara
Ara 2014년 8월 18일
Thank you It's work! :)
Thomas Hermelin
Thomas Hermelin 2018년 11월 20일
Hi, what if the GUI was made in GUIDE?
Thanks !
Thomas - you could probably use the above and just set the callback for the WindowKeyPressFcn in the OpenFcn of the GUI
set(handles.figure1,'WindowKeyPressFcn',@keyPressCallback);
where you handles.figure1 is the handle to the GUIDE-created GUI.
Le Dung
Le Dung 2019년 1월 17일
편집: Le Dung 2019년 1월 17일
i have a problem. could you help me.
i have a GUI contains a listbox and a editbox. as picture below:
Listbox have a array of strings:
a
an
able
back
bath
bench
car
create
common
claim
dad
did
dear
eat
edit
english
enable
far
father
fish
jacket
jet
kiss
gather
get
gun
...
and so on
Now, i want to type a word "jacket" (for example) into edittext. i expect when i type word "j" on edittext, the listbox will display all word that begin with word "j". Similarly, when i import a consecutive word, "a", (now, i'm having "ja" on edittext). the listbox will display all word that contains words "ja". and so on
Thank you so much
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개)

카테고리

도움말 센터File Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

질문:

Ara
2014년 8월 16일

댓글:

2019년 1월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by