Knowledge about keyboard Event Response:
Instance: Create a GUI and add two input boxes and one button. after entering the data, click the button for verification. After the verification is successful, close the current interface to open a new interface:
(1) Create a New Gui: (add components and modify attributes)
(2) Right-click the button to add the Event Response Function:
(3) Right-click the interface and add the keyboard button response function:
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 ,...) callthe local % function named callback in gui_key.m with the given input arguments. % gui_key ('properties', '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 editgui_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}); endif 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_keyhandles.output = hobject; % update handles structureguidata (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 structurevarargout {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 '); endfunction 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) % % click event Response name = get (handles. name, 'string'); psw = get (handles. psw, 'string'); If strcmp (name, 'zhang san') & strcmp (psw, '123') % string comparison Delete (GCF ); % Close the current window guide_mouse; % open new guideend % % % --- 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) % % press the keyboard if get (GCF, 'currentcharacter ') = 13% judge whether the press is the Enter key name = get (handles. name, 'string'); psw = get (handles. psw, 'string'); If strcmp (name, 'zhang san') & strcmp (psw, '123') % string comparison Delete (GCF ); % Close the current window guide_mouse; % open new guide endend % % %
(4) run the test (the results after the program is started, the results of input information, click Login or click Enter)
Matlab learning --------- GUI keyboard Event Response Learning