Detect Keyboard Input Matlab - matlab

I have a simple question, although it's harder than it seems; I couldn't find the answer on the interwebs :O
I'm writing a script in Matlab. What I want to do is the following:
When I press the esc key, I want a helpdialogue to pop up, so my script pauses. (So when I press esc, I want to stop the whole script to run so that the car (which im writing the script for) stops driving)
How do I do this? How can I say to Matlab: When I press esc, do this...
Thanks for your time guys!
EDIT: It's no option to implement something which awaits the keypress. Im writing a script for a driving car. It just has to drive around basically, but when I press esc for example, it should stop driving. So the script just has to run, untill I press the esc key; then the script has to pause.

KeyPressFcn is good because it forces you to write event-driven code. Which is generally a good idea! However, if KeyPressFcn doesn't seem right for you, for example if you must keep running in a loop, and you just want to poll whether a key has been pressed, I found this solution buried in the matlab website:
get(gcf,'CurrentCharacter')
Then you could set this property to a blank, and poll it as required.
e.g:
finish=false;
set(gcf,'CurrentCharacter','#'); % set to a dummy character
while ~finish
% do things in loop...
% check for keys
k=get(gcf,'CurrentCharacter');
if k~='#' % has it changed from the dummy character?
set(gcf,'CurrentCharacter','#'); % reset the character
% now process the key as required
if k=='q', finish=true; end
end
end
This worked well for me in 2014b. The downside is that the graphics window needs to be focused to receive the key events.

In a matlab figure you can define a 'KeyPressFcn' that works similar to do what you ask.
If you are in the console you have to work around that matlab is single threaded. Basically you need to halt the program flow to check for key presses.
btw - also when you use 'KeyPressFcn' you will need to make some pauses so that Matlab will check if anything has happened.
btw2 - I should also add during this pauses Matlab will not only read your key presses - but also do some housekeeping such as redrawing its window and stuff.

I frequently ran into similar use cases and typically preferred to react to joystick buttons because of the more convenient interface provided by vrjoystick. However, I recently wrote a library that provides a similar interface for keyboard inputs.
% Pause on ESC
kb = HebiKeyboard();
while true
state = read(kb);
if state.ESC
% PAUSE DRIVING
else
% DRIVE CAR
end
end
It's non-blocking and doesn't require focus on any particular figure.
File Exchange: http://mathworks.com/matlabcentral/fileexchange/61306-hebirobotics-matlabinput
Github: https://github.com/HebiRobotics/MatlabInput

I had a related task once, and i did it with getkey form matlab file exchange.
Basicly you will want to have it listen for ascii 1B (27 decimal)
if getkey does not solve your problem you can still have a look at its code and maybe find the line that will do the trick for you.

Related

Seeing which part of code MatLab is currently running [duplicate]

