我有一个 while 循环,无限循环,我想在按下键盘键时停止它。
伪代码:
While(1)
do stuff;
listening for key;
if key is pressed
break;
end
end
函数 waitforbuttonpress
让我按下键,所以运气不好。
我在网上找不到任何选项。
最佳答案
好吧,我知道这有点晚了,但经过长时间的寻找,我找到了解决方案。当图形窗口获得焦点时,您可以这样做:
set(gcf,'currentchar',' ') % set a dummy character
while get(gcf,'currentchar')==' ' % which gets changed when key is pressed
do_stuff()
end
当焦点位于图形中时按下某个键时,它会设置属性“currentchar”。在 R2010b 中测试。
关于matlab - 在 Matlab 中按下一个键停止无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23788722/