How to make MATLAB detect keyboard stroke?
583 ビュー (過去 30 日間)
古いコメントを表示
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!
0 件のコメント
採用された回答
Nirav Sharda
2017 年 4 月 18 日
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
k = waitforbuttonpress;
% 28 leftarrow
% 29 rightarrow
% 30 uparrow
% 31 downarrow
value = double(get(gcf,'CurrentCharacter'))
I hope this helps.
6 件のコメント
Caden Scharpf
2020 年 4 月 21 日
The numbers for the keys are their corresponding ASCII Charactershttps://www.petefreitag.com/cheatsheets/ascii-codes/
その他の回答 (2 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!