Getting arrow key presses in code without any figures

조회 수: 43 (최근 30일)
John Endler
John Endler 2024년 5월 12일
이동: Voss 2024년 5월 16일
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

채택된 답변

Zinea
Zinea 2024년 5월 13일
편집: Zinea 2024년 5월 13일
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
John Endler
John Endler 2024년 5월 15일
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).

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2024년 5월 15일
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
John Endler
John Endler 2024년 5월 16일
이동: Voss 2024년 5월 16일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Timing and presenting 2D and 3D stimuli에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by