2020年3月

为何人的双眼能看见荒野
因为那是无法想象的景象
在身体上方一寸的高处
是难以触及的沉重希望
坠落的幻肢不再疼痛
任由无形的恶犬黑色冲撞
好让黎明前的文字审判
决定将我从白日之国流放

GoldenDict 在 Linux 上的屏幕取词或许是利用了 X 第二剪贴板的特性,所以当用户选择文本后可以自动在附近弹出一个小按钮,点击后即可查词。而 Windows 上的取词一直不太稳定,唯一稳定的方法是利用剪贴板激活全局热键 Ctrl + C + C,而这对于正在使用剪贴板的情况非常不友好。本次努力是试图利用 AutoHotkey 尽量再现 Linux 上的体验。

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.

; Clip() - Send and Retrieve Text Using the Clipboard
; by berban - updated February 18, 2019
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=62156
Clip(Text="", Reselect="")
{
    Static BackUpClip, Stored, LastClip
    If (A_ThisLabel = A_ThisFunc) {
        If (Clipboard == LastClip)
            Clipboard := BackUpClip
        BackUpClip := LastClip := Stored := ""
    } Else {
        If !Stored {
            Stored := True
            BackUpClip := ClipboardAll ; ClipboardAll must be on its own line
        } Else
            SetTimer, %A_ThisFunc%, Off
        LongCopy := A_TickCount, Clipboard := "", LongCopy -= A_TickCount ; LongCopy gauges the amount of time it takes to empty the clipboard which can predict how long the subsequent clipwait will need
        If (Text = "") {
            SendInput, ^c
            ClipWait, LongCopy ? 0.6 : 0.2, True
        } Else {
            Clipboard := LastClip := Text
            ClipWait, 10
            SendInput, ^v
        }
        SetTimer, %A_ThisFunc%, -700
        Sleep 20 ; Short sleep in case Clip() is followed by more keystrokes such as {Enter}
        If (Text = "")
            Return LastClip := Clipboard
        Else If ReSelect and ((ReSelect = True) or (StrLen(Text) < 3000))
            SendInput, % "{Shift Down}{Left " StrLen(StrReplace(Text, "`r")) "}{Shift Up}"
    }
    Return
    Clip:
    Return Clip()
}

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

CoordMode, mouse, Screen
Gui, New, +AlwaysOnTop -Resize -MaximizeBox -MinimizeBox +MinSize -Caption, Dict
Gui, Add, Button, cBlue gSearch X0 Y0, Search
Gui, Margin, 0, 0
Gui, Show, NA AutoSize, movinggui
settimer, WatchCursor, 1000
return

WatchCursor:
MouseGetPos, oX, oY
WinMove, movinggui, , oX +20, oY + 20
return


OnMessage(0x0201, "WM_LBUTTONDOWN")

WM_LBUTTONDOWN()
{
  If (A_Gui)
    PostMessage, 0xA1, 2
}

Search:
Send, !{Tab}
Sleep, 300
text := Clip()
run "C:\Program Files (x86)\GoldenDict\GoldenDict.exe"  %text%
return

不得不承认 AHK 的脚本语言真的非常反人类。我就随便写写了。

不足之处在于,一个按钮会始终跟随光标,怎样根据需要显示我不会。此外,为了让用户能点击到,设置了一秒的运动延迟。GoldenDict 程序路径以及按钮位置等参数可以根据需要在脚本中修改。

GoldenDict 的弹出窗口可以用命令行调用,这个功能真是隐蔽,此前闻所未闻。

脚本参考了:

在此感谢。