From 8250f43f6193efead8ec810224f9d1a90658e65a Mon Sep 17 00:00:00 2001 From: teascade Date: Tue, 11 Dec 2018 00:44:04 +0200 Subject: [PATCH] Added dynamic support for importing and using pyxhook --- README.md | 2 -- main.py | 25 ++++++++++++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 75de554..dd27916 100644 --- a/README.md +++ b/README.md @@ -22,8 +22,6 @@ or **on Linux** run After that install pygame with `python -m pip install pygame` -**If you are working on linux** you need to replace `pyHook` with `pyxhook` on line 5 of `main.py` - And then you should be able to open bongocat with `python main.py` diff --git a/main.py b/main.py index 418f08f..ce2bfcc 100644 --- a/main.py +++ b/main.py @@ -2,7 +2,6 @@ import pygame import sys -from pyHook import HookManager from input_manager import InputManager from bongocat import Bongocat @@ -17,15 +16,17 @@ class Main: bongocat = Bongocat() clackmanager = Clackmanager() - debug = False + debug = True sounds = False - def init(self): + def init(self, linux): hooks_manager = HookManager() hooks_manager.KeyDown = self.OnKeyDown hooks_manager.KeyUp = self.OnKeyUp hooks_manager.HookKeyboard() + if linux: + hooks_manager.start() pygame.init() pygame.mixer.init(buffer=1024) @@ -40,7 +41,7 @@ class Main: screen = pygame.display.set_mode(size) - self.font = pygame.font.SysFont('Iosevka', 24) + self.font = pygame.font.SysFont('Iosevka', 15) self.bongocat.load() self.clackmanager.load() @@ -49,6 +50,8 @@ class Main: while True: for event in pygame.event.get(): if event.type == pygame.QUIT: + if linux: + hooks_manager.cancel() sys.exit() font_surface = self.font.render("Pressed keys: " + str(self.input_manager.currently_pressed_keys), @@ -62,8 +65,12 @@ class Main: pygame.display.flip() + if linux: + hooks_manager.cancel() + def OnKeyDown(self, event): self.input_manager.press_key(event.Key) + print event.Message return True def OnKeyUp(self, event): @@ -79,5 +86,13 @@ class Main: if __name__ == "__main__": + from sys import platform + if platform == "linux" or platform == "linux2": + from pyxhook import HookManager + linux = True + else: + from pyHook import HookManager + linux = False + main = Main() - main.init() + main.init(linux)