Real time detect keypress

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 comentario

Walter Roberson
Walter Roberson el 24 de Mzo. de 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.

Iniciar sesión para comentar.

 Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Oct. de 2017
Editada: Walter Roberson el 14 de Ag. de 2021

2 votos

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? http://psychtoolbox.org/docs/KbCheck
  2. check to see if a character is currently ready?
  3. check how many characters are currently available? http://psychtoolbox.org/docs/CharAvail
  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? http://psychtoolbox.org/docs/KbWait
  7. wait for a character to be available? http://psychtoolbox.org/docs/GetChar
  8. check whether one particular key is pressed? (relatively fast) -- http://psychtoolbox.org/docs/RestrictKeysForKbCheck 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?) http://psychtoolbox.org/docs/KbPressWait
  10. allow characters to be collected in the background while you are doing other things? http://psychtoolbox.org/docs/GetChar
The documentation at http://psychtoolbox.org/docs/ListenChar 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 comentarios

John BG
John BG el 11 de Oct. de 2017
Just gave Jan Simon this answer, despite his answer in my opinion more of a comment, for pointing on the right direction
Jan
Jan el 11 de Oct. de 2017
Editada: Jan el 11 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (3)

John BG
John BG el 10 de Oct. de 2017
Editada: Jan el 10 de Oct. de 2017

4 votos

Hi Michael
this is John BG jgb2012@sky.com
1.
Let me show you how to use figure to capture keystrokes
clear all;close all;clc
a='0';b='0'
S.fh = figure( 'units','pixels',...
'position',[500 500 200 260],...
'menubar','none','name','move_fig',...
'numbertitle','off','resize','off',...
'keypressfcn',@f_capturekeystroke,...
'CloseRequestFcn',@f_closecq);
S.tx = uicontrol('style','text',...
'units','pixels',...
'position',[60 120 80 20],...
'fontweight','bold');
guidata(S.fh,S)
function f_capturekeystroke(H,E)
% capturing and logging keystrokes
S2 = guidata(H);
P = get(S2.fh,'position');
set(S2.tx,'string',E.Key)
assignin('base','a',E.Key) % passing 1 keystroke to workspace variable
evalin('base','b=[b a]') % accumulating to catch combinations like ctrl+S
end
function f_closecq(src,callbackdata)
selection = questdlg('Close This Figure?','Close Request Function','Yes','No','Yes');
switch selection
case 'Yes'
S.fh.WindowSyle='normal'
delete(gcf)
case 'No'
return
end
end
.
2.
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.
.
3.
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'))
4.
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 comentarios

Jan
Jan el 10 de Oct. de 2017
@John BG:
1. You have been informed several times by editors and admins, that posting copy righted code without providing the required license information is not accepted in the forum. You are the only person in this forum who does not respect this.
2. The point of the question was, how to get the mouse position without using a figure: "avoid using a GUI".
John BG
John BG el 11 de Oct. de 2017
Editada: John BG el 11 de Oct. de 2017
Walter
It's for clarity of reading, and to save time to the readers.
The reproduction takes place within the MATLAB forum anyway.
People find it helpful and useful.
I always include a concise reference to the author(s) including a link.
If you focus only on the 'without GUI' why don't you tell Michael 'go and learn Java'?
As Jan Simon points out, it can be done with the minimal expression of a figure, as if having none, to precisely avoid peak and shovel mining into Java libraries
John BG
John BG el 11 de Oct. de 2017
Editada: John BG el 11 de Oct. de 2017
and for the record, I have nothing but respect to the people asking questions and this forum, by giving as complete as possible, to the extend of my knowledge, answers, and avoiding posting comments as answers, as well as not withholding useful information in order to solve questions.
Jan
Jan el 11 de Oct. de 2017
Editada: Jan el 11 de Oct. de 2017
John BG, neither "clarity of reading" nor "saving time" matters here. Posting code without its license file violates the license conditions of the FEX submissions and the terms of use of the forum. Read Ned Gulley's official comment. It wastes the time of the editors and admins to clean up your contributions again and again.
The members of the forum know your kind of respect, so there is no need to tell stories.
Walter Roberson
Walter Roberson el 11 de Oct. de 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.

Iniciar sesión para comentar.

Jan
Jan el 23 de Jul. de 2014

2 votos

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 comentarios

Michael
Michael el 24 de Jul. de 2014
Ok. I can get a key value within a call back function, but only if I actually create a GUI using uicontrol. Just calling figure does not make the figure window appear and does not seem to make the figure (active? in focus?). So keystrokes are not responded to.
Michael
Michael el 24 de Jul. de 2014
Actually the mouse position can be captured with get(0,'PointerLocation')
Jan
Jan el 2 de Ag. de 2014
Editada: Jan el 11 de Oct. de 2017
@Michael: The PointerLocation works under Windows only, as far as I remember [EDITED: See Walter's comment: this concerned older Matlab versions only]. Actually figure creates a figure and set the focus to it.
Walter Roberson
Walter Roberson el 10 de Oct. de 2017
PointerLocation works on OS-X as well.

Iniciar sesión para comentar.

balandong
balandong el 23 de Mzo. de 2020

0 votos

Can consider this FEX submission KbTimer. It is MEX base thus quite fast

Categorías

Preguntada:

el 23 de Jul. de 2014

Editada:

el 14 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by