Is there any way to stop the execution of a matlab program from the debugger like ctrl+c does, but then being able to continue execution (like you can in say c#)?
If not, is there any better way to workaround this other than trying to pre-emptively set break points or dbstop statements in your matlab code?
I would like to be able to interrupt a long running simulation to look at the current state and then continue the simulation.
The two options I'm currently using/considering are
dbstop commands (or (conditional) breakpoints) in the code.
Drawback is that sometimes I don't want to stop the simulation for a few hours, sometimes want to stop after only a few seconds (and I don't necessarily know that in advance) and this doesn't work well with this approach: If I set the break condition to break every 5 minutes, I can't leave matlab running for hours without interaction. If I set the condition to higher, I have to wait too long for the condition to hit.
include code to save the workspace every few seconds/minutes and import the workspace into a second matlab instance. Drawback is that this is a huge hassle and also doesn't necessarily allows me to resume the simulation with the state of the saved workspace then step through the code for a few iterations.
I'm hoping there is a better solution than either of the 2. Thanks for any advice!
Edit: I think what I'm going to do is write simple matlab function that checks an environment variable or a file on disk every iteration and calls dbstop if I set a flag in this file or env. This way I can control when (and if needed which of several) the breakpoint hits from outside matlab by editing the file. Messy, but should work.
This is not necessarily the best way, but you could simulate a file-based signal/interrupt framework. It could be done by checking every once in a while inside the long simulation loop for the existence of a specific file. If it does, you enter interactive mode using the keyboard command.
Something along the lines:
CHECK_EVERY = 10; %# like a polling rate
tic
i = 1; %# loop counter
while true %# long running loop
if rem(i,CHECK_EVERY) == 0 && exist('debug.txt','file')
fprintf('%f seconds since last time.\n', toc)
keyboard
tic
end
%# ... long calculations ...
i = i + 1;
end
You would run your simulation as usual. When you would like to step in the code, simply create a file debug.txt (manually that is), and the execution will halt and you get the prompt:
2.803095 seconds since last time.
K>>
You could then inspect your variables as usual... To continue, simply run return (dont forget to temporarily rename or remove the file). In order to exit, use dbquit
EDIT: Just occurred to me, instead of checking for files, an easier solution would be to use a dummy figure as the flag (as long as the figure is open, keep running).
hFig = figure; drawnow
while true
if ~ishandle(hFig)
keyboard
hFig = figure; drawnow
end
%# ...
pause(0.5)
end
With the release of R2016a, you can just hit the Pause button in the code editor and it will halt right away. The keyboard shortcut is Ctrl+F5.
To pause the execution of a program while it is running, in the Editor tab, click the Pause button. MATLAB pauses execution at the next executable line*.
When your code is running, the Start button will turn into a pause:
Another change with this release is the ability to add/remove breakpoints while running. Previously you couldn't do this, apparently.
You can set a conditional breakpoint in the MATLAB Editor. You can also use DBSTOP to do this. For example, this will set a conditional breakpoint in the file myFcn at line 20 which will stop execution when a loop variable i is a multiple of 500:
dbstop in myFcn.m at 20 if rem(i,500) == 0
Then you can continue execution after you inspect some of your variables.
If saving the workspace to a file is a good proxy for what you want, how about making a simple GUI with a toggle button. In your code, check the state of the button. If the button is depressed, save the state, update a static text to reflect time stamp of last save, unpress the button. Optionally, have a conditional breakpoint based on the state of that toggle button.
Here is an alternate solution using the waitinput File Exchange submission.
The advantage is that you can use it from whithin the current session or in cases where it is troublesome to set up a file. Also it won't leave a file behind on the computer.
The downside is there as well unfortunately, you need to wait for the checking moment before you can terminate and it costs a little bit of time.
for t = 1:10
pause(3) %Doing some calculations
str = waitinput('Enter 1 if you want to stop ',5);
if ~isnan(str)
keyboard; % Enter dbcont if you want to continue from here
end
['moving on, it is now: ' datestr(now)]
pause(3) %Doing some more calculations
end
If you want, you can prevent lines being printed to the screen. In this case you need to enter the input at the time the figure window is open (Look in your start bar on windows).
To summarize, the short code that you can put somewhere like a conditional breakpoint would be:
if ~isnan(waitinput('',5))
keyboard;
end
After certain version (I don't know which one exactly):
Windows: Ctrl + F5
Mac: Command + F5 (I guess)
Unix: I am looking for answer too
After 2016a, there is a button for that on the interface too.

Matlab control+enter key on figure

I want to capture when the user holds down the control key and presses the enter key on a figure window. Note: This is the default keys for "Evaluate Current Section" in the editor.
See example code below:
function test
f = figure;
f.KeyPressFcn = #myKeyPressFcn;
end
function myKeyPressFcn ( hFig, event )
cm = hFig.CurrentModifier();
if ~isempty ( cm )
fprintf ( 'CurrentKey: %s:%s\n', cm{1}, hFig.CurrentKey );
else
fprintf ( 'CurrentKey: %s\n', hFig.CurrentKey );
end
end
To reproduce save the above in an active file in the editor and run the function - the editor needs to be open (this is important!!).
With the figure active press any key -> the selected key is written to the terminal window. However if you hold down Control and press the enter (return) key then this is not captured but instead Matlab attempts to "Evaluate Current Section" (or cell as it used to be called) in the editor (no matter that the figure has the focus). This of course throws as error...
I have contacted Matlab support and this is "expected behaviour!". I can (just about) see why it might be a good idea for demos - but for professional applications that run in Matlab I personally think this "feature" is a bug!!
Notes
When the editor is closed the control+enter is captured in the figure
In deployed applications the control+enter is captured.
If I manually change the Evaluate Current Section shortcut then control+enter is captured.
I have tried a number of things to resolve this but none have worked, for example hiding the editor or setting editor enable state to false (neither of these are acceptable solutions - I was trying to see what I could get to work on a small test case...):
function test
desktop = com.mathworks.mde.desk.MLDesktop.getInstance;
jEditor = desktop.getGroupContainer('Editor').getTopLevelAncestor;
jEditor.setVisible(false);
jEditor.setEnable(false);
f = figure
f.KeyPressFcn = #myKeyPressFcn;
uiwait(f);
jEditor.setVisible(true);
jEditor.setEnable(true);
end
The only way I can get it to work is to close all of the editor files on launching the GUI and then opening them again when the GUI closes (this is not an acceptable solution... - for fairly obvious reasons!!)
I did think about trying to temporarily modify the shortcut key (Preferences-Keyboard-Shortcuts) of the "Evaluate Current Section" -> but haven't worked out a way to do it from the commandline, and then set it back again when finished. If this is fast you could do it when the user presses and releases the control key.
So what am I asking:
If possible I need a solution that will work for anyone anywhere - as if I can get this to work it will be included in a new add-on feature in my Matlab GUI Toolbox. - which is used by people all over the world.
Do you know how to modify the keyboard shortcuts from the commandline - if so how!
Any other suggestions?
My other idea is to change my shortcut to a different key combination - but wheres the fun in that! :) (I will still have the issue if some user somewhere has altered the execute the current cell to my new combination...)
P.S. Thanks for reading this far down!! :)
Why don't you go to the home> Preferences > keyboard > Shortcutand change it there?
you only need to hit Ctrl + Enter in the black box at top of the page for searching the related command, which is here Evaluate Current Section and change it whatever you like.
Please bear in mind you will only need to split out your windows (Undock them). Then, when you click on Ctrl + Enter, it will do whatever you would like.
I hope you find this answer helpful.
You can try the solution from my FEX submission. The KbTimer is motivated by the need to capture keyboard stroke without the need of GUI that designed either by GUIDE or APP DESIGNER. Note that the implementation of function was inspired from the PsychToolbox which is MEX based.

