game controller

http://libSDL.org/ - Simple DirectMedia Layer SDL is a cross-platform multimedia library designed to provide low level access to audio, keyboard, mouse, joystick, 3D hardware via OpenGL, and 2D video framebuffer.

There is a tcl interface - package require mkLibsdl - and some demo scripts joystick(1-5).tcl to get status information from the gamepad or joystick http://mkextensions.sourceforge.net/ .


 # gamepad button to key translation
 bind . <Escape> {exec wish $argv0 &; exit}
 package require mkLibsdl
 package require Tk
 pack [label .l -text "press gamepad buttons\n1 Tab\n2 some text\n3 backspace\n4 backTab" -justify left]
 pack [entry .e0] [entry .e1] -pady 5
 # buttons w/o commands to take focus
 pack \
 [button .b0 -text "Tcl/Tk language"]\
 [button .b1 -text "designed by"]\
 [button .b2 -text "John Ousterhout"] -pady 2
 foreach x [winfo children .] {
   $x configure -highlightthickness 1 -highlightcolor blue -highlightbackground yellow
 }
 
 array set ::jbuttons {}
 array set ::afterid {}
 # key is "device number"."button number"
 trace add variable ::jbuttons(0.0) write {type <Tab>}
 trace add variable ::jbuttons(0.3) write {type <Shift-Tab>}
 trace add variable ::jbuttons(0.1) write {typelist {<Key-T> <Key-c> <Key-l> <Key-/> <Key-T> <Key-k> <Key-space>}}
 trace add variable ::jbuttons(0.2) write {type <Key-BackSpace>}
 proc setstate {} {
   set levent [joystick event peek]
   foreach {x ijoystick stype icontrol x ivalue} $levent break
   switch $stype {
     button {
       set ::jbuttons($ijoystick.$icontrol) $ivalue
     }
     axis {
       set ::aAxes($ijoystick.$icontrol) $ivalue
     }
   }
 }
 proc repeat {ev n2} {
   event generate . $ev
   set ::afterid($n2) [after 200 [list repeat $ev $n2]]
 }
   
 proc type {ev n1 n2 op} {
   set state [lindex [array get $n1 $n2] 1]
   if {$state} {
     repeat $ev $n2
   } else {
     after cancel [lindex [array get ::afterid $n2] 1]
   }
 }
 proc typelist {evlist n1 n2 op} {
   if {[lindex [array get $n1 $n2] 1]} {
     foreach x $evlist {
       event generate . $x
     }
   }
 }
 joystick event eval setstate
 focus -force .e1

See also