python模拟键盘输入+切换键盘布局

PostMessage()

def keyHwnd(hwndEx, char):
    """
    向指定控件输入值
    :param hwndEx: 控件句柄
    :param char: 字符串
    :return: True or Flase
    """
    try:
        for _ in char:
            print('key:%s    ascii:%d'  % (_, ord(_)))
            win32api.PostMessage(hwndEx, win32con.WM_CHAR, ord(_), 0)
            time.sleep(random.uniform(0,0.2))
    except Exception as e:
        print(e)
        return False

    return True
    
hwnd = win32gui.FindWindow(None,'a.txt - 记事本')
print(hwnd)

win32gui.SetForegroundWindow(hwnd)
hwndex = win32gui.FindWindowEx(hwnd,None,'Edit', None)
keyHwnd(hwndex,'撒地方SDFkof;ldsojfdfdsjfd;slkjfdlksjfkldsjflkdsjlkfjkldsljkfjdssj')
   

切换键盘布局

该功能封装成了一个装饰器,函数执行完恢复了原键盘布局

import win32con
from win32con import WM_INPUTLANGCHANGEREQUEST
import win32gui
import win32api

def setKeyboardLayout_en(inner):

    def wrapper(*args, **kwargs):
        if win32api.LoadKeyboardLayout('0x0409', win32con.KLF_ACTIVATE) == None:
            return Exception('加载键盘失败')
        # 语言代码
        # https://msdn.microsoft.com/en-us/library/cc233982.aspx
        LID = {0x0804: "Chinese (Simplified) (People's Republic of China)",
               0x0409: 'English (United States)'}

        # 获取前景窗口句柄
        hwnd = win32gui.GetForegroundWindow()

        # 获取前景窗口标题
        title = win32gui.GetWindowText(hwnd)
        # 获取键盘布局列表
        im_list = win32api.GetKeyboardLayoutList()
        im_list = list(map(hex, im_list))
        print(im_list)
        oldKey = hex(win32api.GetKeyboardLayout())

        # 设置键盘布局为英文
        result = win32api.SendMessage(
            hwnd,
            WM_INPUTLANGCHANGEREQUEST,
            0,
            0x4090409)
        if result == 0:
            print('设置英文键盘成功!')


        inner(*args,*kwargs)

        result = win32api.SendMessage(
            hwnd,
            WM_INPUTLANGCHANGEREQUEST,
            0,
            oldKey)
        if result == 0:
            print('还原键盘成功!')
    return wrapper

ascii标准码

ascii标准码

keybd_event()

win32api.keybd_event(65, 0, 0, 0)
time.sleep(random.uniform(0, 0.5))
win32api.keybd_event(65, 0, win32con.KEYEVENTF_KEYUP, 0)

microsoft键盘码参考

1405648879_285726.jpg

1405648891_822255.jpg

这位兄弟写的关于模拟键盘非常全

转载于:https://www.cnblogs.com/jokerBi/p/10974028.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值