1import pygame, keyboard
2
3class Input:
4
5 input_state = "main"
6
7 record_keys = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
8 "1", "2", "3", "4", "5", "6", "7", "8", "9", "0",
9 "UP", "DOWN", "LEFT", "RIGHT",
10 "SPACE", "CTRL", "SHIFT", "ALT", "RETURN", "ESCAPE", "TAB", "BACKSPACE"]
11
12 def set_input_state(self, state):
13 self.input_state = state
14
15 def check_keys(self):
16 for key in self.record_keys:
17 exec("self.key_" + key + " = " + str(keyboard.is_pressed(key)))
18
19 def __init__(self):
20 self.check_keys()
21
22 self.any_key_pressed = False
23
24 def is_pressed(self, key, input_state=""):
25 if input_state != "":
26 return bool(keyboard.is_pressed(key) and pygame.mouse.get_focused() and input_state == self.input_state)
27 else:
28 return bool(keyboard.is_pressed(key) and pygame.mouse.get_focused())
29
30 def is_just_pressed(self, key, input_state=""):
31 exec("self.check_key = self.key_" + key)
32 if input_state != "":
33 return bool(self.check_key == False and keyboard.is_pressed(key) and input_state == self.input_state)
34 else:
35 return bool(self.check_key == False and keyboard.is_pressed(key))
36
37 def is_any_key_pressed(self):
38 return self.any_key_pressed