Learning KeymouseGo - 03 Recorder

109 阅读1分钟

Introduction

These files are used to recorder the operation on keyboard and mouse.

1.png

This article will ignore WindowsRecorder.py since it is only activate on windows system.

UniversalRecorder.py

UniversalRecorder.py mainly implement a system to record and play back user interactions with the computer, including mouse movements, clicks, and keyboard input. It uses the pynput library to capture mouse and keyboard events, and the Recorder module to record and play back the events.

Pynput

Pynput library allows you to control and monitor input devices. In UniversalRecorder.py, only imported mouse and keyboard.

from pynput import mouse

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} at {1}'.format(
        'down' if dy < 0 else 'up',
        (x, y)))

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

# ...or, in a non-blocking fashion:
listener = mouse.Listener(
    on_move=on_move,
    on_click=on_click,
    on_scroll=on_scroll)
listener.start()

We can define functions on_move, on_click and on_scroll in mouse.Listener to set the things should be done when corresponding respective events.

from pynput import keyboard

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 keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = keyboard.Listener(
    on_press=on_press,
    on_release=on_release)
listener.start()

The monitor of keyboard works the same. Define functions on_press and on_release in keyboard.Listener to set the things should be done at corresponding events. In this case, we should only record the information of these events when they occur.

To be mentioned, the monitors are always thread.Thread.

__init__.py and globals.py

globals.py defines several functions which are universal between windows and ubuntu, while __init__.py checks the system and automatically choose the corresponding file between WindowsRecorder.py and UniversalRecorder.py.