Pushbutton activated by Enter(return) key

조회 수: 18 (최근 30일)
Petr
Petr 2012년 10월 31일
Hi,
I would like to ask you why doesn't work when I call funtion pushbutton1_Callback inside pushbutton1_KeyPressFcn. I have besidde other things buttons where I want them to react to Enter key when user Tabs to it (+ ofcourse when user just click at it). I thought that the simplest way would be to call pushbutton1_Callback in pushbutton1_KeyPressFcn,but obviously not...how caan I do this feature ?
Thanks, Peter
  댓글 수: 5
Sean de Wolski
Sean de Wolski 2012년 11월 1일
How are you running this GUI? Are you just double-clicking on the figure? You need to run the *.m file associated with the figure:
>>myGUI
Petr
Petr 2012년 11월 2일
편집: Petr 2012년 11월 2일
Yes I'm useing GUIDE and I am running my program from *.m file...even when I use handles it show same error...
I dont get why error mentiones handles...I dont want to use that i want to call a Callback function from another function...
Image Analyst method is also a good idea, but it's doing the same think...
For example this in my codeworks perfectly....
function uipushtool4_ClickedCallback(hObject, eventdata, handles) % hObject handle to uipushtool4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
pushbutton2_Callback

댓글을 달려면 로그인하십시오.

채택된 답변

Petr
Petr 2012년 11월 7일
Solution was simple and enough elegant for me...
.
function pushbutton1_KeyPressFcn(hObject, eventdata, handles)
key = get(gcf,'CurrentKey');
if(strcmp (key , 'return'))
pushbutton1_Callback(hObject, eventdata, handles)
end
function pushbutton1_Callback(hObject, eventdata, handles)
%code to be executed
  댓글 수: 1
Luis Santiago Hernández Castillo
Maybe this one could be a good option too:
%bhok is the handles of your pushbutton.
%xxx represents any data for ur code.
bhok.Callback=@(src,~)okcbd(src,xxx);
bhok.KeyPressFcn=@(src,event)bhokcall(src,event,xxx);
function bhokcall(src,event,xxx)
if event.Key=='return' %#ok<BDSCA>
okcbd(src,xxx)
end
end
function okcbd(src,xxx)
%code to be executed
end

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2012년 10월 31일
You can do this, you just need to feed it the correct inputs. You could probably find these by looking in the property inspector in GUIDE at the callback to see what GUIDE is feeding it.
  댓글 수: 1
Petr
Petr 2012년 11월 2일
what do you mean with correct inputs ?

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2012년 11월 1일
Just put the inside of the click callback into its own function, like Button1Pressed() or something like that.
function handles=Button1Pressed(handles)
% whatever it does....
Then have the click and keypress callbacks both call Button1Pressed(). You don't need to pass in anything other than handles.
  댓글 수: 1
Petr
Petr 2012년 11월 2일
Good idea, but same error and doesnt work :)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by