1- import sys , os
1+ import sys , os , time
22import configparser
33config = configparser .ConfigParser ()
44config .read ('config.ini' , encoding = "utf-8" )
5+ secret_config = configparser .ConfigParser ()
6+ secret_config .read ('secret.ini' , encoding = "utf-8" )
57import pytesseract
68# 判断是开发环境还是生产环境
79if os .path .exists ("./.github" ):
1618 QSystemTrayIcon , QMenu , QLabel , QStyle , QVBoxLayout ,
1719 QGraphicsDropShadowEffect )
1820
19- from pynput import mouse
21+ from pynput import mouse as pynput_mouse
22+ from pynput import keyboard as pynput_keyboard
2023import keyboard
24+ import pyperclip
2125from PIL import Image
2226import ctypes
27+ import win32clipboard
28+ import win32con
2329user32 = ctypes .windll .user32
2430kernel32 = ctypes .windll .kernel32
2531
2632from translator import Translator
2733trans = Translator ()
2834
35+ from translator import tencent
36+ tencent_trans = tencent .Trans (secret_config )
37+
38+ def wait_for_all_keys_up (timeout = 2.0 , interval = 0.1 ):
39+ """等待所有物理按键抬起(超时时间内)"""
40+ start_time = time .time ()
41+ while time .time () - start_time < timeout :
42+ if not any (keyboard .is_pressed (key ) for key in keyboard .all_modifiers | set ("abcdefghijklmnopqrstuvwxyz1234567890" )):
43+ return True
44+ time .sleep (interval )
45+ return False
46+
47+ def copy_selected_text ():
48+ """模拟 Ctrl+C 并返回复制的文本"""
49+ if not wait_for_all_keys_up ():
50+ return None
51+
52+ # 模拟 Ctrl+C
53+ keyboard .press ('ctrl' )
54+ keyboard .press ('c' )
55+ time .sleep (0.1 ) # 等待复制完成
56+ keyboard .release ('c' )
57+ keyboard .release ('ctrl' )
58+
59+ # 获取剪贴板内容
60+ return pyperclip .paste ()
61+
62+
2963# 翻译弹窗,用于展示翻译结果
3064class TextWindow (QWidget ):
3165 open_or_close_triggered = Signal ()
@@ -253,6 +287,7 @@ class TrayApp(QMainWindow):
253287 clipboard_changed_triggered = Signal ()
254288 esc_triggered = Signal ()
255289 click_triggered = Signal (int , int , str , bool )
290+ copy_into_english_triggered = Signal ()
256291 def __init__ (self ):
257292 super ().__init__ ()
258293 self .tray = QSystemTrayIcon (self )
@@ -276,29 +311,39 @@ def __init__(self):
276311 self .tray .setContextMenu (self .menu )
277312 self .tray .show ()
278313
314+ # 连接截屏翻译
279315 self .capture_triggered .connect (self .capture )
280-
281316 capture_triggered_hotkeys = config .get ('DEFAULT' , 'capture_triggered_hotkey' ).split ("," )
282317 self .capture_handle_hotkeys = []
283318 for hotkey in capture_triggered_hotkeys :
284319 self .capture_handle_hotkeys .append (
285320 keyboard .add_hotkey (hotkey , self .capture_triggered .emit ))
286321 self .stop_trans = False
322+
323+ # 连接停止翻译
287324 stoptrans_triggered_hotkeys = config .get ('DEFAULT' , 'stoptrans_triggered_hotkey' ).split ("," )
288325 for hotkey in stoptrans_triggered_hotkeys :
289326 keyboard .add_hotkey (hotkey , self .on_hotkey_stoptrans )
327+
328+ # 连接停止截屏
290329 keyboard .add_hotkey ('esc' , self .esc_triggered .emit )
291330
331+ # 连接将选中的中文转为英文
332+ copy_into_english_triggered_hotkeys = \
333+ config .get ('DEFAULT' , 'copy_into_english_triggered_hotkey' ).split ("," )
334+ for hotkey in copy_into_english_triggered_hotkeys :
335+ keyboard .add_hotkey (hotkey , self .on_hotkey_copy_into_english )
336+
292337 self .clipboard = QApplication .clipboard ()
293338 self .on_hotkey_stoptrans ()
294339
340+ # 创建鼠标监听器
295341 def on_click_wrapper (x , y , button , pressed ):
296342 self .click_triggered .emit (x , y , button .name , pressed )
297- # 创建鼠标监听器
298- self .listener = mouse .Listener (
343+ self .mouse_listener = pynput_mouse .Listener (
299344 on_click = on_click_wrapper ,
300345 )
301- self .listener .start ()
346+ self .mouse_listener .start ()
302347
303348 def clipboard_changed_trans (self ):
304349 clipboard_text = self .clipboard .text ().strip ()
@@ -322,7 +367,15 @@ def on_hotkey_stoptrans(self):
322367 self .action_stoptrans .setText (f"启用复制翻译({ config .get ('DEFAULT' , 'stoptrans_triggered_hotkey' )} )" )
323368 self .stop_trans = not self .stop_trans
324369
325-
370+ # 将选中的中文转为英文快捷键,并原地粘贴,并修改剪切板
371+ def on_hotkey_copy_into_english (self ):
372+ self .on_hotkey_stoptrans ()
373+ clipboard_text = copy_selected_text ()
374+ if trans_result := tencent_trans .translate (clipboard_text , target = "en" ):
375+ pyperclip .copy (trans_result )
376+ keyboard .press_and_release ('ctrl+v' )
377+ self .on_hotkey_stoptrans ()
378+
326379 def capture (self ):
327380 self .shot_window = ScreenShotWindow ()
328381 self .shot_window .text_window_open_or_close_triggered .connect (self .on_hotkey_stoptrans )
0 commit comments