简体   繁体   English

Python:同时记录击键和鼠标移动

[英]Python: Recording Keystrokes and Mousemovement simultaneously

i cant find a solution for the following problem:我找不到以下问题的解决方案:

I would like to record keystrokes and mousemovement at the same time.我想同时记录击键和鼠标移动。 Right now i tried to combine the scripts from the pynput Package Documentation.现在我尝试组合来自 pynput 包文档的脚本。

Monitoring the mouse: https://pynput.readthedocs.io/en/latest/mouse.html#monitoring-the-mouse监控鼠标: https : //pynput.readthedocs.io/en/latest/mouse.html#monitoring-the-mouse

Monitoring the keyboard: https://pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard监控键盘: https : //pynput.readthedocs.io/en/latest/keyboard.html#monitoring-the-keyboard

from pynput import mouse
from pynput import keyboard

def on_move(x, y):
    print('Pointer moved to {0}'.format(
        (x, y)))

def on_click(x, y, button, pressed):
    print('{0} at {1}'.format(
        'Pressed' if pressed else 'Released',
        (x, y)))
    if not pressed:
        # Stop listener
        return False

def on_scroll(x, y, dx, dy):
    print('Scrolled {0}'.format(
        (x, y)))

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False 

# Collect events until released
with mouse.Listener(
        on_move=on_move,
        on_click=on_click,
        on_scroll=on_scroll) as listener:
    listener.join()
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()   

Right now the keyboard listener only starts after the mouse listener gets closed.现在键盘侦听器仅在鼠标侦听器关闭后才启动。 Is there any way to record mouse and keyboard at the same time?有没有办法同时录制鼠标和键盘? Are there better site-packages for this?是否有更好的站点包? Thanks a lot in advance!非常感谢!

All you need is this, my friend你只需要这个,我的朋友

# Collect events until released
with keyboard.Listener(on_release=on_release) as k_listener, mouse.Listener(on_click=on_click) as m_listener:
    k_listener.join()
    m_listener.join()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM