如何使用箭头键更改某物的值? [英] How can I change the value of something using the arrow keys?
问题描述
我创建了一个d = 20
值.我希望每次按右箭头键时d
增加10
,而每次按左箭头键时减小10
.因此,如果我按一次右箭头键,则按d = 30
,如果我再次按右箭头键,则按d = 40
.然后,如果我按向左箭头键,则按d = 30
.
I have created a value of d = 20
. I would like d
to increase by 10
every time I press the right arrow key and decrease every time I hit the left arrow key. So If I pressed the right arrow key once then d = 30
, and if I pressed the right arrow key again then d = 40
. And then if I pressed the left arrow key then d = 30
.
我应该使用WindowPressKeyFcn
吗?
推荐答案
d = 20;
f = figure;
k=1;
while k
waitforbuttonpress;
if get(gcf,'CurrentCharacter')==28 % Detection for left key
d=d-10
elseif get(gcf,'CurrentCharacter')==29 % Detection for right key
d=d+10
elseif get(gcf,'CurrentCharacter')==32
k=0; % Stop execution if Spacebar is pressed
end
end
close(f) %Closing the figure window
这将打开一个图形窗口.按 → 或 ← 键分别将值增加或减少10.如果按 空格键 ,执行将停止.
This will open a figure window. Press → or ← keys to increase or decrease the value by 10 respectively. The execution will stop if you press Spacebar .
PS::您的图形窗口应始终位于焦点上.
P.S: Your figure window should always be on focus.
这篇关于如何使用箭头键更改某物的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!