Browse Source

Add support for pressing two buttons on the same side

Teascade 8 months ago
parent
commit
848c9d2f74
2 changed files with 37 additions and 2 deletions
  1. 35
    2
      input_manager.py
  2. 2
    0
      main.py

+ 35
- 2
input_manager.py View File

@@ -1,4 +1,7 @@
1 1
 
2
+import time
3
+
4
+
2 5
 class InputManager:
3 6
 
4 7
     currently_pressed_keys = []
@@ -7,6 +10,13 @@ class InputManager:
7 10
     right_paw_keys = []
8 11
     shifted_keys = {}
9 12
 
13
+    right_paw_repress_time = 0
14
+    left_paw_repress_time = 0
15
+    right_paw_repressed = False
16
+    left_paw_repressed = False
17
+
18
+    repress_time = 0.05
19
+
10 20
     def init(self, linux):
11 21
         if linux:
12 22
             import keylist_linux_fi as keys
@@ -21,12 +31,35 @@ class InputManager:
21 31
         for key in keys.left_paw_keys:
22 32
             self.left_paw_keys.append(key.lower())
23 33
 
34
+    def timer_update(self):
35
+        curr_time = time.time()
36
+        update = False
37
+        if self.right_paw_repressed:
38
+            delta = curr_time - self.right_paw_repress_time
39
+            if delta > self.repress_time:
40
+                self.right_paw_repressed = False
41
+                update = True
42
+        if self.left_paw_repressed:
43
+            delta = curr_time - self.left_paw_repress_time
44
+            if delta > self.repress_time:
45
+                self.left_paw_repressed = False
46
+                update = True
47
+        if update:
48
+            self.on_update(True)
49
+
24 50
     def on_update(self, down):
25 51
         pass
26 52
 
27 53
     def press_key(self, key):
28 54
         key = key.lower()
29 55
         if not key in self.currently_pressed_keys:
56
+            if key in self.left_paw_keys and self.left_keys_pressed():
57
+                self.left_paw_repressed = True
58
+                self.left_paw_repress_time = time.time()
59
+            if key in self.right_paw_keys and self.right_keys_pressed():
60
+                self.right_paw_repressed = True
61
+                self.right_paw_repress_time = time.time()
62
+
30 63
             self.currently_pressed_keys.append(key)
31 64
             self.on_update(True)
32 65
 
@@ -48,11 +81,11 @@ class InputManager:
48 81
         for key in self.currently_pressed_keys:
49 82
             if key in self.left_paw_keys:
50 83
                 pressed = True
51
-        return pressed
84
+        return pressed and not self.left_paw_repressed
52 85
 
53 86
     def right_keys_pressed(self):
54 87
         pressed = False
55 88
         for key in self.currently_pressed_keys:
56 89
             if key in self.right_paw_keys:
57 90
                 pressed = True
58
-        return pressed
91
+        return pressed and not self.right_paw_repressed

+ 2
- 0
main.py View File

@@ -59,6 +59,8 @@ class Main:
59 59
                         hooks_manager.cancel()
60 60
                     sys.exit()
61 61
 
62
+            self.input_manager.timer_update()
63
+
62 64
             font_surface = self.font.render("Pressed keys: " + str(self.input_manager.currently_pressed_keys),
63 65
                                             True, [0, 0, 0], [255, 255, 255])
64 66
             textpos = font_surface.get_rect()