input_manager.py 2.7KB

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