Detect Keyboard Input Matlab

Technical Source
2 min readOct 22, 2024

--

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.

NOTE:-

Matlabsolutions.com provide latest MatLab Homework Help,MatLab Assignment Help , Finance Assignment Help for students, engineers and researchers in Multiple Branches like ECE, EEE, CSE, Mechanical, Civil with 100% output.Matlab Code for B.E, B.Tech,M.E,M.Tech, Ph.D. Scholars with 100% privacy guaranteed. Get MATLAB projects with source code for your learning and research.

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

SEE COMPLETE ANSWER CLICK THE LINK

https://www.matlabsolutions.com/resources/detect-keyboard-input-matlab.php

https://matlabarticlesworld.blogspot.com/2024/10/detect-keyboard-input-matlab.html

--

--

Technical Source
Technical Source

Written by Technical Source

Simple! That is me, a simple person. I am passionate about knowledge and reading. That’s why I have decided to write and share a bit of my life and thoughts to.

No responses yet