Javadoc link

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.

[[service/Keyboard.py]]

 

Example code (from branch develop):
# create services
python = runtime.start("python", "Python")
keyboard = runtime.start("keyboard", "Keyboard")
python.subscribe("keyboard", "publishMouseMoved")
 
# non blocking event example
keyboard.addKeyListener(python);
 
def onKey(key):
    print ("you pressed ", key)
 
def onMouseMoved(mouseEvent):
    print ("x ", mouseEvent.pos.x, " y ", mouseEvent.pos.y)
 
# blocking example
print "here waiting"
keypress = keyboard.readKey()
print "finally you pressed", keypress, "!"

kwatters

10 years 1 month ago

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

10 years 1 month ago

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!!!!"
 
 
Example configuration (from branch develop):
!!org.myrobotlab.service.config.ServiceConfig
listeners: null
peers: null
type: Keyboard