python window鼠标键盘事件

import ctypes
import win32clipboard as w
import win32con
import win32api

class WinEvent:
    keys = {'A': 65, 'J': 74, 'S': 83, '1': 49, 'B': 66, 'K': 75, 'T': 84, '2': 50, 'C': 67, 'L': 76, 'U': 85, '3': 51,
            'D': 68, 'M': 77, 'V': 86, '4': 52, 'E': 69, 'N': 78, 'W': 87, '5': 53, 'F': 70, 'O': 79, 'X': 88, '6': 54,
            'G': 71, 'P': 80, 'Y': 89, '7': 55, 'H': 72, 'Q': 81, 'Z': 90, '8': 56, 'I': 73, 'R': 82, '0': 48, '9': 57,
            'F1': 112, 'F7': 118, 'F2': 113, 'F8': 119, '*': 106, 'F3': 114, 'F9': 120, 'F4': 115, 'F10': 121,
            'ENTER': 13, 'F5': 116, 'F11': 122, 'F6': 117, 'F12': 123, '.': 190, 'BACKSPACE': 8, 'CTRL_L': 17,
            'CTRL': 17, 'CAPS_LOCK': 4, 'CMD':91, 'CMD_R':91, 'MENU': 93,
            'ESC': 27, 'RIGHTARROW': 39, '-': 189, 'TAB': 9, 'SPACE': 32, 'DOWNARROW': 40, 'CLEAR': 12,
            'PAGEUP': 33, 'INSERT': 45, '/': 191, 'PAGEDOWN': 34, 'PAGE_DOWN': 34, 'PAGE_UP': 33, 'DELETE': 46, '`': 192, 'SHIFT': 16, 'END': 35,
            'NUMLOCK': 144, '[': 219, 'CONTROL': 17, 'HOME': 36, ';': 186, '\\': 220, 'ALT': 18, 'ALT_L': 18, 'LEFTARROW': 37,
            '=': 187, ']': 221, 'CAPELOCK': 20, 'UPARROW': 38, ',': 188, "'": 222, '"': 'SHIFT+\'', '<': 'SHIFT+,',
            '}': 'SHIFT+]',
            '|': 'SHIFT+\\', '+': 'SHIFT+=', '?': 'SHIFT+/', '{': 'SHIFT+[', ':': 'SHIFT+;', '_': 'SHIFT+-',
            '>': 'SHIFT+.', '~': 'SHIFT+`'
            }
    hex_code = {'\\x01': 'A', '\\x02': 'B', '\\x03': 'C', '\\x04': 'D', '\\x05': 'E', '\\x06': 'F', '\\x07': 'G', '\\x08': 'H',
                '\\t': 'I', '\\n': 'J', '\\x0b': 'K', '\\x0c': 'L', '\\r': 'M', '\\x0e': 'N', '\\x0f': 'O', '\\x10': 'P',
                '\\x11': 'Q', '\\x12': 'R', '\\x13': 'S', '\\x14': 'T', '\\x15': 'U', '\\x16': 'V', '\\x17': 'W', '\\x18': 'X',
                '\\x19': 'Y', '\\x1a': 'Z', '\\x1b': '[', '\\x1d': ']', '\\x1c': '\\'}

    @staticmethod
    def set_text(string):
        w.OpenClipboard()
        w.EmptyClipboard()
        w.SetClipboardData(win32con.CF_UNICODETEXT, string)
        w.CloseClipboard()

    def enter_text(self, text):
        self.set_text(text)
        self.ctrlV()

    @staticmethod
    def ctrlV():
        win32api.keybd_event(17, 0, 0, 0)  # ctrl
        win32api.keybd_event(86, 0, 0, 0)  # V
        win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)  # 释放按键
        win32api.keybd_event(17, 0, win32con.KEYEVENTF_KEYUP, 0)

    @staticmethod
    def scroll_up():
        win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, 119)

    @staticmethod
    def scroll_down():
        win32api.mouse_event(win32con.MOUSEEVENTF_WHEEL, 0, 0, -119)

    @staticmethod
    def scroll_right():
        win32api.mouse_event(win32con.MOUSEEVENTF_HWHEEL, 0, 0, 119, 0)

    @staticmethod
    def scroll_left():
        win32api.mouse_event(win32con.MOUSEEVENTF_HWHEEL, 0, 0, -119, 0)

    @staticmethod
    def altS():
        win32api.keybd_event(18, 0, 0, 0)
        win32api.keybd_event(83, 0, 0, 0)
        win32api.keybd_event(83, 0, win32con.KEYEVENTF_KEYUP, 0)
        win32api.keybd_event(18, 0, win32con.KEYEVENTF_KEYUP, 0)

    @staticmethod
    def enter():
        win32api.keybd_event(13, 0, 0, 0)
        win32api.keybd_event(13, 0, win32con.KEYEVENTF_KEYUP, 0)

    def key(self, key):
        code = self.keys.get(key.upper())
        if isinstance(code, str):
            self.unite_key(code)
        else:
            win32api.keybd_event(code, 0, 0, 0)
            win32api.keybd_event(code, 0, win32con.KEYEVENTF_KEYUP, 0)

    def _hex_key(self, key):
        key = self.hex_code.get(key)
        code = self.keys.get(key)
        return code

    def unite_key(self, key):
        codes = key.split('+')
        for code in codes:
            self.key_down(code)
        for code in codes[::-1]:
            self.key_up(code)

    def key_down(self, key):
        code = self.keys.get(key.upper())
        if isinstance(code, str):
            self.unite_key(code)
            return
        if not code:
            key = self.hex_code.get(key)
            code = self.keys.get(key)
        if not code:
            return
        win32api.keybd_event(code, 0, 0, 0)

    def key_up(self, key):
        code = self.keys.get(key.upper())
        if isinstance(code, str):
            self.unite_key(code)
            return
        if not code:
            key = self.hex_code.get(key)
            code = self.keys.get(key)
        if not code:
            return
        win32api.keybd_event(code, 0, win32con.KEYEVENTF_KEYUP, 0)

    @staticmethod
    def lmouse_down():
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)

    @staticmethod
    def lmouse_up():
        win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

    @staticmethod
    def rmouse_down():
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)

    @staticmethod
    def rmouse_up():
        win32api.mouse_event(win32con.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

    # 模拟单击
    def lclick(self):
        self.lmouse_down()
        self.lmouse_up()

    def rclick(self):
        self.rmouse_down()
        self.rmouse_up()

    # 移动鼠标的位置
    @staticmethod
    def move_mouse(position):
        ctypes.windll.user32.SetCursorPos(position[0], position[1])

    def mouse_click(self, position):
        self.move_mouse(position)
        self.lclick()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值