Real time detect keypress

189 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2020-3-24
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-10-10
编辑:Walter Roberson 2021-8-14
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 个评论
John BG
John BG 2017-10-11
Just gave Jan Simon this answer, despite his answer in my opinion more of a comment, for pointing on the right direction
Jan
Jan 2017-10-11
编辑:Jan 2017-10-11
+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.

请先登录,再进行评论。

更多回答(3 个)

John BG
John BG 2017-10-10
编辑:Jan 2017-10-10
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 个评论
Jan
Jan 2017-10-11
编辑:Jan 2017-10-11
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 2017-10-11
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.

请先登录,再进行评论。


Jan
Jan 2014-7-23
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 个评论
Jan
Jan 2014-8-2
编辑:Jan 2017-10-11
@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 2017-10-10
PointerLocation works on OS-X as well.

请先登录,再进行评论。


balandong
balandong 2020-3-23
Can consider this FEX submission KbTimer. It is MEX base thus quite fast

类别

Help CenterFile Exchange 中查找有关 Timing and presenting 2D and 3D stimuli 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by