Keyboard

javadoc

The Keyboard Service allows keyboard input to be sent to other services.  At the moment the keyboard events are retrieved from a gui element, so the keyboard control must have focus.

Important update:

From version 1.0.1802 the Keyboard service will listen to all keyboard events even if it doesn't have focus.

 

This small program shows how to get keyboard input into a Python service.


#file : service/Keyboard.py edit raw
# create services
python = Runtime.start("python", "Python")
keyboard = Runtime.start("keyboard", "Keyboard")

# non blocking event example
keyboard.addKeyListener(python);

def onKey(key):
    print "you pressed ", key

# blocking example
print "here waiting"
keypress = keyboard.readKey()
print "finally you pressed", keypress, "!"

 

Examples:


#file : service/Keyboard.py edit raw
# create services
python = Runtime.start("python", "Python")
keyboard = Runtime.start("keyboard", "Keyboard")

# non blocking event example
keyboard.addKeyListener(python);

def onKey(key):
    print "you pressed ", key

# blocking example
print "here waiting"
keypress = keyboard.readKey()
print "finally you pressed", keypress, "!"

kwatters's picture

Wait for enter from a keyboard

Here's a python script that will start a keyboard service.. attach a callback for the "Enter" key.
it creates a loop in the waitForEnter  to wait on the global variable "keyboardInput" to be set. then it breaks the loop and continues.   You'll ahve to be on the keyboard service page to press enter o/w it won't get picked up in the gui.
 
 
keyboardInput = None
keyboard = Runtime.start("keyboard", "Keyboard")
keyboard.addCommand("Enter", "python", "onKey", "Enter")
 
def onKey(cmd):
  global keyboardInput
  keyboardInput = cmd
  print 'python data is', cmd
 
def waitForEnter():
  global keyboardInput
  while True:
    if keyboardInput is None:
      continue
    else:
      keyboardInput = None
      break
 
print "Do something"
waitForEnter()
print "Ok. you've done something. now. do something else"
waitForEnter()
print "yay. you did something..."
kmcgerald's picture

Here is what I went with for

Here is what I went with for my servo calibration script.

 

keyboard = Runtime.start("keyboard", "Keyboard") # For getting user confirmation
 
def waitForInput():
  print "Press the C key to continue\n"
  keypress = keyboard.readKey()
  while keypress != 'C': #loop until space
    keypress = keyboard.readKey()
 
#here we'll print some stuff and then call the waitForInput function
print "On your mark ... Get set ..."
waitForInput()
print "GO!!!!"