- check to see if a key is currently pressed? http://psychtoolbox.org/docs/KbCheck
- check to see if a character is currently ready?
- check how many characters are currently available? http://psychtoolbox.org/docs/CharAvail
- read the current keypress if there is one?
- read the current character if there is one?
- wait for a keypress to be available? http://psychtoolbox.org/docs/KbWait
- wait for a character to be available? http://psychtoolbox.org/docs/GetChar
- check whether one particular key is pressed? (relatively fast) -- http://psychtoolbox.org/docs/RestrictKeysForKbCheck and then KbCheck
- 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
- allow characters to be collected in the background while you are doing other things? http://psychtoolbox.org/docs/GetChar
Real time detect keypress
265 views (last 30 days)
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.
Walter Roberson on 10 Oct 2017
Edited: Walter Roberson on 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:
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).
More Answers (3)
John BG on 10 Oct 2017
Edited: Jan on 10 Oct 2017
this is John BG firstname.lastname@example.org
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],...
% 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
selection = questdlg('Close This Figure?','Close Request Function','Yes','No','Yes');
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]
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
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
Jan on 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.