python : Wait event on keyboard, with minimized script?

Asked

Viewed 190 times

0

I wonder if there is a way to detect events on the keyboard with my minimized script.

I’ve tried several methods like msvcrt.getch(), input("") and others but they only work with the focus on the script window.

The program flow is as follows:

Main function

 1 - Espera um evento(qualquer) no teclado
 2 - Se houver um evento
        Funçãoqualquer()
 3 - Se não
        retorna para a função Main
  • You can use pyhook to hook the http://pyhook.sourceforge.net/doc_1.5.0keyboard event/

1 answer

0

As stated earlier, you can use the library Pyhook that monitors user activity through your keyboard or mouse inputs.
I bumped into a video which shows an effective example of saving keyboard inputs 'silently' using Pyhook:

import pyHook, pythomcom, sys, logging
file_log='C:\\important\\log.txt' #Define onde as entradas serão salvas

def OnKeyboardEvent(event): #Configurações do log 
    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()

The code may be a bit 'old' but it is a clear example of how such a feature would be.

For the script to work silently in fact it is necessary to be saved in the format pyw that makes it run without opening a runscreen.

NOTE: In the video quoted it is said that such technique can be used maliciously so use it consciously.

Browser other questions tagged

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