How to make MATLAB detect keyboard stroke?

575 visualizaciones (últimos 30 días)
M Min
M Min el 16 de Abr. de 2017
Respondida: Amir Azadeh Ranjbar el 19 de Oct. de 2023
Hi,
I want to write a code which detect left and right arrow stroke in keyboard.
I tried to use below kind of code but it shows crosshair and also interrupts the working of other line. (Making crosshair invisible is somewhat important in this case)
[~,~,button]=ginput(1);
switch button
case 28 %left
case 29 %right
end
Is there any better way to do it? Thanks!

Respuesta aceptada

Nirav Sharda
Nirav Sharda el 18 de Abr. de 2017
There are a couple of ways to achieve this. You can use the KeyPressFcn callback on the figure window. Here is an example.
h_fig = figure;
set(h_fig,'KeyPressFcn',@myfun);
The myfun here could have something like this.
function myfun(src,event)
disp(event.Key);
end
The other way to achieve this is to use ' waitforbuttonpress ' function. Here is an example code.
k = waitforbuttonpress;
% 28 leftarrow
% 29 rightarrow
% 30 uparrow
% 31 downarrow
value = double(get(gcf,'CurrentCharacter'))
I hope this helps.
  6 comentarios
Joseph Rucker
Joseph Rucker el 20 de Abr. de 2020
How do you find out what the numbers for the keys are?
Caden Scharpf
Caden Scharpf el 21 de Abr. de 2020
The numbers for the keys are their corresponding ASCII Charactershttps://www.petefreitag.com/cheatsheets/ascii-codes/

Iniciar sesión para comentar.

Más respuestas (2)

balandong
balandong el 23 de Mzo. de 2020
Maybe Im bit late to join the party. But you can consider this FEX submission KbTimer. It is MEX base thus quite fast

Amir Azadeh Ranjbar
Amir Azadeh Ranjbar el 19 de Oct. de 2023

Categorías

Más información sobre GUIDE 앱 마이그레이션하기 en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!