Skip to content

Commit

Permalink
Use GetModuleHandle to fix WindowsXP bug #132
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed Mar 25, 2018
1 parent 2d1a833 commit 9289aeb
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions keyboard/_winkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,15 @@

import ctypes
from ctypes import c_short, c_char, c_uint8, c_int32, c_int, c_uint, c_uint32, c_long, Structure, CFUNCTYPE, POINTER
from ctypes.wintypes import WORD, DWORD, BOOL, HHOOK, MSG, LPWSTR, WCHAR, WPARAM, LPARAM, LONG
from ctypes.wintypes import WORD, DWORD, BOOL, HHOOK, MSG, LPWSTR, WCHAR, WPARAM, LPARAM, LONG, HMODULE, LPCWSTR
LPMSG = POINTER(MSG)
ULONG_PTR = POINTER(DWORD)

kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)
GetModuleHandleW = kernel32.GetModuleHandleW
GetModuleHandleW.restype = HMODULE
#GetModuleHandleW.argtypes = [LPCWSTR]

#https://github.com/boppreh/mouse/issues/1
#user32 = ctypes.windll.user32
user32 = ctypes.WinDLL('user32', use_last_error = True)
Expand Down Expand Up @@ -85,7 +90,7 @@ class INPUT(ctypes.Structure):

LowLevelKeyboardProc = CFUNCTYPE(c_int, WPARAM, LPARAM, POINTER(KBDLLHOOKSTRUCT))

SetWindowsHookEx = user32.SetWindowsHookExA
SetWindowsHookEx = user32.SetWindowsHookExW
#SetWindowsHookEx.argtypes = [c_int, LowLevelKeyboardProc, c_int, c_int]
SetWindowsHookEx.restype = HHOOK

Expand Down Expand Up @@ -547,7 +552,7 @@ def low_level_keyboard_handler(nCode, wParam, lParam):

WH_KEYBOARD_LL = c_int(13)
keyboard_callback = LowLevelKeyboardProc(low_level_keyboard_handler)
keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_callback, NULL, NULL)
keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_callback, GetModuleHandleW(NULL), NULL)

# Register to remove the hook when the interpreter exits. Unfortunately a
# try/finally block doesn't seem to work here.
Expand Down

0 comments on commit 9289aeb

Please sign in to comment.