check for keyboard press in while loop

59 ビュー (過去 30 日間)
raheem mian
raheem mian 2020 年 3 月 10 日
回答済み: Amir Azadeh Ranjbar 2023 年 10 月 19 日
I'd like to check for a keyboard press and increment a number if the right arrow key is pressed and decrement if the left array key is pressed.
  2 件のコメント
Geoff Hayes
Geoff Hayes 2020 年 3 月 11 日
raheem - please provide some context. Do you have a figure or GUI that you can use to listen for key press events? Or are you doing something else?
raheem mian
raheem mian 2020 年 3 月 11 日
Yeah I have a volume of images that I want to iterate through. I would be using imagesc(vol(:, :, i)) where i is the iterator

サインインしてコメントする。

回答 (2 件)

Geoff Hayes
Geoff Hayes 2020 年 3 月 12 日
raheem - since you have a figure, you could use the WindowKeyPressFcn to listen for a key press event (assuming that the figure has focus). You could then change the contents of the axes given the left or right arrow. Just save the below to a file named MyKeyPressListener.m.
function MyKeyPressListener
hFig = figure;
hAxes = gca;
set(hFig,'WindowKeyPressFcn',@keyPressCallback);
function keyPressCallback(source,eventdata)
% determine the key that was pressed
keyPressed = eventdata.Key;
if strcmpi(keyPressed,'rightarrow')
C = [0 2 4; 8 10 12; 16 18 20];
imagesc(C, 'Parent', hAxes);
elseif strcmpi(keyPressed,'leftarrow')
C = [0 2 4 6; 8 10 12 14; 16 18 20 22];
imagesc(C, 'Parent', hAxes);
end
end
end

Amir Azadeh Ranjbar
Amir Azadeh Ranjbar 2023 年 10 月 19 日

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by