Real time detect keypress

280 Ansichten (letzte 30 Tage)
Michael am 23 Jul. 2014
Bearbeitet: Walter Roberson am 14 Aug. 2021
I'd like to create a simple program to emulate real time programming but without sensors (so any student with a computer and MATLAB can work with it anywhere). Wanting a simple problem (assignment) I'd like to avoid using a GUI. My thought was to detect a keypress and initiate action based on what key was pressed and whether or not a given different key had been pressed first. Real time programming does not use pauses or anything that waits without monitoring. If this is not possible, maybe I can do something with the mouse pointer location instead? I understand that this can be obtained from the root object. Just creating a figure doesn't work. Maybe because the mouse needs to be in the figure window? Just calling figure doesn't make the figure window show up.
  1 Kommentar
Walter Roberson
Walter Roberson am 24 Mär. 2020
Technically speaking, Real Time programming can include pauses or waiting for events. Real Time programming is about ensuring that actions are taken on time and that reactions are made within a bounded time.
For example it would be perfectly valid to have a Real Time program whose purpose was defined as waiting indefinitely (years even) for an input line to be raised and then to react within 1 millisecond. And it is also perfectly valid to have a Real Time program that has a time budget of only 1 ms to probe keyboard hardware to determine which keys are currently pressed... with it having been carefully defined what to do if more than a certain number are pressed at the same time since transferring the full list of up to 105 keys could exceed the time budget.
Real Time is about having bounded reaction time for defined actions, but those defined actions can involve waiting indefinitely if that is the purpose of the system.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Okt. 2017
Bearbeitet: Walter Roberson am 14 Aug. 2021
PsychToolbox has a series of related routines that are ultimately mex based (so they use operating system features.) The facilities provided there raise questions about exactly what needs to be done:
  1. check to see if a key is currently pressed?
  2. check to see if a character is currently ready?
  3. check how many characters are currently available?
  4. read the current keypress if there is one?
  5. read the current character if there is one?
  6. wait for a keypress to be available?
  7. wait for a character to be available?
  8. check whether one particular key is pressed? (relatively fast) -- and then KbCheck
  9. flush all key presses and characters the user has already entered and wait for a new one ? (can the user drive the program more quickly by pressing keys faster?)
  10. allow characters to be collected in the background while you are doing other things?
The documentation at discusses various platform limitations, and specifically mentions the possibility of running MATLAB with -nojvm, which you would probably be doing if you had no graphics.
Edited on 11-11-2020 to update links (prior links were broken).
  2 Kommentare
Jan am 11 Okt. 2017
Bearbeitet: Jan am 11 Okt. 2017
+1 and accepted: The PsychToolbox contains many powerful tools for tracking the keyboard and the mouse apart from using a figure, which was the point of the question.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (3)

John BG
John BG am 10 Okt. 2017
Bearbeitet: Jan am 10 Okt. 2017
Hi Michael
this is John BG
Let me show you how to use figure to capture keystrokes
clear all;close all;clc
S.fh = figure( 'units','pixels',...
'position',[500 500 200 260],...
S.tx = uicontrol('style','text',...
'position',[60 120 80 20],...
function f_capturekeystroke(H,E)
% capturing and logging keystrokes
S2 = guidata(H);
P = get(S2.fh,'position');
assignin('base','a',E.Key) % passing 1 keystroke to workspace variable
evalin('base','b=[b a]') % accumulating to catch combinations like ctrl+S
function f_closecq(src,callbackdata)
selection = questdlg('Close This Figure?','Close Request Function','Yes','No','Yes');
switch selection
case 'Yes'
case 'No'
Regarding the crux of the query
' .. to detect a keypress and initiate action based on what key was pressed and whether or not a given different key had been pressed first .. '
the variable b contains the log of all keystrokes, sequentially logged, so one can catch combinations like ctrl+S
One can build a 2 characters window and with a switch discern any string that should trigger desired events.
For the mouse capture, a start point for your development could be Rodney Thomson example, one of many examples compiled in a bundle available in the File Exchange to start learning GUI development, but also directly available here
[EDITED, copy righted code removed]
test call
t = linspace(-5,5);
y = sinc(t);
f = figure;
plot(t, y, 'r');
set(f, 'WindowButtonMotionFcn', @(obj, event)cursorLocation(obj, event, 'BottomLeft', ' X: %.3f\n Y: %.3f', 'r'))
Generating a figure is the shortest way to catch keystrokes (and mouse), but if you want to spend time Java, then one can 'lift the bonnet' of the operative system used and catch keystrokes there. I don't know how it works, but I know it's explained here:
Undocumented Secrets of MATLAB - Java Programming
author: Mr Yair Altman
ed: CRC Press
So, Michael
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
  5 Kommentare
Walter Roberson
Walter Roberson am 11 Okt. 2017
John BG, I am not sure why you are referring to me in the above? I did not discuss posting copyright code (not in this Question)
"If you focus only on the 'without GUI' why don't you tell Michael 'go and learn Java'?"
At the moment I am not certain it is possible to include Java in a compiled executable that is restricted to have no graphics.

Melden Sie sich an, um zu kommentieren.

Jan am 23 Jul. 2014
Neither the keyboard not the mouse events can be caught easily without a GUI. Because creating such a GUI can be done by the single command figure, this would be the right way to go.
  4 Kommentare
Walter Roberson
Walter Roberson am 10 Okt. 2017
PointerLocation works on OS-X as well.

Melden Sie sich an, um zu kommentieren.

balandong am 23 Mär. 2020
Can consider this FEX submission KbTimer. It is MEX base thus quite fast

Community Treasure Hunt

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

Start Hunting!

Translated by