Clear Filters
Clear Filters

Getting arrow key presses in code without any figures

23 views (last 30 days)
Is there a way to get key presses, including arrow keys, in an m script which does not produce or need figures?
I have browsed through mulltple entries from the MATLAB community pages but all of the m files involve figures. I do not want or need graphical input, just a user interaction with the keyboard. I do not want to use simulink. Just a plain m file.
Please give a detailed answer, I am unfamiliar with callbacks and find them very confusing. If this does not require a callback (after all no figure is involved), so much the better.
thanks

Accepted Answer

Zinea
Zinea on 13 May 2024
Edited: Zinea on 13 May 2024
Capturing arrow keys directly in a MATLAB script without using a figure is beyond the capabilities provided by MATLAB’s standard console input functions. This detection of keys can be achieved in two ways, as given below:
  1. ‘’KeyPressFcn” - Key-press callback can access specific information about the user’s interaction with the keyboard. You can refer to this link for more information: https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-KeyPressFcn
  2. Hidden/unobtrusive figure window: The figure window can be made very small and positioned off-screen. However, completely hiding the figure (e.g., setting ‘Visible’, ‘off’) usually disables its ability to receive key press events. Here is the script that keeps the figure less obtrusive so that it doesn’t appear on the MATLAB window while capturing the key presses:
function captureArrowKeys
% Create a figure that's barely visible or off-screen
screenSize = get(0, 'ScreenSize'); % Get the screen size
figPosition = [screenSize(3) - 1, screenSize(4) - 1, 1, 1]; % Position off-screen (bottom-right corner)
fig = figure('Name', 'Arrow Key Detector', ...
'NumberTitle', 'off', ...
'MenuBar', 'none', ...
'ToolBar', 'none', ...
'Position', figPosition, ... % Position off-screen or as small as possible
'KeyPressFcn', @keyPressedCallback, ...
'WindowStyle', 'modal'); % Make the window modal to keep focus
disp('Press arrow keys. Press ESC to exit. The figure is off-screen but still active.');
% This function is called every time a key is pressed in the figure window
function keyPressedCallback(~, event)
switch event.Key
case 'leftarrow'
disp('Left arrow key pressed.');
case 'rightarrow'
disp('Right arrow key pressed.');
case 'uparrow'
disp('Up arrow key pressed.');
case 'downarrow'
disp('Down arrow key pressed.');
case 'escape'
disp('Escape key pressed. Exiting...');
close(fig);
otherwise
disp(['Other key pressed: ', event.Key]);
end
end
end
Here is a screenshot of the Command Window when the above script is executed. The key presses are correctly detected by the script as shown:
NOTE:
  • Positioning Off-Screen: The figure is positioned off-screen (or as close as possible) based on the screen size. This makes it less obtrusive but still technically visible to the operating system.
  • Modal Window: Setting the figure to be 'modal' helps keep it in focus, which is necessary for capturing key presses.
  • Screen Size Consideration: The script calculates the screen size to position the figure. Depending on your screen resolution and configuration, you might need to adjust the figPosition values.
You may refer to this link for the documentation on creating callbacks for graphics objects: https://www.mathworks.com/help/matlab/creating_plots/create-callbacks-for-graphics-objects.html
Hope it helps!
  1 Comment
John Endler
John Endler on 15 May 2024
Thanks, but that is very disappointing. I had already thought of hiding the required useless window. Waste of CPU. I hope the MATLAB developers will eventually fix this odd problem. I'll just go to PYTHON! (I used to do this in PASCAL).

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 15 May 2024
You can potentially use one of the routines from PsychToolbox; https://www.mathworks.com/matlabcentral/answers/143088-real-time-detect-keypress#answer_285124
The toolbox as a whole would be rather heavy for your needs, but the key detection routines are mex based and could potentially be extracted from the toolbox.
  1 Comment
John Endler
John Endler on 16 May 2024
Moved: Voss on 16 May 2024
Thank you Walter. I'll have a look. I had looked at this toolbox but was reluctant to use it because it uses Java. But if I can use the mex system without java I'll have a go.
thanks,
John

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!