GUI: how to call callback function from keyboard using 'windowkeypressfcn'?

조회 수: 16 (최근 30일)
Hi all,
I've pushbutton callback function as below:
function movefront_Callback(hObject, eventdata, handles)
handles.speed.String = num2str(str2double(handles.speed.String) +1);
And I would like to call this function from the keyboard, so I've this:
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
switch eventdata.Key
case 'a'
handles.speed.String = num2str(str2double(handles.speed.String) + 1);
case 'b'
handles.speed.String = num2str(str2double(handles.speed.String) - 1);
end
The above code works but I called it directly from the handles structure. How do I call the callback function from the windowskeypress function?
e.g
switch eventdata.Key
case 'a'
@movefront_Callback(); %not working obviously
thanks in advance.

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 4월 18일
James - all you need to do is to just call the callback directly and ensure that you pass the correct inputs. Since your callback signature is
function movefront_Callback(hObject, eventdata, handles)
then you need to pass the handle to the movefront control (this is hObject), the event data (which can be empty as it seems to be only valid for key presses and other events like that) and the handles structure. So your key press callback can become
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
switch eventdata.Key
case 'a'
movefront_Callback(handles.movefront, [], handles);
case 'b'
moveback_Callback(handles.moveback, [], handles);
end
Try the above and see what happens!

추가 답변 (0개)

카테고리

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