programming-bongocat/input_manager.py

92 lines
2.7 KiB
Python

import time
class InputManager:
currently_pressed_keys = []
left_paw_keys = []
right_paw_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):
if linux:
import keylist_linux_fi as keys
else:
import keylist_win_fi as keys
self.shifted_keys = keys.shifted_keys
for key in keys.right_paw_keys:
self.right_paw_keys.append(key.lower())
for key in keys.left_paw_keys:
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):
pass
def press_key(self, key):
key = key.lower()
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.on_update(True)
def release_key(self, key):
key = key.lower()
if key in self.currently_pressed_keys:
self.currently_pressed_keys.remove(key)
self.on_update(False)
shifted = self.shifted_keys.get(key)
if shifted != None:
shifted = shifted.lower()
if shifted in self.currently_pressed_keys:
self.currently_pressed_keys.remove(shifted)
self.on_update(False)
def left_keys_pressed(self):
pressed = False
for key in self.currently_pressed_keys:
if key in self.left_paw_keys:
pressed = True
return pressed and not self.left_paw_repressed
def right_keys_pressed(self):
pressed = False
for key in self.currently_pressed_keys:
if key in self.right_paw_keys:
pressed = True
return pressed and not self.right_paw_repressed