programmatically press an enter key after starting .exe file in Matlab

In Matlab I can start external .exe files that sometime have a pop up that requires an enter key pressed. For example:
system('C:\Program Files (x86)\WinZip\WINZIP32.EXE')
will start Winzip, and then in order to use it you need to pass the "buy now" pop up window by pressing enter.
Now my problem is not with winzip, I only gave it as an example (i use winrar anyway :).
How can I programmatically press an enter key in Matlab in such cases ? (I use win 7)
Can an event listener be used to solve that?
EDIT: The java.awt.Robot class indeed works on explorer, but not on any software that has a pop up window with an OK button that needs to be pressed. I don't know why it doesn't work for that. I gave the winzip example because I assume everybody has winzip/winrar installed in their machine. The actual software I have is different and irrelevant for the question.
There is a way using Java from Matlab, specifically the java.awt.Robot class. See here.
Apparently there are two types of programs, regarding the way they work when called from Matlab with system('...'):
For some programs, Matlab waits until the program has finished before running the next statement. This happens for example with WinRAR (at least in my Windows 7 machine).
For other programs this doesn't happen, and Matlab proceeds with the next statement right after the external program has been started. An example of this type is explorer (the standard Windows file explorer).
Now, it is possible to return execution to Matlab immediately even for type 1 programs: just add & at the end of the string passed to system. This is standard in Linux Bash shell, and it also works in Windows, as discussed here.
So, you would proceed as follows:
robot = java.awt.Robot;
command = '"C:\Program Files (x86)\WinRAR\WinRAR"'; %// external program; full path
system([command ' &']); %// note: ' &' at the end
pause(5) %// allow some time for the external program to start
robot.keyPress (java.awt.event.KeyEvent.VK_ENTER); %// press "enter" key
robot.keyRelease (java.awt.event.KeyEvent.VK_ENTER); %// release "enter" key
If your applications are only on Windows platform, you can try using .net objects.
The SendWait method of the SendKeys objects allows to send virtually any key, or key combination, to the application which has the focus, including the "modifier" keys like Alt, Shift, Ctrl etc ...
The first thing to do is to import the .net library, then the full syntax to send the ENTER key would be:
NET.addAssembly('System.Windows.Forms');
System.Windows.Forms.SendKeys.SendWait('{ENTER}'); %// send the key "ENTER"
If you only do it once the full syntax is OK. If you plan to make extensive use of the command, you can help yourself with an anonymous helper function.
A little example with notepad
%% // import the .NET assembly and define helper function
NET.addAssembly('System.Windows.Forms');
sendkey = #(strkey) System.Windows.Forms.SendKeys.SendWait(strkey) ;
%% // prepare a few things to send to the notepad
str1 = 'Hello World' ;
str2 = 'OMG ... my notepad is alive' ;
file2save = [pwd '\SelfSaveTest.txt'] ;
if exist(file2save,'file')==2 ; delete(file2save) ; end %// this is just in case you run the test multiple times.
%% // go for it
%// write a few things, save the file then close it.
system('notepad &') ; %// Start notepad, without matlab waiting for the return value
sendkey(str1) %// send a full string to the notepad
sendkey('{ENTER}'); %// send the {ENTER} key
sendkey(str2) %// send another full string to the notepad
sendkey('{! 3}'); %// note how you can REPEAT a key send instruction
sendkey('%(FA)'); %// Send key combination to open the "save as..." dialog
pause(1) %// little pause to make sure your hard drive is ready before continuing
sendkey(file2save); %// Send the name (full path) of the file to save to the dialog
sendkey('{ENTER}'); %// validate
pause(3) %// just wait a bit so you can see you file is now saved (check the titlebar of the notepad)
sendkey('%(FX)'); %// Bye bye ... close the Notepad
As explained in the Microsoft documentation the SendKeys class may have some timing issues sometimes so if you want to do complex manipulations (like Tab multiple times to change the button you actually want to press), you may have to introduce a pause in your Matlab calls to SendKeys.
Try without first, but don't forget you are managing a process from another without any synchronization between them, so timing all that can require a bit of trial and error before you get it right, at least for complex sequences (simple one should be straightforward).
In my case above for example I am running all my data from an external hard drive with an ECO function which puts it into standby, so when I called the "save as..." dialog, it takes time for it to display because the HDD has to wake up. If I didn't introduce the pause(1), sometimes the file path would be imcomplete (the first part of the path was send before the dialog had the focus).
Also, do not forget the & character when you execute the external program. All credit to Luis Mendo for highlighting it. (I tend to forget how important it is because I use it by default. I only omit it if I have to specifically wait for a return value from the program, otherwise I let it run on its own)
The special characters have a special code. Here are a few:
Shift +
Control (Ctrl) ^
Alt %
Tab {TAB}
Backspace {BACKSPACE}, {BS}, or {BKSP}
Validation {ENTER} or ~ (a tilde)
Ins Or Insert {INSERT} or {INS}
Delete {DELETE} or {DEL}
Text Navigation {HOME} {END} {PGDN} {PGUP}
Arrow Keys {UP} {RIGHT} {DOWN} {LEFT}
Escape {ESC}
Function Keys {F1} ... {F16}
Print Screen {PRTSC}
Break {BREAK}
The full list from Microsoft can be found here
There is a small javascript utility that simulates keystrokes like this on the Windows javascript interpreter.
Just create a js file with following code:
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
then call it from Matlab after the necessary timeout like this:
system('c:\my\js\file\script.js {Enter}');
Can't test here now, but I think this should work...
If you need to run a console-only program in a context that permits full DOS redirection, you can create a file called, say, CR.txt containing a carriage return and use the '<' notation to pipe the value into the program.
This only works if you can provide all the keyboard input can be recorded in the file. It fails dismally if the input has to vary based on responses.
An alternative is to duplicate the input (and possibly output) stream(s) for the program and then pipe data into and out of the program. This is more robust and can permit dynamic responses to the data, but will also likely require substantial effort to implement a robot user to the application.
Rog-O-Matic is an example of a large application completely controlled by a program that monitors screen output and simulates keyboard input to play an early (1980s) ASCII graphic adventure game.
The other responses will be required for GUI-based applications.
Python package pywinauto can wait any dialog and click buttons automatically. But it's capable for native and some .NET applications only. You may have problems with pressing WPF button (maybe QT button is clickable - not checked), but in such case code like app.DialogTitle.wait('ready').set_focus(); app.DialogTitle.type_keys('{ENTER}') may help. Your case is quite simple and probably some tricks with pywinauto are enough. Is your "app with popup" 64-bit or 32-bit?
wait and wait_not functions have timeout parameter. But if you need precisely listener with potentially infinite loop awaiting popups, good direction is global Windows hooks (pyHook can listen mouse and keybd events, but cannot listen dialog opening). I'll try to find my prototype that can detect new windows. It uses UI Automation API event handlers... and... ops... it requires IronPython. I still don't know how to set UI Automation handler with COM interface from standard CPython.
EDIT (2019, January): new module win32hooks was implemented in pywinauto a while ago. Example of usage is here: examples/hook_and_listen.py.

