Added dynamic support for importing and using pyxhook

This commit is contained in:
Sofia 2018-12-11 00:44:04 +02:00
parent 45542b1b7e
commit 8250f43f61
2 changed files with 20 additions and 7 deletions

View File

@ -22,8 +22,6 @@ or **on Linux** run
After that install pygame with After that install pygame with
`python -m pip install pygame` `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 And then you should be able to open bongocat with
`python main.py` `python main.py`

25
main.py
View File

@ -2,7 +2,6 @@
import pygame import pygame
import sys import sys
from pyHook import HookManager
from input_manager import InputManager from input_manager import InputManager
from bongocat import Bongocat from bongocat import Bongocat
@ -17,15 +16,17 @@ class Main:
bongocat = Bongocat() bongocat = Bongocat()
clackmanager = Clackmanager() clackmanager = Clackmanager()
debug = False debug = True
sounds = False sounds = False
def init(self): def init(self, linux):
hooks_manager = HookManager() hooks_manager = HookManager()
hooks_manager.KeyDown = self.OnKeyDown hooks_manager.KeyDown = self.OnKeyDown
hooks_manager.KeyUp = self.OnKeyUp hooks_manager.KeyUp = self.OnKeyUp
hooks_manager.HookKeyboard() hooks_manager.HookKeyboard()
if linux:
hooks_manager.start()
pygame.init() pygame.init()
pygame.mixer.init(buffer=1024) pygame.mixer.init(buffer=1024)
@ -40,7 +41,7 @@ class Main:
screen = pygame.display.set_mode(size) screen = pygame.display.set_mode(size)
self.font = pygame.font.SysFont('Iosevka', 24) self.font = pygame.font.SysFont('Iosevka', 15)
self.bongocat.load() self.bongocat.load()
self.clackmanager.load() self.clackmanager.load()
@ -49,6 +50,8 @@ class Main:
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
if linux:
hooks_manager.cancel()
sys.exit() sys.exit()
font_surface = self.font.render("Pressed keys: " + str(self.input_manager.currently_pressed_keys), font_surface = self.font.render("Pressed keys: " + str(self.input_manager.currently_pressed_keys),
@ -62,8 +65,12 @@ class Main:
pygame.display.flip() pygame.display.flip()
if linux:
hooks_manager.cancel()
def OnKeyDown(self, event): def OnKeyDown(self, event):
self.input_manager.press_key(event.Key) self.input_manager.press_key(event.Key)
print event.Message
return True return True
def OnKeyUp(self, event): def OnKeyUp(self, event):
@ -79,5 +86,13 @@ class Main:
if __name__ == "__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 = Main()
main.init() main.init(linux)