Created
April 2, 2024 02:31
-
-
Save laygond/ba9b868599e2568fe7f81e2809aec3d4 to your computer and use it in GitHub Desktop.
pynput keylogger starter code example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pynput.keyboard import Key, Listener | |
#--- Helper Function --- | |
def alphanumeric_filter(key): | |
''' | |
Return alphanumeric keys as a char type and special keys as a key type | |
''' | |
try: | |
return key.char | |
except AttributeError: | |
return key | |
# Callback functions needed for Keyboard Listener | |
def on_press(key): | |
key = alphanumeric_filter(key) | |
print('{} was pressed'.format(key)) | |
def on_release(key): | |
key = alphanumeric_filter(key) | |
print('{} was released'.format(key)) | |
# ADD ALL YOUR CONDITIONS HERE | |
if key == Key.esc or key== 'q': | |
return False # Stop listener | |
if key == 'a': | |
print("EXCLUSIVE MESSAGE for alphabet key = a") | |
if key == '1': | |
print("EXCLUSIVE MESSAGE for number key = 1") | |
if key == Key.space: | |
print("EXCLUSIVE MESSAGE for special key = Key.space") | |
if key in ['s','e','x','y']: | |
print("EXCLUSIVE MESSAGE for sexy keys") | |
print('[INFO] Keyboard Listener started... ') | |
with Listener(on_press = on_press, | |
on_release = on_release) as listener: | |
listener.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment