pythonkeyboardwebots

Accessing Keyboard Input in Webots


I am attempting to modify the controller file highway_overtake.py in order to access keyboard inputs from the user. After following the documentation, the relevant code appears as follows:

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'

I believe this is following the documentation verbatim, and have tried modifying the example with various different key inputs. How should I proceed?

Referenced Documentation: https://www.cyberbotics.com/doc/reference/keyboard


Solution

  • I don't know whats wrong with your code but, here is how you can take input from keyboard in webots:

    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