2014-11-22 61 views
-1

我有一个gui,当我点​​击它们时工作正常,但不是点击我想通过在键盘上按一个键(如uparrow)来激活它们。我确实在第一个按钮(C3)上创建了一个keypressfnc。但我不知道该怎么办now.Here是我的GUI代码如何将键分配给Matlab上的按钮?

function varargout = untitled(varargin) 

gui_Singleton = 1; 
gui_State = struct('gui_Name',  mfilename, ... 
        'gui_Singleton', gui_Singleton, ... 
        'gui_OpeningFcn', @untitled_OpeningFcn, ... 
        'gui_OutputFcn', @untitled_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 


% --- Executes just before untitled is made visible. 
function untitled_OpeningFcn(hObject, eventdata, handles, varargin) 

handles.output = hObject; 


guidata(hObject, handles); 




function varargout = untitled_OutputFcn(hObject, eventdata, handles) 
% varargout cell array for returning output args (see VARARGOUT); 
% hObject handle to figure 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 

% Get default command line output from handles structure 
varargout{1} = handles.output; 


% --- Executes on button press in pushbutton1. 
function pushbutton1_Callback(hObject, eventdata, handles) 
sr = 16000; 
T = 4; % seconds duration 
t = 0:(1/sr):T; 
n = 20; 
f = ((2^(1/12))^(n-49))*440; 
a = linspace(0,1,numel(t)); 
y = exp(-a*4).*sin(2*pi*f*t); 
sound(y, sr); 
plot(t, y); 


% --- Executes on button press in pushbutton2. 
function pushbutton2_Callback(hObject, eventdata, handles) 
sr = 16000; 
T = 4; % seconds duration 
t = 0:(1/sr):T; 
n = 40; 
f = ((2^(1/12))^(n-49))*440; 
a = linspace(0,1,numel(t)); 
y = exp(-a*4).*sin(2*pi*f*t); 
sound(y, sr); 
plot(t, y); 

% --- Executes on button press in pushbutton3. 
function pushbutton3_Callback(hObject, eventdata, handles) 
% hObject handle to pushbutton3 (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 


% --- Executes on button press in pushbutton4. 
function pushbutton4_Callback(hObject, eventdata, handles) 
% hObject handle to pushbutton4 (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 


% --- Executes on button press in pushbutton5. 
function pushbutton5_Callback(hObject, eventdata, handles) 
% hObject handle to pushbutton5 (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 


% --- Executes on button press in pushbutton6. 
function pushbutton6_Callback(hObject, eventdata, handles) 
% hObject handle to pushbutton6 (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 


% --- Executes on button press in pushbutton7. 
function pushbutton7_Callback(hObject, eventdata, handles) 
% hObject handle to pushbutton7 (see GCBO) 
% eventdata reserved - to be defined in a future version of MATLAB 
% handles structure with handles and user data (see GUIDATA) 


% --- Executes on key press with focus on pushbutton1 and none of its controls 


% --- Executes on key press with focus on pushbutton1 and none of its controls. 


% --- Executes on key press with focus on pushbutton1 and none of its controls. 
function pushbutton1_KeyPressFcn(hObject, eventdata, handles) 
% hObject handle to pushbutton1 (see GCBO) 
% eventdata structure with the following fields (see UICONTROL) 
% 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) 

回答

1

KeyPressFcn被称为将是按下此键时,属于对象焦点之一。这可能是被点击的最后一个对象。在这里,您将KeyPressFcn设置为按钮的属性,但只有在按钮是焦点对象时才会调用此按钮。这也包括数字窗口。此示例显示如何为按钮和图形实现按键事件,其中两个值中的KeyPressFcn值都设置为相同的函数。

function KeyPressExample 

fig = figure(); 
set(fig, 'KeyPressFcn', @ChangeColorKeyPress, 'Tag', 'MainGUI'); 

button = uicontrol(fig, 'Units', 'normalized', 'Position', [.4 .4 .2 .2], ... 
    'String', 'Change Color', ... 
    'CallBack', @ChangeColorButtonPress, ... 
    'KeyPressFcn', @ChangeColorKeyPress); 

    function ChangeColorButtonPress(hobj, EventData, handles) 

     set(findobj('Tag', 'MainGUI'), 'Color', rand(3, 1)); 

    end 

    function ChangeColorKeyPress(hobj, EventData, handles) 

     if strcmp(EventData.Key, 'uparrow') 

      set(findobj('Tag', 'MainGUI'), 'Color', rand(3,1)); 

     end 

    end 

end 
+0

我是一个很初级的MATLAB student.I只是希望我的功能运行时我点击uparrow.i无法理解你的例子对不起。我没有(button = uicontrol())例如 – Wardruna 2014-11-23 13:04:11

+0

我给出的例子是一个使用KeyPressFcn属性执行功能的编程GUI。按下向上箭头时,图形背景将会改变颜色。要在GUIDE中执行相同的操作,您必须将KeyPressFcn添加到您希望执行KeyPressFcn的任何时候可能关注的对象。这基本上意味着将它添加到GUI中的每个对象,包括数字窗口本身。现在,您只需将它添加到您的按钮中,这样KeyPressFcn将仅在点击按钮后点击向上箭头键才会执行。 – Staus 2014-11-23 23:21:49

+1

因此,在GUIDE中执行起来要困难得多,因为编程方式更容易。我现在更改了一下代码,现在我的函数可以工作了。 – Wardruna 2014-11-24 08:32:47

0

您还可以结合按键和使用try/catch语句点​​击的回调一个回调:

function KeyPressExample 

fig = figure(); 
set(fig, 'KeyPressFcn', @ChangeColorButtonPress,'Tag', 'MainGUI'); 

button = uicontrol(fig, 'Units', 'normalized', 'Position', [.4 .4 .2 .2], ... 
'String', 'Change Color', ... 
'CallBack', @ChangeColorButtonPress, ... 
'KeyPressFcn', @ChangeColorButtonPress); 

function ChangeColorButtonPress(varargin) 
    EventData=varargin{2} 
    try 
     if strcmp(EventData.Key, 'uparrow') 
     set(findobj('Tag', 'MainGUI'), 'Color', rand(3,1)); 
     end 
    catch 
     set(findobj('Tag', 'MainGUI'), 'Color', rand(3, 1)); 
    end  
end 
end 
相关问题