美文网首页
python判断输入法

python判断输入法

作者: BI罗 | 来源:发表于2021-03-01 11:48 被阅读0次
import ctypes
import time
import os

print('5秒后本程序会开始运行,\n请迅速将本程序置于后台,\n然后到文本编辑器之类的软件的输入框切换输入法到需要的状态\n\n')
time.sleep(5)

user32 = ctypes.WinDLL('user32', use_last_error=True)
curr_window = user32.GetForegroundWindow()
thread_id = user32.GetWindowThreadProcessId(curr_window, 0)
klid = user32.GetKeyboardLayout(thread_id)
lid = klid & (2**16 - 1)
lid_hex = hex(lid)

print(lid_hex)
if lid_hex == '0x409':                                     #好像就只有美国键盘布局才是这个字符
    print('当前的输入法状态是英文输入模式\n\n')
elif lid_hex == '0x804':                                   #其他输入法也可能是0x804',英国键盘布局也是这个
    print('当前的输入法是搜狗输入法\n\n')
else:
    print('当前的输入法既不是英文输入也不是搜狗输入法\n\n')

os.system('cmd')

这个代码不严谨,无法获取当前是否中英的状态

相关文章

网友评论

      本文标题:python判断输入法

      本文链接:https://www.haomeiwen.com/subject/tgaofltx.html