如何在python后台读取按键

问题描述 投票:0回答:1

我得到,这是一个非常难有起色,在后台的事情,但是我正在寻找的是一个程序,可以记录按键,多少时间需要按键之间。什么我都看着没有一个能够在后台录制,或实际工作。

编辑:

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)

这允许程序在后台工作,但它需要的,而不是在它拿起你绑定的输入的字符。我还需要确保输入的角色获得与焦点的窗口。

python-3.x keypress
1个回答
0
投票

你可能要抓住关键,然后再模拟相同的按键。请检查出来了Python,键盘模块。

编辑:添加代码示例。

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
© www.soinside.com 2019 - 2024. All rights reserved.