Skip to content

Commit

Permalink
新功能:复制OCR文本后发送指定快捷键,可用于唤起翻译器等工具
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroi-sora committed Jun 18, 2023
1 parent 9bc76d6 commit 43116f0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 15 deletions.
15 changes: 14 additions & 1 deletion ocr/msn_quick.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from ocr.msn import Msn
from ocr.output_panel import OutputPanel # 输出器
from ui.win_notify import Notify # 通知弹窗
from utils.hotkey import Hotkey # 按键

import tkinter as tk
import time
Expand Down Expand Up @@ -62,7 +63,19 @@ def onGet(self, numData, ocrData):
self.outputPanel.print(tbStr) # 输出到面板
if Config.get('isNeedCopy'): # 需要复制
pyperclipCopy(tbStr) # 复制到剪贴板
Notify('已复制文字', tbStr)
if not Config.get('isHotkeyFinishSend'):
Notify('已复制文字', tbStr)
else: # 需要发送按键
ks, kn = Config.get('hotkeyFinishSend'), Config.get('hotkeyFinishSendNumber')
kt = Config.get('hotkeyFinishSendBetween')
try:
for i in range(kn):
if i > 0: # 间隔
time.sleep(kt)
Hotkey.send(ks) # 发送按键
Notify(f"已发送按键 {ks}", tbStr)
except Exception as e:
Notify(f"发送按键 {ks} 失败", str(e))
else:
Notify('识别完成', tbStr)
# 计算置信度
Expand Down
19 changes: 10 additions & 9 deletions ui/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def delHotkey(hotkey): # 移除已有快捷键
Log.info(f'快捷键【{hotkey}】移除错误:{err}')

@staticmethod
def hotkeyFrame(master, name, configName, func, isFix=False, hotkeyCom=None, isAutoBind=False):
def hotkeyFrame(master, name, configName, func=None, isFix=False, hotkeyCom=None, isAutoBind=False):
'''添加一个热键框架
父框架 | 热键名称(描述) | 热键在Config中的名称 | 触发事件 |
固定热键 | 固定热键名 | 是否创建完成后自动绑定'''
Expand All @@ -56,13 +56,14 @@ def addHotkey(hotkey): # 注册新快捷键
tk.messagebox.showwarning(
'提示', f'请先录制{name}快捷键')
return
try:
Hotkey.add(hotkey, func) # 添加快捷键监听
except ValueError as err:
Config.set(isHotkey, False)
Config.set(hotkeyName, '')
tk.messagebox.showwarning(
'提示', f'无法注册快捷键【{hotkey}\n\n错误信息:\n{err}')
if callable(func):
try:
Hotkey.add(hotkey, func) # 添加快捷键监听
except ValueError as err:
Config.set(isHotkey, False)
Config.set(hotkeyName, '')
tk.messagebox.showwarning(
'提示', f'无法注册快捷键【{hotkey}\n\n错误信息:\n{err}')

def onRead(): # 当 修改键按下

Expand Down Expand Up @@ -114,7 +115,7 @@ def onCheck(): # 当 复选框按下

# 标题 | 快捷键Label | 修改
wid = ttk.Checkbutton(hFrame, variable=Config.getTK(isHotkey),
text=f'{name} 快捷键 ', command=onCheck)
text=name, command=onCheck)
wid.grid(column=0, row=0, sticky='w')

if isFix: # 固定组合,不给修改
Expand Down
20 changes: 16 additions & 4 deletions ui/win_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -438,22 +438,22 @@ def changeColor(configName, title=None):
lab3.pack(side='left', padx=9)
lab3.bind(
'<Button-1>', lambda *e: changeColor('scsColorBoxDown', '截图矩形框 虚线底层颜色'))
wid = Widget.hotkeyFrame(fhkUmi, '截图识别 ', 'Screenshot',
wid = Widget.hotkeyFrame(fhkUmi, '截图识别 快捷键 ', 'Screenshot',
lambda *e: self.win.event_generate(
'<<ScreenshotEvent>>'), isAutoBind=False)
wid.pack(side='top', fill='x')
self.balloon.bind(
wid, '关闭快捷键后,仍能通过面板上的按钮或托盘小图标调用截图\n点击【修改】可设置自定义快捷键')

syssscom = 'win+shift+s'
fhkSys = Widget.hotkeyFrame(frss, '系统截图 ', 'Screenshot',
fhkSys = Widget.hotkeyFrame(frss, '系统截图 快捷键 ', 'Screenshot',
lambda *e: self.win.event_generate(
'<<ScreenshotEvent>>'), True, syssscom, isAutoBind=False)
self.balloon.bind(
fhkSys, '监听到系统截图后调用OCR\n\n若截图后软件没有反应,请确保windows系统自带的\n【截图和草图】中【自动复制到剪贴板】开关处于打开状态')

wid = Widget.hotkeyFrame(
fQuick, '粘贴图片 ', 'Clipboard', self.runClipboard, isAutoBind=True)
fQuick, '粘贴图片 快捷键 ', 'Clipboard', self.runClipboard, isAutoBind=True)
wid.pack(side='top', fill='x', padx=4)
self.balloon.bind(wid, '尝试读取剪贴板,若存在图片则调用OCR\n点击【修改】可设置自定义快捷键')
if Config.get('isAdvanced'): # 隐藏高级选项:组合键判定调节
Expand Down Expand Up @@ -499,6 +499,18 @@ def changeColor(configName, title=None):
wid.grid(column=2, row=1)
self.balloon.bind(wid, f'每次快捷识图将清空识别内容面板,同时省略时间等信息')

if Config.get('isAdvanced'): # 隐藏高级选项:自动发送按键
frSend = tk.Frame(fQuick)
frSend.pack(side='top', fill='x', pady=2, padx=4)
frSend.grid_columnconfigure(0, weight=1)
self.balloon.bind(frSend, '执行OCR并将结果复制到剪贴板后,发送键盘按键\n可用于联动唤起翻译器或AHK等工具\n次:重复发送按键的次数,如2为双击')
wid = Widget.hotkeyFrame(
frSend, '自动复制后发送按键', 'FinishSend', isAutoBind=True)
wid.grid(column=0, row=0, sticky="nsew")
tk.Entry(frSend, width=2, textvariable=Config.getTK('hotkeyFinishSendNumber')
).grid(column=1, row=0)
tk.Label(frSend, text='次').grid(column=2, row=0)

# 切换截图模式
def onModeChange():
isHotkey = Config.get('isHotkeyScreenshot')
Expand Down Expand Up @@ -852,7 +864,7 @@ def initEX(): # 额外
wid = tk.Checkbutton(fEX, text='高级选项', fg='gray',
variable=Config.getTK('isAdvanced'))
self.balloon.bind(
wid, '启用隐藏的高级选项,重启后生效\n组合键判定规则 | 图片许可后缀 | 引擎启动参数 | 引擎管理策略')
wid, '启用隐藏的高级选项,重启后生效')
wid.pack(side='right', padx=10)
# 若初始时非置顶,不显示提示,则尾部预留出空间
if not Config.get('isWindowTop'):
Expand Down
20 changes: 20 additions & 0 deletions utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,26 @@ class WindowTopModeFlag():
'isSave': True,
'isTK': True,
},
'isHotkeyFinishSend': { # T时启用复制文本后发送按键
'default': False,
'isSave': True,
'isTK': True,
},
'hotkeyFinishSend': { # 复制文本后发送按键
'default': 'ctrl+c',
'isSave': True,
'isTK': True,
},
'hotkeyFinishSendNumber': { # 发送按键的次数
'default': 2,
'isSave': True,
'isTK': True,
},
'hotkeyFinishSendBetween': { # 重复发送按键的间隔时间,秒
'default': 0.2,
'isSave': True,
'isTK': True,
},
# 计划任务设置
'isOpenExplorer': { # T时任务完成后打开资源管理器到输出目录
'default': False,
Expand Down
2 changes: 1 addition & 1 deletion utils/hotkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def addRelease(hotkey, callback):
def remove(hotkey):
'''移除一个快捷键组合监听'''
hotkeyApi.remove(hotkey) # 移除该快捷键
Log.info(f'注销监听 ×【{hotkey}】')
# Log.info(f'注销监听 ×【{hotkey}】')

@staticmethod
def read(callback):
Expand Down

0 comments on commit 43116f0

Please sign in to comment.