首页 > 其他分享 >from pynput.keyboard import Key, Controller k = Controller():在不需要定位元素的前提下的键盘操作

from pynput.keyboard import Key, Controller k = Controller():在不需要定位元素的前提下的键盘操作

时间:2022-09-20 13:55:17浏览次数:74  
标签:elif ctrl release Controller Key keyboard press model

 

def control_keyboard(model, content=None, no_wait=False):
    """
    模拟电脑的键盘操作
    :param model: 要进行的操作:复制--ctrl_C  粘贴--ctrl_V  全选--ctrl_A 删除--del 回车--enter
    :param content: 要输入的内容,默认为None
    no_wait:操作演示按键盘不能等待否则不生效
    """
    # 防止相邻两个操作之间没有停顿导致执行不生效
    if not no_wait:
        time.sleep(5)
    # time.sleep(2)
    if model == 'ctrl_A':
        if sys.platform == 'darwin':
            k.press(Key.cmd)
            k.touch('a', is_press=True)
            k.release(Key.cmd)
        else:
            with k.pressed(Key.ctrl):
                k.press('a')
                k.release('a')

    if model == 'esc':
        k.touch(Key.esc, is_press=True)

    elif model == 'ctrl_C':
        if sys.platform == 'darwin':
            k.press(Key.cmd)
            k.touch('c', is_press=True)
            k.release(Key.cmd)
        else:
            with k.pressed(Key.ctrl):
                k.press('c')
                k.release('c')

    elif model == 'ctrl_V':
        if sys.platform == 'darwin':
            k.press(Key.cmd)
            k.touch('v', is_press=True)
            k.release(Key.cmd)
        else:
            with k.pressed(Key.ctrl):
                k.press('v')
                k.release('v')

    elif model == 'ctrl_T':
        if sys.platform == 'darwin':
            k.press(Key.cmd)
            k.tap('t')
            k.release(Key.cmd)
        else:
            with k.pressed(Key.ctrl):
                k.press('t')
                k.release('t')

    elif model == 'del':
        k.press(Key.backspace)
        k.release(Key.backspace)

    elif model == 'enter':
        if sys.platform == 'darwin':
            k.touch(Key.enter, is_press=True)
        else:
            k.press(Key.enter)
            k.release(Key.enter)

    elif model == 'up':
        k.press(Key.up)
        k.release(Key.up)

    elif model == 'down':
        k.press(Key.down)
        k.release(Key.down)

    elif model == 'left':
        k.press(Key.left)
        k.release(Key.left)

    elif model == 'right':
        k.press(Key.right)
        k.release(Key.right)

    elif model == 'tab':
        k.press(Key.tab)
        k.release(Key.tab)

    elif model == 'input':
        if not isinstance(content, str):
            content = str(content)
        pyperclip.copy(content)
        if sys.platform == 'darwin':
            k.press(Key.cmd)
            k.tap('v')
            k.release(Key.cmd)
        elif sys.platform == 'linux':
            for char in content:
                k.press(char)
                k.release(char)
        else:
            with k.pressed(Key.ctrl):
                k.press('v')
                k.release('v')
    # 防止相邻两个操作之间没有停顿导致执行不生效
    if not no_wait:
        time.sleep(2)

 

标签:elif,ctrl,release,Controller,Key,keyboard,press,model
From: https://www.cnblogs.com/hls-code/p/16710796.html

相关文章