1

I get that it's a very difficult thing to pick up on in the background, but what I'm looking for is a program that records a keypress, and how much time it takes between keypresses. None of what I have looked into was able to record in the background, or actually work.

EDIT:

import win32con, ctypes, ctypes.wintypes

def esc_pressed():
    print("Hotkey hit!")

ctypes.windll.user32.RegisterHotKey(None, 1, 0, 0xDD) # this binds the ']' key

try:
    msg = ctypes.wintypes.MSG()
    ctypes.windll.user32.GetMessageA
    while ctypes.windll.user32.GetMessageA(ctypes.byref(msg), None, 0, 0) != 0:
        if msg.message == win32con.WM_HOTKEY:
            esc_pressed()
        ctypes.windll.user32.TranslateMessage(ctypes.byref(msg))
        ctypes.windll.user32.DispatchMessageA(ctypes.byref(msg))
finally:
    ctypes.windll.user32.UnregisterHotKey(None, 1)

This allows for the program to work in the background, but it takes the inputted character you bound, instead of picking up on it. I still need to make sure the inputted character gets to the window with focus.

1 Answers1

1

You probably have to catch the key and then simulate the same key press again. Try checking out Python Keyboard module for that.

EDIT: Added code sample.

import keyboard, time

def KeyPressSample(startkey='tab', endkey='esc'):
    while True:  # making a inifinte loop
        try:
            if keyboard.is_pressed(startkey):
                time.sleep(0.25)
                print("%s Key pressed." % startkey)
            elif keyboard.is_pressed(endkey):
                print("%s Key pressed." % endkey)
                break
        except KeyboardInterrupt:
            print('\nDone Reading input. Keyboard Interupt.')
            break
        except Exception as e:
            print(e)
            break
Naveen
  • 770
  • 10
  • 22
  • 1
    A thought on improving your answer could be to add code snippets modeling how you would use the python keyboard module to accomplish such task :) – Smarticles101 Jan 24 '19 at 14:30