-
Notifications
You must be signed in to change notification settings - Fork 4
/
joypad.py
63 lines (37 loc) · 1.35 KB
/
joypad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# -*- coding: utf-8 -*-
# Authors: TurBoss
# Models: TurBoss
# Joystick
import pyglet
class Joypad:
def __init__(self):
print("-- init Joystick")
pyglet.app.platform_event_loop.start()
self.controller = Controller()
self.joysticks = pyglet.input.get_joysticks()
if self.joysticks:
for i in range(0,len(self.joysticks)):
print("--- found joystick nº%s" % i)
self.joystick = self.joysticks[i]
self.joystick.push_handlers(self.controller)
self.joystick.open()
else:
print("--- no joystick(s) found")
def clean(self):
if self.joysticks:
for i in range(0, len(self.joysticks)):
print("--- close joystick nº %s" % i)
self.joystick = self.joysticks[i]
self.joystick.close()
def update(self, task):
pyglet.app.platform_event_loop.step(0.003)
return task.cont
class Controller:
def on_joybutton_press(self, joystick, button):
print(joystick, button)
def on_joybutton_release(self, joystick, button):
print(joystick, button)
def on_joyaxis_motion(self, joystick, axis, value):
print(joystick, axis, value)
def on_joyhat_motion(self, joystick, hat_x, hat_y):
print(joystick, hat_x, hat_y)