pynput 1.基础操作+键盘鼠标监控

一、控制鼠标

1.监控鼠标操作

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()

2.鼠标操作

from pynput.mouse import Button, Controller

mouse = Controller()

1)click(button, count=1)
Emits a button click event at the current position.

The default implementation sends a series of press and release events.

Parameters:

  • button (Button) – The button to click.
  • count (int) – The number of clicks to send.

2)move(dx, dy)
Moves the mouse pointer a number of pixels from its current position.

Parameters:

  • x (int) – The horizontal offset.
  • dy (int) – The vertical offset.
    Raises:
  • ValueError – if the values are invalid, for example out of bounds

3)position
The current position of the mouse pointer.

This is the tuple (x, y), and setting it will move the pointer.

4)press(button)
Emits a button press event at the current position.

Parameters: button (Button) – The button to press.

5)release(button)
Emits a button release event at the current position.

Parameters: button (Button) – The button to release.

6)scroll(dx, dy)
Sends scroll events.

Parameters:

  • dx (int) – The horizontal scroll. The units of scrolling is undefined.
  • dy (int) – The vertical scroll. The units of scrolling is undefined.
    Raises:
    ValueError – if the values are invalid, for example out of bounds

二、控制键盘

1.监控键盘操作

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()

2.键盘操作

1)press(key)
Presses a key.

2)release(key)
Releases a key.

3)type(string)

你可能感兴趣的:(pynput 1.基础操作+键盘鼠标监控)