필터 지우기
필터 지우기

How to find value of modifier with keypressfcn?

조회 수: 9 (최근 30일)
Lyn
Lyn 2015년 1월 7일
댓글: Geoff Hayes 2015년 1월 9일
Hi, I'm trying to make a simple gui where 'tab' increases variable 'a' and 'shift+tab' decreases it.
Unfortunately, my code can't seem to pick up the shift modifier being pressed?
function varargout = modt(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @modt_OpeningFcn, ...
'gui_OutputFcn', @modt_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
% End initialization code - DO NOT EDIT
function modt_OpeningFcn(hObject, eventdata, handles, varargin)
handles.a = 0;
guidata(hObject, handles);
function varargout = modt_OutputFcn(hObject, eventdata, handles)
function figure1_KeyPressFcn(hObject, eventdata, handles)
handles = guidata(hObject);
if strcmp(eventdata.Key,'tab')
handles.a = handles.a + 1
elseif strcmp(eventdata.Modifier{:},'shift') && strcmp(eventdata.Key,'tab')
handles.a = handles.a - 1
end
guidata(hObject,handles)
Thanks for the help!

채택된 답변

Geoff Hayes
Geoff Hayes 2015년 1월 8일
Lyn - look closely at your if/elseif
if strcmp(eventdata.Key,'tab')
handles.a = handles.a + 1
elseif strcmp(eventdata.Modifier{:},'shift') && strcmp(eventdata.Key,'tab')
handles.a = handles.a - 1
end
For your elseif there are two conditions:
strcmp(eventdata.Modifier{:},'shift') && strcmp(eventdata.Key,'tab')
So the code will decrement a by one if both conditions are true. BUT, the second condition is the (only) condition for the if statement! So your elseif will never evaluate because the if will always take precedence.
Try the following instead
if strcmp(eventdata.Key,'tab')
if isempty(eventdata.Modifier)
handles.a = handles.a + 1
elseif strcmp(eventdata.Modifier{:},'shift')
handles.a = handles.a - 1
end
end
Try the above and see what happens! Note that you can remove the line
handles = guidata(hObject);
and just use the handles structure that is passed as the third input to this function.
  댓글 수: 2
Lyn
Lyn 2015년 1월 8일
Oh gosh you're right.. -smacks forehead- thank you very much!
Geoff Hayes
Geoff Hayes 2015년 1월 9일
Glad that I was able to help, Lyn!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by