SmartBody : Creating a mouse and keyboard interface with sbgui

A mouse and keyboard interface can be created with sbgui by extending the SBInterfaceListener interface as follows:

 

 DescriptionUsage
onStart()Called when the simulation starts.

onStart()

onMouseClick()Called when a mouse button is pressed.

onMouseClick(x, y, button)

where x and y are the 2D positions of the mouse location at the time of the click, and button is the number of the button that is pressed (0 = left, 1 = middle, 2 = right)

onMouseMove()Called when the mouse is moved.

onMouseClick(x, y)

where x and y are the 2D positions of the mouse location

onMouseRelease()Called when a mouse button is released.

onMouseRelease(x, y, button)

where x and y are the 2D positions of the mouse location at the time of the release, and button is the number of the button that is pressed (0 = left, 1 = middle, 2 = right)

onMouseDrag()Called when the mouse is moved and a mouse key is pressed down.

onMouseDrag(x, y)

where x and y are the 2D positions of the mouse location during the drag

onKeyboardPress()Called when a key on the keyboard is pressed.

onKeyboardPress(c)

where c is the character pressed

onKeyboardRelease()Called when a key on the keyboard is released.

onKeyboardRelease(c)

where c is the character released

onEnd()Called when the simulation ends.onEnd()

to add the controller, it needs to be instantiated, then added to the GUI interface manager as in the following example:

class Controller (GUIInterface.SBInterfaceListener):
	# pressed key
	def onMouseClick(self, x, y, button):
		print "Mouse clicked!"
 
	def onMouseMove(self, x, y):
		print "Mouse moved!"
 
	def onMouseDrag(self, x, y):
		print "Mouse dragged!"

	# released key
	def onMouseRelease(self,  x, y, button):
		print "Mouse released!"
 
	# pressed key
	def onKeyboardPress(self, c):
		print "Key pressed!"
	# released key
	def onKeyboardRelease(self, c):
		print "Key released!"

# create an instance of the interface
controller = Controller()
# get the interface manager
manager = GUIInterface.getInterfaceManager()
# add the interface to the interface manager
manager.addInterfaceListener(controller)
# if needed, remove the interface
#manager.removeInterfaceListener(controller)