How to stop a Matlab script but don’t kill the process? [duplicate]

This question already has answers here:
Stop and continue execution from debugger possible?
(6 answers)
Closed 8 years ago.
Strg+C stops and kills a Matlab script (at least sometimes). But is there a way to stop a Matlab, take a look at some variables and continue the calculation?
I am not talking about just setting a breakpoint. I want my script, let’s say run for couple hours come back to it hit some buttons that stops the calculations take a look at some variable and then continue the calculation.
I tried to find out if there is some shortcut key for this – I am quite sure there isn’t.
Now I was thinking about including an if-case that looks if a certain button was pressed by the user. If so there would be a useless k=0 line and a breakpoint on it. And if no one is pressing this button the loop would continue. But this is where my limited Matlab knowledge leaves me. I don’t know if there is a way to ask for a user-button press but don’t wait for a button press like in the function input. Also I just have a running script, I don’t have any GUI.
To drop to the command prompt you need the command keyboard and then type return when you have finished (you don't need a breakpoint). The tricky bit is how to trigger it. There a few options. The easiest is to open a figure window. The following code halts the process when any key is pressed.
keyDownListener=#(src,event) keyboard;
fig = figure;
drawnow
set(fig,'KeyPressFcn',keyDownListener)
for p=1:10000
%do some thing
end
You can modify this to test for a specific key since the keypress is contained within the event struct.
To use no figure gui at all its more of a problem. I'm not aware of a non blocking keyboard input method. A mex file the runs kbhit() in C might do it, but kbhit() is not standard C so it would only work on Windows. An easier option maybe to test for the presence of a file.
for p=1:100000
if exist(fullfile(pwd,'halt.tmp'),'file')
keyboard
end
%do something here
end
This drops to the debug console when halt.tmp is created in the current directory.
Other potential methods could involve using multiple threads to read 'input' (either the Parallel computer toolbox or undocumented Java code), or using http://psychtoolbox.org/ as mentioned by #bdecaf

Matlab pause function that returns the keystroke?

I'd like to use a function in some matlab scripts that would do the following:
Pause until user hits any single key
Return the key stroke hit by the user
The pause function doesn't work, because it doesn't return the user keystroke. The input function doesn't work, because it requires the "enter" key to be hit. The keyboard command dumps you into the full command line window which I don't want (although might be useful for future work). Note, there are ways to do this from figures, i.e. if a figure has focus, you can sniff keystrokes using the 'KeyPressFcn' handler. But I want to do this from scripts. Worst case, I can use the input command, but was hoping for something that required fewer keystrokes from the user.
I found this on MathWorks. Does it do what you want: http://www.mathworks.com/matlabcentral/fileexchange/7465-getkey