Real time detect keypress
Show older comments
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 Comment
Walter Roberson
on 24 Mar 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.
Accepted Answer
More Answers (3)
Hi Michael
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 Comments
Jan
on 10 Oct 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".
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
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.
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
on 11 Oct 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.
Jan
on 23 Jul 2014
2 votes
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 Comments
Michael
on 24 Jul 2014
Michael
on 24 Jul 2014
Walter Roberson
on 10 Oct 2017
PointerLocation works on OS-X as well.
balandong
on 23 Mar 2020
0 votes
Can consider this FEX submission KbTimer. It is MEX base thus quite fast
Categories
Find more on Timing and presenting 2D and 3D stimuli in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!