-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpressed.py
105 lines (80 loc) · 3.18 KB
/
pressed.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from threading import Thread, Timer
class Button:
def __init__(
self, hold_time=0, double_time=0, wait_hold=True, name=None, number=None, **kwds
):
self.hold_time = hold_time
self.double_time = double_time
self.wait_hold = wait_hold
self.name = name
self.number = number
self.__dict__.update(kwds)
self.pressed = False
self.held = False
self.pressed_double = False
if self.hold_time:
self.hold_timer = Thread()
if self.double_time:
self.double_timer = Thread()
def __repr__(self):
return "Button({}, {}, {}, {})".format(
self.hold_time, self.double_time, self.name, self.number
)
def press(self):
if self.pressed: # Some devices send 'down' continually while pressed
return
if not (self.hold_time or self.double_time):
self.press_action(self)
elif self.double_time and self.double_timer.is_alive():
self.double_timer.cancel()
self.pressed_double = True
self.double_action(self)
elif self.hold_time:
# wait_hold means don't do press action if hold time is reached
if not self.wait_hold:
self.press_action(self)
self.hold_timer = Timer(self.hold_time, self.hold)
self.hold_timer.start()
self.pressed = True
def release(self):
if not self.pressed:
return
# Wait, doesn't this cause press action to trigger on both press and release in this case? Yeah, it does. Not sure why I thought this was a good idea...?
# if not (self.hold_time or self.double_time):
# self.press_action
# self.pressed = False
# self.pressed_simultaneous = False
# return
starting_double = self.double_time and not (self.held or self.pressed_double)
if self.hold_time and self.hold_timer.is_alive():
# print('canceling hold timer')
self.hold_timer.cancel()
if not (starting_double or self.pressed_double) and self.wait_hold:
self.press_action(self)
if starting_double:
self.double_timer = Timer(self.double_time, self.press_action)
self.double_timer.start()
self.release_action(
self
) # Only useful when not using any of the fancy stuff, probably, but fire on every release for now
self.held = False
self.pressed = False
self.pressed_double = False
def hold(self):
self.held = True
self.hold_action(self)
# Default actions take a self and second self, because they get passed
# self as methods, while assigned functions are not methods and need
# to be passed the button to reference stored state.
def press_action(self, self2):
pass
# print('Pressed: ' + str(self))
def hold_action(self, self2):
pass
# print('Held: ' + str(self))
def double_action(self, self2):
pass
# print('Double pressed: ' + str(self))
def release_action(self, self2):
pass
# print('Released: ' + str(self))