- Adding a 'keyPressCallback' as a 'KeyPressFcn' in the figure.
- Defining a button using 'uicontrol' with a 'Callback' function. This step enables a mouse click to also be registered as a valid click. User can remove this 'Callback' if only keypad click is required to be considered.
- 'keyPressCallback' registers the keypad input and checks if it is from a numeric keypad key, then further calls the 'buttonCallback'.
Can't control a GUI pushbutton by numeric keypad.
    3 views (last 30 days)
  
       Show older comments
    
Hi,
I have a pushbutton added in my MATLAB GUI.  I want it to be pressed when using a numeric keypad.
It works when using a keyboard, but I need it to work when using a numeric keypad.
Can anyone help?
function varargout = gui(varargin)
% GUI MATLAB code for gui.fig
%      GUI, by itself, creates a new GUI or raises the existing
%      singleton*.
%
%      H = GUI returns the handle to a new GUI or the handle to
%      the existing singleton*.
%
%      GUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI.M with the given input arguments.
%
%      GUI('Property','Value',...) creates a new GUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before gui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to gui_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
% Last Modified by GUIDE v2.5 25-Feb-2019 22:11:28
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @gui_OpeningFcn, ...
                   'gui_OutputFcn',  @gui_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 is made visible.
function gui_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 (see VARARGIN)
% Choose default command line output for gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = gui_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)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
NET.addAssembly('System.Speech');
obj = System.Speech.Synthesis.SpeechSynthesizer;
obj.Volume = 100;
Speak(obj, 'ten');
% --- Executes on key press with focus on figure1 or any of its controls.
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  structure with the following fields (see MATLAB.UI.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;
switch eventdata.Key
    case '1'
          pushbutton1_Callback(handles.pushbutton1,[],handles);
 end
0 Comments
Answers (1)
  Rahul
      
 on 10 Mar 2025
        In order to achieve the desired functionality of controlling a GUI Pushbutton with a numeric keypad keypress, consider usinhg a keyPressCallback function which is assigned to the 'KeyPressFcn' of the figure/GUI. Here is an example:
function myGui
    hFig = figure('Name', 'My GUI', 'NumberTitle', 'off', 'KeyPressFcn', @keyPressCallback);
    hButton = uicontrol('Style', 'pushbutton', 'String', 'Press Me',
                        'Callback', @buttonCallback);
    % Callback function for key press
    function keyPressCallback(~, event)
        % Check if the pressed key is from the numeric keypad
        if isnumeric(str2double(event.Key))
            buttonCallback(hButton, []);
        end
    end
    % Callback function for the pushbutton
    function buttonCallback(~, ~)
        disp('Button Pressed!');
    end
end
myGui
I used the following steps:
The following MATLAB Answers can be referred:
The following MathWorks documentations can be referred to know more:
'KeyPressFCN': https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-KeyPressFcn
Thanks.
0 Comments
See Also
Categories
				Find more on Graphics Object Properties in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
