How to use the spacedevice module
This demo demonstrates the usage of the SpaceDevice object in the
cgkit.spacedevice module which can be used to access events from a
SpaceMouse or SpaceBall. You can use this module to add support for a
SpaceDevice in your own Python application. The demo simply prints the
events generated from a SpaceDevice to the console.
Note however, that you don't have to do all this if you are using the
cgkit viewer tool. In this case, the tool already does this low level
stuff and you simply have to connect to the appropriate event
(SPACE_MOTION, SPACE_BUTTON_DOWN, SPACE_BUTTON_UP and
SPACE_ZERO).
The demo only runs on Windows and requires pygame v1.7.1 (or higher).
Here is the script:
import sys
import pygame
from pygame.locals import *
from cgkit import spacedevice
def handleSystemEvent(evt):
"""Handle a system event.
evt is a pygame event object that contains a system event. The function
first checks if the event was generated by a SpaceDevice and if it was,
it prints the event data.
"""
global sdev
res, evttype, data = sdev.translateWin32Event(evt.msg, evt.wparam, evt.lparam)
if res!=spacedevice.RetVal.IS_EVENT:
return
if evttype==spacedevice.EventType.MOTION_EVENT:
t,r,period = data
print "Motion: trans:%s rot:%s period:%d"%(t, r, period)
elif evttype==spacedevice.EventType.BUTTON_EVENT:
pressed, released = data
print "Button: pressed:%s released:%s"%(pressed, released)
elif evttype==spacedevice.EventType.ZERO_EVENT:
print "Zero"
if not spacedevice.available():
print "No SpaceDevice functionality available"
sys.exit(1)
passed, failed = pygame.init()
if failed>0:
print "Error initializing pygame"
sys.exit(1)
pygame.display.set_caption("SpaceDevice demo")
srf = pygame.display.set_mode((640,480))
pygame.event.set_allowed(SYSWMEVENT)
sdev = spacedevice.SpaceDevice()
info = pygame.display.get_wm_info()
hwnd = info["window"]
sdev.open("Demo", hwnd)
major, minor, build, versionstr, datestr = sdev.getDriverInfo()
print "Driver info:"
print "------------"
print "%s, v%d.%d.%d, %s\n"%(versionstr, major, minor, build, datestr)
devtyp, numbuttons, numdegrees, canbeep, firmware = sdev.getDeviceInfo()
print "Device info:"
print "------------"
print "Device ID:",sdev.getDeviceID()
print "Type :",devtyp
print "#Buttons :",numbuttons
print "#Degrees :",numdegrees
print "Can beep :",canbeep
print "Firmware :",firmware
print ""
running = True
while running:
events = pygame.event.get()
for evt in events:
if evt.type==QUIT:
running=False
elif evt.type==KEYDOWN and evt.key==27:
running=False
elif evt.type==SYSWMEVENT:
handleSystemEvent(evt)
sdev.close()
Back to the tutorial index