Skip to content

Instantly share code, notes, and snippets.

@Fulgen301
Created November 8, 2020 21:32
Show Gist options
  • Save Fulgen301/68c33baff9c11c657612c006d92730c7 to your computer and use it in GitHub Desktop.
Save Fulgen301/68c33baff9c11c657612c006d92730c7 to your computer and use it in GitHub Desktop.
Keyboard controller mapping for https://the-pigeon-protocol.itch.io/notusa
import keyboard
import pyxinput
class Key(object):
__slots__ = "key", "mapping", "value_pressed", "value_released"
def __init__(self, key, mapping : str, value_pressed, value_released):
self.key = key
self.mapping = mapping
self.value_pressed = value_pressed
self.value_released = value_released
class GeneralButton(Key):
def __init__(self, key, mapping : str):
super().__init__(key, mapping, True, False)
class Button(GeneralButton):
def __init__(self, key, mapping : str):
super().__init__(key, "Btn" + mapping)
class Axis(Key):
def __init__(self, key, mapping : str, opposite : bool):
super().__init__(key, mapping, -32767 if opposite else 32767, 0)
class Emulator(object):
controller = None
hooks = None
def __init__(self, *keys):
self.controller = pyxinput.vController(False)
self.hooks = []
for key in keys:
self.hooks.append(self.press_hook(key))
self.hooks.append(self.release_hook(key))
def __del__(self):
for hook in self.hooks:
keyboard.unhook_key(hook)
def press_hook(self, key : Key):
return keyboard.on_press_key(key.key, lambda event: self.controller.set_value(key.mapping, key.value_pressed), False)
def release_hook(self, key : str):
return keyboard.on_release_key(key.key, lambda event: self.controller.set_value(key.mapping, key.value_released), False)
if __name__ == "__main__":
left = Emulator(Button("a", "X"), Button("v", "A"), Button("d", "B"), Axis("y", "AxisLx", True), Axis("c", "AxisLx", False), Axis("x", "AxisLy", True), Axis("s", "AxisLy", False))
right = Emulator(Button("j", "X"), Button("-", "A"), Button("l", "B"), Axis("m", "AxisLx", True), Axis(".", "AxisLx", False), Axis(",", "AxisLy", True), Axis("k", "AxisLy", False))
keyboard.wait()
@RaMo-teamhellep
Copy link

Hello!
I am one of the developers of Not USA and I am happy to announce that we implemented keyboard controls a few days after the gamejam. But you can not believe how honoured I feel that you took the time to write this! I hope you enjoyed our small game ^-^"
Cheers,
Raff

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment