|
50鱼币
以下是可以输出英文到输入框的代码,但是处理不了中文:
- # !/usr/bin/env python3
- # _*_ coding:utf-8 _*_
- """
- @File : AutoInput.py
- @Project : AutoInput
- @Time : 2023/5/10 22:15
- @Author : Gag
- @Software : PyCharm
- @License : (C)Copyright 2018-2028, Taogroup-NLPR-CASIA
- @Last Modify Time @Version @Desciption
- -------------------- -------- -----------
- 2023/5/10 22:15 1.0 None
- """
- # 监听键盘以达到快捷键启动程序
- import pythoncom
- import PyHook3
- # 用户选择文件
- import tkinter as tk
- from tkinter import filedialog
- # 用于判断文件的编码格式
- import chardet
- # pip install chardet -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
- # 用于模拟键盘输入
- from pymouse import PyMouse
- from pykeyboard import PyKeyboard
- # pip install PyUserInput -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
- import pypinyin
- # pip install pypinyin -i https://pypi.tuna.tsinghua.edu.cn/simple some-package
- import time
- # 判断当前输入的文字是否是中文
- def is_chinese(word):
- for ch in word:
- if '\u4e00' <= ch <= '\u9fff':
- return True
- return False
- # 判断文件编码格式
- def get_encoding(file):
- with open(file, 'rb') as f:
- tmp = chardet.detect(f.read())
- return tmp['encoding']
- # 监听键盘
- def onKeyboardEvent(event):
- '''
- # 监听键盘事件
- print("MessageName:", event.MessageName)
- print("Message:", event.Message)
- print("Time:", event.Time)
- print("Window:", event.Window)
- print("WindowName:", event.WindowName)
- print("Ascii:", event.Ascii, chr(event.Ascii))
- print("Key:", event.Key)
- print("KeyID:", event.KeyID)
- print("ScanCode:", event.ScanCode)
- print("Extended:", event.Extended)
- print("Injected:", event.Injected)
- print("Alt", event.Alt)
- print("Transition", event.Transition)
- print("---")
- '''
- # 通过 键盘上的 Insert 按键 触发自动输入
- if event.Key == "Insert":
- # 打开文件
- # 创建对象
- root = tk.Tk()
- root.withdraw()
- # 获取文件路径
- FilePath = filedialog.askopenfilename()
- print(FilePath)
- # 获取文件编码格式
- Encoding = get_encoding(FilePath)
- print("%s的文件编码格式是%s:" % (FilePath, Encoding))
- # 创建文件对象
- f = open(FilePath, 'r', encoding=Encoding)
- lines = f.read()
- # print(lines)
- # 将文件内容输出到指定地点
- # 创建鼠标键盘对象
- m = PyMouse() # 建立鼠标对象
- k = PyKeyboard() # 建立键盘对象
- # 获取文字输入位置
- location = m.position()
- m.click(location[0], location[1])
- # 自动输入部分
- k.type_string(lines)
- #关闭文件
- f.close()
- # 不要修改此返回值,否则将对按键进行拦截
- return True
- def main():
- # 创建一个“钩子”管理对象
- hm = PyHook3.HookManager()
- # 监听所有键盘事件
- hm.KeyDown = onKeyboardEvent
- # 设置键盘“钩子”
- hm.HookKeyboard()
- # 进入循环,如不手动关闭,程序将一直处于监听状态
- pythoncom.PumpMessages()
- if __name__ == "__main__":
- main()
复制代码
我该如何修改代码才能处理中文字符?
我没看明白,你要把中英文输入到输入框吗,代替键盘输入吗?直接用keyboard库啊,中英文都行。
|
最佳答案
查看完整内容
我没看明白,你要把中英文输入到输入框吗,代替键盘输入吗?直接用keyboard库啊,中英文都行。
|