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)

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • 今天早上因为要回老家,起的很早。先背了单词,就去洗漱了。洗漱完就去吃饭了,吃过饭整整东西就去坐车了。唉,坐个车,手...
    温柔鬼_馨阅读 209评论 0 1
  • 当你喜欢我的时候,我不喜欢你 当你爱上我的时候,我喜欢上你 当你离开我的时候,我却爱上你 是你走的太快,还...
    默看红尘阅读 607评论 0 2
  • 对于自贡的记忆,先是盐巴,后来在女儿的描述下,知道了还有恐龙。而女儿对自贡的喜欢缘于她舍友陛下。这个与女儿关系甚好...
    小小的叶阅读 445评论 0 0
  • 那木,你好! 木都嘛死,谢谢! 哇锁坐,吃完了。
    Cocosiny阅读 619评论 0 0