switching focus for keyboard input in a GUIDE created function

조회 수: 1 (최근 30일)
Jan Meyer
Jan Meyer 2016년 2월 8일
댓글: Jan Meyer 2016년 3월 2일
I want to create a slider, after which you use it, a push button appears. However, instead of clicking the button, I want to enable the user to simply push a button on the keyboard with the same effect as pushing the pushbutton. The problem, I encounter, is that the pushbutton is not in the focus. There have been some answers out there, how to do it within the uicontrol environment. I want to know, how to do it within the GUI environment. Here is a sample code:
function varargout = movedslider(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @movedslider_OpeningFcn, ...
'gui_OutputFcn', @movedslider_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
function movedslider_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
guidata(hObject, handles);
function varargout = movedslider_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
function slider1_Callback(hObject, eventdata, handles)
global v1 v2
v1=get(handles.slider1, 'value')
if v1~=v2
set(handles.pushbutton1,'visible','on')
end
function slider1_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function pushbutton1_Callback(hObject, eventdata, handles)
global v2 v1
v2=v1;
set(handles.pushbutton1,'visible','off');
function movedslider_WindowKeyPressFcn(hObject, eventdata, handles)
keyPressed = eventdata.Key;
if strcmpi(keyPressed,'x')
uicontrol(handles.pushbutton1);
pushbutton1_Callback(handles.pushbutton1,[],handles);
end
What do I have to change, in order to have the pushbutton1 effect while pushing 'x' on the keyboard?
Thanks! Jan
  댓글 수: 1
Jan
Jan 2016년 2월 8일
Please format your question properly. See the "? Help" button to learn how to post text and code in a readable form. Thanks.

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

채택된 답변

Geoff Hayes
Geoff Hayes 2016년 2월 10일
Jan Meyer - make sure that the WindowKeyPressFcn property of the figure has been assigned correctly for your GUI. Using GUIDE, launch the Property Inspector for the figure, and verify that the WindowKeyPressFcn property is set to
@(hObject,eventdata)movedslider('figure1_WindowKeyPressFcn',hObject,eventdata,guidata(hObject))
where the callback (in your code) will then be
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata structure with the following fields (see FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
keyPressed = eventdata.Key;
if strcmpi(keyPressed,'x')
uicontrol(handles.pushbutton1);
pushbutton1_Callback(handles.pushbutton1,[],handles);
end
This is slightly different from what you have where the WindowKeyPressFcn is prefixed with the name of the GUI. With the above, I was able to get the pushbutton callback to fire when I pressed the x key.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Properties에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by