Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows hook with Python
    primarykey
    data
    text
    <p>Hey guys. I'm trying to get a hook into windows and use Python to record keystrokes. For this, I'm using a LowLevelKeyboard Callback procedure.</p> <pre><code>def run(): global KeyBdHook global messages KeyBdHook = HHook() messages = [] start = time.time() #Record keystrokes for 2 seconds. while time.time() &lt; (start + 2): KeyBdHook.hook = SetWindowsHookEx(13, KeyboardProc, GetModuleHandle(0), 0) if KeyBdHook.hook == 0: print 'ERROR: '+str(ctypes.windll.kernel32.GetLastError()) UnhookWindowsHookEx(KeyBdHook.hook) print messages def KeyboardProc(nCode, wParam, lParam): """http://msdn.microsoft.com/en-us/library/ms644985(v=vs.85).aspx""" if nCode &lt; 0: return ctypes.windll.user32.GetNextHookEx(KeyBdHook.hook, nCode, wParam, lParam) else: ctypes.windll.kernel32.RtlMoveMemory(ctypes.addressof(KeyBdHook.kStruct), ctypes.c_void_p(lParam), ctypes.sizeof(lParam)) messages.append(KeyBdHook.kStruct) return ctypes.windll.user32.GetNextHookEx(KeyBdHook.hook, nCode, wParam, lParam) def SetWindowsHookEx(idHook, lpFn, hMod, dwThreadId): WinFunc = ctypes.WINFUNCTYPE(c_ulong, c_ulong, c_ulong, c_ulong) return ctypes.windll.user32.SetWindowsHookExA(idHook, WinFunc(lpFn), hMod, dwThreadId) def GetModuleHandle(lpModuleName): return ctypes.windll.kernel32.GetModuleHandleA(lpModuleName) def UnhookWindowsHookEx(hHook): return ctypes.windll.user32.UnhookWindowsHookEx(hHook) class HHook(): def __init__(self): self.hook = HHOOK self.kStruct = KBLLHOOKSTRUCT() class KBLLHOOKSTRUCT(Structure): """http://msdn.microsoft.com/en-us/library/ms644967(v=vs.85).aspx""" _fields_ = [("vkCode", c_ulong), ("scanCode", c_ulong), ("flags", c_ulong), ("time", c_ulong), ("dwExtraInfo", POINTER(c_ulong))] </code></pre> <p>The problem with this is it never enters the KeyboardProc function. I'm thinking that I might have to cast it as a C-type function using ctypes.WINFUNCTYPE or ctypes.CFUNCTYPE, but I can't figure it out. Windows doesn't seem to be throwing an error on SetWindowsEx either.</p> <p>I'm assuming it's not handling the KeyboardProc parameter being passed into SetWindowsEx. Any ideas on how to cast this so Windows can input data into it? Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload