码迷,mamicode.com
首页 > 其他好文 > 详细

Matlab学习---------GUI键盘响应事件的学习

时间:2014-08-26 09:52:15      阅读:2724      评论:0      收藏:0      [点我收藏+]

标签:matlab   键盘响应   currentcharacter   keypressfcn   

键盘响应事件的学习主要知识:

bubuko.com,布布扣

实例:创建一个GUI,添加两个输入框,一个按钮,实现输入数据之后点击按钮进行验证,验证成功之后关闭当前界面打开新的界面:

(1)创建一个新的GUI界面:(添加组件并修改属性)

bubuko.com,布布扣

(2)右键按钮添加点击事件响应函数:

(3)右键界面,添加键盘按键响应函数:

function varargout = gui_key(varargin)
% GUI_KEY MATLAB code for gui_key.fig
%      GUI_KEY, by itself, creates a new GUI_KEY or raises the existing
%      singleton*.
%
%      H = GUI_KEY returns the handle to a new GUI_KEY or the handle to
%      the existing singleton*.
%
%      GUI_KEY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_KEY.M with the given input arguments.
%
%      GUI_KEY('Property','Value',...) creates a new GUI_KEY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_key_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_key_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help gui_key

% Last Modified by GUIDE v2.5 26-Aug-2014 09:03:08

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_key_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_key_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 gui_key is made visible.
function gui_key_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to gui_key (see VARARGIN)

% Choose default command line output for gui_key
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes gui_key wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = gui_key_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;



function psw_Callback(hObject, eventdata, handles)
% hObject    handle to psw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of psw as text
%        str2double(get(hObject,'String')) returns contents of psw as a double


% --- Executes during object creation, after setting all properties.
function psw_CreateFcn(hObject, eventdata, handles)
% hObject    handle to psw (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function name_Callback(hObject, eventdata, handles)
% hObject    handle to name (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of name as text
%        str2double(get(hObject,'String')) returns contents of name as a double


% --- Executes during object creation, after setting all properties.
function name_CreateFcn(hObject, eventdata, handles)
% hObject    handle to name (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in login.
function login_Callback(hObject, eventdata, handles)
% hObject    handle to login (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%按钮点击事件响应
name=get(handles.name,'String');
psw=get(handles.psw,'String');
if strcmp(name,'张三') && strcmp(psw,'123')%字符串比较
    delete(gcf);%关闭当前的窗口
    guide_mouse;%打开新的GUIDE
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(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)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%键盘按下的响应事件
if get(gcf,'CurrentCharacter')==13 %判断按下的是否是回车键
    name=get(handles.name,'String');
    psw=get(handles.psw,'String');
    if strcmp(name,'张三') && strcmp(psw,'123')%字符串比较
        delete(gcf);%关闭当前的窗口
        guide_mouse;%打开新的GUIDE
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
(4)运行测试(程序启动后的结果、输入信息的结果,点击登录或者单击回车的结果)

bubuko.com,布布扣 bubuko.com,布布扣
bubuko.com,布布扣




Matlab学习---------GUI键盘响应事件的学习

标签:matlab   键盘响应   currentcharacter   keypressfcn   

原文地址:http://blog.csdn.net/z1137730824/article/details/38842845

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!