首页 > 解决方案 > 在 Webbots 中访问键盘输入

问题描述

我正在尝试修改控制器文件highway_overtake.py 以访问用户的键盘输入。按照文档操作后,出现相关代码如下:

from Controller import Keyboard
keyboard = Keyboard();
keyboard.enable(50);
....
[other webots controller logic]
while driver.step () != -1
     key = keyboard.getKey( )
     if(key ==Keyboard.CONTROL+ord('M')):
          print 'Key Pressed'

我相信这是逐字遵循文档,并尝试使用各种不同的键输入修改示例。我应该如何进行?

参考文档:https ://www.cyberbotics.com/doc/reference/keyboard

标签: pythonkeyboardwebots

解决方案


我不知道您的代码有什么问题,但是,您可以通过以下方式在 webbots 中从键盘获取输入:

from controller import Keyboard

timestep = int(robot.getBasicTimeStep())

keyboard=Keyboard()
keyboard.enable(timestep)

while robot.step(timestep) != -1:
    key=keyboard.getKey()
    if (key==Keyboard.CONTROL+ord('M')):
        print ('Ctrl+M is pressed')
#here you get print only when you press ctrl and m togather
#in webots ctrl + B is a shortcut which mess the window layouts thats why i changed B to M 
# hope it helps

推荐阅读