Add support for pressing two buttons on the same side

This commit is contained in:
Sofia 2018-12-11 03:14:05 +02:00
parent e4cca123c9
commit 848c9d2f74
2 changed files with 37 additions and 2 deletions

View File

@ -1,4 +1,7 @@
import time
class InputManager: class InputManager:
currently_pressed_keys = [] currently_pressed_keys = []
@ -7,6 +10,13 @@ class InputManager:
right_paw_keys = [] right_paw_keys = []
shifted_keys = {} shifted_keys = {}
right_paw_repress_time = 0
left_paw_repress_time = 0
right_paw_repressed = False
left_paw_repressed = False
repress_time = 0.05
def init(self, linux): def init(self, linux):
if linux: if linux:
import keylist_linux_fi as keys import keylist_linux_fi as keys
@ -21,12 +31,35 @@ class InputManager:
for key in keys.left_paw_keys: for key in keys.left_paw_keys:
self.left_paw_keys.append(key.lower()) self.left_paw_keys.append(key.lower())
def timer_update(self):
curr_time = time.time()
update = False
if self.right_paw_repressed:
delta = curr_time - self.right_paw_repress_time
if delta > self.repress_time:
self.right_paw_repressed = False
update = True
if self.left_paw_repressed:
delta = curr_time - self.left_paw_repress_time
if delta > self.repress_time:
self.left_paw_repressed = False
update = True
if update:
self.on_update(True)
def on_update(self, down): def on_update(self, down):
pass pass
def press_key(self, key): def press_key(self, key):
key = key.lower() key = key.lower()
if not key in self.currently_pressed_keys: if not key in self.currently_pressed_keys:
if key in self.left_paw_keys and self.left_keys_pressed():
self.left_paw_repressed = True
self.left_paw_repress_time = time.time()
if key in self.right_paw_keys and self.right_keys_pressed():
self.right_paw_repressed = True
self.right_paw_repress_time = time.time()
self.currently_pressed_keys.append(key) self.currently_pressed_keys.append(key)
self.on_update(True) self.on_update(True)
@ -48,11 +81,11 @@ class InputManager:
for key in self.currently_pressed_keys: for key in self.currently_pressed_keys:
if key in self.left_paw_keys: if key in self.left_paw_keys:
pressed = True pressed = True
return pressed return pressed and not self.left_paw_repressed
def right_keys_pressed(self): def right_keys_pressed(self):
pressed = False pressed = False
for key in self.currently_pressed_keys: for key in self.currently_pressed_keys:
if key in self.right_paw_keys: if key in self.right_paw_keys:
pressed = True pressed = True
return pressed return pressed and not self.right_paw_repressed

View File

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