test页首

autohotkey实现鼠标中键翻译选中文字

#SingleInstance, Force
#Include <JSON>

; JSON库: https://github.com/cocobelgica/AutoHotkey-JSON
; 参考小海 cosea https://zhuanlan.zhihu.com/p/103357456
; todo&bug:
; 先将就只提取translation,(得添加base.explain)分行只翻译第一行,其他
; 太长好像翻译不了 包含奇怪的字符可能也不行
; 判断是否有选中文字再执行(获取光标单词?)

removeTip:
    ToolTip
return
CapsLock & `::
ToolTip
return

MButton::
    Clipboard=
    Send, ^c
    ClipWait,2
    If ErrorLevel ;
    {
        MsgBox, errorr
        Return
    }
    word=%Clipboard%
    result:=YouDaoApi(word)
    ; Clipboard=%result% ;json 结果,可以去json.cn看看结构
    value := JSON.Load(result)
    s:=value.translation[1]
    ToolTip, %s%
    SetTimer, removeTip,-1000
Return

YouDaoApi(KeyWord)
{
    KeyWord:=Sub_UrlEncode(KeyWord,"utf-8")
    url:="http://fanyi.youdao.com/fanyiapi.do?keyfrom=xxxxxxxx&key=1360116736&type=data&doctype=json&version=1.1&q=" . KeyWord
    WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
    WebRequest.Open("GET", url)
    WebRequest.Send()
    result := WebRequest.ResponseText
    Return result
}

Sub_UrlEncode(str, enc="UTF-8")
{
    hex := "00", func := "msvcrt\" . (A_IsUnicode ? "swprintf" : "sprintf")
    VarSetCapacity(buff, size:=StrPut(str, enc)), StrPut(str, &buff, enc)
    While (code := NumGet(buff, A_Index - 1, "UChar")) && DllCall(func, "Str", hex, "Str", "%%%02X", "UChar", code, "Cdecl")
        encoded .= hex
    Return encoded
}

/*
json有用的有
中文单词:
translation
base.explains[]
base.phonetic

英文单词:
translation
base.[us-|uk-|]phonetic
base.explains[]
base.phonetic  

长文只有translation
*/
posted @   开饭了没  阅读(303)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

test页脚

点击右上角即可分享
微信分享提示