🌾 💐 🌷 🌹
我的AHK下載地址:https://github.com/dragon8github/Pandora/raw/master/pandora.exe
AutoHotKey 下載:https://autohotkey.com/download/
國內自制的ahk網站:https://www.autoahk.com/
推薦下載installer
官方網站:https://www.autohotkey.com/docs/AutoHotkey.htm
中文官網:https://wyagd001.github.io/zh-cn/docs/Tutorial.htm
AHK腳本編輯器推薦:http://fincs.ahk4.net/scite4ahk/ | https://wyagd001.github.io/zh-cn/docs/commands/Edit.htm#Editors
個人推薦使用sublime text作為autohotkey的編輯器,只需要安裝aotuhotkey插件即可。
常量列表,十分實用:https://wyagd001.github.io/zh-cn/docs/Variables.htm#DD
優秀的腳本合集:https://www.autoahk.com/archives/1444
特殊鍵解決方案(實用):https://blog.csdn.net/liuyukuan/article/details/5924137 https://wyagd001.github.io/zh-cn/docs/KeyList.htm#SpecialKeys
GUI的一些布局API:https://wyagd001.github.io/zh-cn/docs/commands/Gui.htm#PosSize
使用html做ahk界面:https://blog.csdn.net/liuyukuan/article/details/53504400
(重要)使用SciTE4AutoHotkey,出現中文怪異的問題。
左上角File--Encoding---UTF-8 with BOM
1、代碼加入 #InstallKeybdHook,並且開啟腳本
2、左鍵點擊右下角的圖標 -> View -> Key History
將ahk編譯成exe:http://ahkcn.sourceforge.net/docs/Scripts.htm#ahk2exe
快捷鍵
Symbol | Description |
---|---|
# | Win (Windows logo key) |
! | Alt |
^ | Control / Ctrl |
+ | Shift |
& | An ampersand may be used between any two keys or mouse buttons to combine them into a custom hotkey. |
默認是左側的,如果想要右側的加入< >即可,譬如按下右側的ctrl , 那就是 >^
更多熱鍵請參考:
(重要,推薦)https://autohotkey.com/docs/Hotkeys.htm
https://autohotkey.com/docs/Hotkeys.htm
https://autohotkey.com/docs/commands/Send.htm
清空換行符和空格符
fuck(value) { return Trim(StrReplace(StrReplace(StrReplace(value, "`r"), "`n"), "`r`n")) }
獲取圖片大小(根據圖片路徑)
objImage := ComObjCreate("WIA.ImageFile") objImage.LoadFile(path) MsgBox, % objImage.Width MsgBox, % objImage.Height
將桌面的圖片上傳到阿里雲圖床
http://likeyunba.com/imgupload/
先引入上述的ahk文件。
; 將里面的 uploadfile 注釋掉 #Include src/uploadFile.ahk ; 自定義上傳配置 uploadfile(objParam){ CreateFormData(PostData, hdr_ContentType, objParam) whr := ComObjCreate("WinHttp.WinHttpRequest.5.1") whr.Open("POST", "https://api.uomg.com/api/image.ali", true) ;地址 whr.SetRequestHeader("Content-Type", hdr_ContentType) whr.Send(PostData) whr.WaitForResponse() return whr.ResponseText } !z:: path := A_Desktop . "\fuckyou.png" ; 注意,這里的路徑必須是數組格式。哪怕只有一個。 data := uploadfile({ Filedata: [path], file: "multipart" }) _data := JSON.Load(data) Clipboard := _data.imgurl ToolTip, 圖床上傳完畢 return
將剪切板的圖片保存到桌面
https://www.jianshu.com/p/ae048b5090f8
https://github.com/mmikeww/AHKv2-Gdip/blob/master/README.md
#Include src/Gdip.ahk createPic(PicPath) { ; Start gdi+ pToken := Gdip_Startup() ;pBitmapAlpha := Gdip_CreateBitmapFromFile(PicPath) ;pBitmapAlpha := Gdip_BitmapFromScreen(0, "") ;pBitmapAlpha := Gdip_BitmapFromScreen(x "|" y "|" width "|" height) ;從剪切板直接獲取位圖 pBitmapAlpha := Gdip_CreateBitmapFromClipboard() ;圖片的寬度 ImgWidth := Gdip_GetImageWidth(pBitmapAlpha) ;圖片的高度 ImgHeight := Gdip_GetImageHeight(pBitmapAlpha) ;保存圖片到指定的位置;第三個參數控制圖片質量 Gdip_SaveBitmapToFile(pBitmapAlpha, PicPath, "255") Gdip_DisposeImage(pBitmapAlpha) ; close gdi+ Gdip_Shutdown(pToken) Traytip, 截圖完畢:, 寬: %ImgWidth% 高: %ImgHeight%`n保存為: %PicPath% } !z:: ; 先用QQ截圖,然后再執行 !z,桌面就會有一張fuckyou.png savePath := A_Desktop . "\fuckyou.png" createPic(savePath) return
hex 轉換為 rgba
HexToRGB(color) { colorR := SubStr(color, 3, 2) colorG := SubStr(color, 5, 2) colorB := SubStr(color, 7, 2) colorR = 0x%colorR% colorG = 0x%colorG% colorB = 0x%colorB% SetFormat, IntegerFast, D colorR += 0 SetFormat, IntegerFast, D colorG += 0 SetFormat, IntegerFast, D colorB += 0 colorRGB = rgba(%colorR%`, %colorG%`, %colorB%, 1) return colorRGB } !z:: MouseGetPos, posX, posY PixelGetColor, color, %posX%, %posY%, Slow RGB Clipboard := HexToRGB(color) return
通過sc碼來設置熱鍵
https://wyagd001.github.io/zh-cn/docs/KeyList.htm#SpecialKeys
!SC029:: Var = ( ``````javascript `````` ) code(Var) return
獲取進程id,可以通過模糊title來查找。
WinGet, v, PID, , Chrome
Process, Close, % v
動態添加熱鍵
__TIP__(a = 123) { MsgBox, % a } !z:: HFN := Func("__TIP__") Hotstring(":*:fuck", HFN.bind("321")) return
GET請求,解決亂碼問題
; 下載內容 ajax(url, q:=false, text:="正在為你下載代碼,請保持網絡順暢") { whr := ComObjCreate("WinHttp.WinHttpRequest.5.1") whr.Open("GET", url, true) whr.SetRequestHeader("Content-Type", "charset=GB2312") whr.Send() whr.WaitForResponse() if (q==false) { if (whr.ResponseText) { tip("下載成功") } else { tip("無內容返回") } } arr := whr.responseBody pData := NumGet(ComObjValue(arr) + 8 + A_PtrSize) length := arr.MaxIndex() + 1 text := StrGet(pData, length, "utf-8") return text ; return whr.ResponseText }
AHK 解析JSON庫
https://github.com/cocobelgica/AutoHotkey-JSON
#Include src/JSON.ahk ; JSON Script !z:: str = ( { "a": 123 } ) data := JSON.Load(str) MsgBox, % data.a return
注意索引是從1開始的。所以如果是數組,第一個的索引是1
!z:: json_str := ajax("https://gitee.com/api/v5/gists?access_token=6ab92e291bbe59b4301191b6aef2bc85&page=1&per_page=20") data := JSON.Load(json_str) MsgBox, % data[1].url return
AHK 解析和使用JSON,依賴 ActiveScript.ahk
https://github.com/Lexikos/ActiveScript.ahk
#Include src/ActiveScript.ahk jsonstr := "{a: 123}" jsonParse = ( eval('(' + jsonstr + ')') ) script := new ActiveScript("JScript") Result := script.Eval(jscontent) MsgBox, % Result.a
AHK GUI 布局工具 SmartGUI Creator
https://wyagd001.github.io/zh-cn/docs/commands/Gui.htm#Position
兩個關於索引的重要認知:
1、SubStr 字符串截取,如果你想從后面開始取,那么索引應該設置為0,比如SubStr("123", 0, 1);
2、數組的第一位,是從1開始的。惡心吧?
獲取按鈕的狀態
; 左鍵是否按緊了 KeyIsDown := GetKeyState("LButton")
KeyIsDown := GetKeyState("Alt")
KeyIsDown := GetKeyState("Ctrl")
powershell.exe 與 ahk結合
!z:: ; zip名字 zipname := "vue3-template.zip" ; 文件夾名字 zippath := "./vue3-template" ; 下載文件的地址 url := "https://raw.githubusercontent.com/dragon8github/Pandora/master/template/vue3-template.zip" ; 由於要使用git命令,所以要將window格式轉化為unix格式的路徑 desk := StrReplace(A_Desktop, "\", "/") ; 文件夾的名字 name := desk . "/vue3_template_" . A_YYYY . A_MM . A_DD . A_Hour . A_Min . A_Sec ; 一系列命令 command = ( mkdir %name% ; cd %name% ; Invoke-WebRequest -uri "%url%" -OutFile "%zipname%" ; Expand-Archive -Path %zipname% -DestinationPath . ; rm %zipname% ) run, powershell.exe %command% return
熱字符串定義一個很重要的技巧:
::.f:: Var = ( .forEach((val, key) => {}) ) code(Var) return ::f:: Var = ( function () {} ) code(Var) return
我定義了.f 和 f ,但如果我輸出 a.f , 我期待觸發 .f , 但實際上是除非 f
其實這樣這樣設置 .f即可。 :?:.f::
:?:.f:: Var = ( .forEach((val, key) => {}) ) code(Var) return
關閉進程,其實特別實用
https://wyagd001.github.io/zh-cn/docs/commands/Process.htm#ex1
Run notepad.exe,,, NewPID Process, Priority, %NewPID%, High MsgBox The newly launched Notepad's PID is %NewPID%.
全局變量global,居然只能這樣使用?
txtit() { ; 全局變量真的只能這樣用了,定義在外面沒有辦法生存。 global pidary := pidary ? pidary : [] pidary.push("110") return } !x:: txtit() txtit() txtit() MsgBox, % pidary.Length() return
強大的 Spy 探測檢測窗口的信息。
只需要右鍵小圖標,選中 spy window 即可,最好選中右上角的 Follow Mouse 方便選中。我們來實戰一下如何查看搜狗輸入法的窗口
我們得知輸入法的窗口信息為: ‘ahk_class SoPY_Comp’。這樣我就能搞很多事情了。
if (WinExist("ahk_class SoPY_Comp")) { MsgBox, 123 }
后記:win10的輸入法有問題,就算能獲取,Send的時候也很多問題。最大的問題在於哪怕出現輸入法了,還是會在UI中輸出英文,最好的辦法還是使用搜狗等第三方吧。
建議屏蔽win10輸入法:https://jingyan.baidu.com/article/ed2a5d1f99277909f7be1753.html
Run,% "C:\Windows\notepad.exe",,, pid WinWait, ahk_pid %pid% WinMove, ahk_pid %pid%,, 0, 0, (A_ScreenWidth)/3, (A_ScreenHeight) Return
listview 點擊事件(click,rightclick)
必須設置AltSubmit參數才行。
Gui, ISearch:Add, ListView, r7 w800 h600 gMyListView AltSubmit xs yp+40, Name|Path MyListView: if (A_GuiEvent = "Normal") { MsgBox, %A_EventInfo% } if (A_GuiEvent = "RightClick") { MsgBox, %A_EventInfo% } return
GUI的關閉事件
GuiEscape: GuiClose: Gui, Hide return PandoraGuiEscape: PandoraGuiClose: Gui, Pandora:Hide return
獲取GUI Edit的值。
(PS: Pandora: 是我的GUI 的ID,如果沒有可以省略。)
GuiControlGet, OutputVar, Pandora:, SearchContent, Text
數組簡單循環
myarray := ["a", "b", "c", "d"] For key, value in myarray MsgBox %key% = %value%
gui Listview 組件的使用
https://wyagd001.github.io/zh-cn/docs/commands/ListView.htm#Intro
PS: 主要是我的GUI叫 Pandora 才需要加入 Pandora: ,否則移出
; 創建含名稱和大小兩列的 ListView: Gui, Pandora:Add, ListView, r7 w260 gMyListView xs, Name|Path Gui, Pandora:Default ; 從文件夾中獲取文件名列表並把它們放入 ListView: Loop, %A_MyDocuments%\*.* LV_Add("", A_LoopFileName, A_LoopFileSizeKB) LV_ModifyCol() ; 根據內容自動調整每列的大小. LV_ModifyCol(2, "Integer") ; 為了進行排序, 指出列 2 是整數.
SendLevel 控制熱鍵和熱字串是否忽略模擬的鍵盤和鼠標事件.
SendLevel 1 Send, canvas.game{tab}
GuiControlGet 如果有GUI,那么需要這樣用,譬如我的GUI是Pandora
GuiControlGet, OutputVar, Pandora:, ClipHistory, Text
獲取當前是否為中文輸入法
!x:: CoordMode Pixel ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 static/icon/cn.png ImageSearch, FoundX2, FoundY2, 0, 0, A_ScreenWidth, A_ScreenHeight, *25 static/icon/cn2.png if (FoundX != "" || FoundX2 != "") { MsgBox, 目前是中文輸入法 } return
標題和聚焦相關
# 獲取當前活躍窗口標題 WinGetTitle, title, A # 根據title獲取當前的ahk_class WinGetClass, outval, 標題名 # 獲取當前活躍窗口ahk_class WinGetClass, outval, A
無限循環和停止。
ISSTOP := false F5:: Loop { Click Sleep, 10 if (ISSTOP) { break } } return F6:: ISSTOP := true return
用IE打開網站示例
run, iexplore.exe http://120.196.128.45:801/ ExitApp
官方有批量定義快捷鍵的方法,但卻沒有批量定義熱字符串的辦法,文檔又非常不友好,只能艱難的摸索出來。
- 替換熱字符串
- 替換label
- 替換函數方法
// 1、替換熱字符串 Loop, 1000 { Hotstring("::h" . A_Index, "height: " . A_Index . "px;") } // 2、替換為label Loop, 1000 { Hotstring(":X:h" . A_Index, "HEIGHT_HANDLE_LABEL") } HEIGHT_HANDLE_LABEL() { MsgBox, % A_ThisHotkey } // 3、替換函數方法 HEIGHT_HANDLE_LABEL() { MsgBox, % A_ThisHotkey } Loop, 1000 { HFN := Func("HEIGHT_HANDLE_LABEL") Hotstring(":X:h" . A_Index, HFN) }
如何輸出雙引號? 其實就是兩個雙引號即可
Var := "我的名字是 "" Lee "" " MsgBox, % Var
正則表達式獲取子匹配,請注意下面的 OutputVar4 變量
強烈推薦看看:https://autohotkey.com/boards/viewtopic.php?f=27&t=7888&hilit=%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F
Var := "<div class = 'departmental'>" RegExMatch(Var, "im)class(\s*)=(\s*)('|""){1}(.+?)('|""){1}", OutputVar) MsgBox, % OutputVar ; class = 'departmental' MsgBox, % OutputVar4 ; departmental
換行符和循環輸出數組Array
https://wyagd001.github.io/zh-cn/docs/Objects.htm#Usage_Simple_Arrays
https://wyagd001.github.io/zh-cn/docs/misc/Arrays.htm
^!r:: tmp := Clipboard if (StrLen(tmp)) { array := StrSplit(tmp, "`n") For key, value in array MsgBox %key% = %value% } return
執行已定義好的熱字符串,Gosub 或者 Goto
https://wyagd001.github.io/zh-cn/docs/Hotstrings.htm
::ff:: MsgBox, 123 return ::test:: Gosub ::ff return
發送純文本,這對代碼很友好,而且不受輸入法的影響,缺點是一旦使用{text},就無法使用#!+這些了
https://wyagd001.github.io/zh-cn/docs/commands/Send.htm#blind
Send, {text} console.log('') Send, {left 2}
ahk調用cmd示例(暫時不知道如何調用bash)
https://wyagd001.github.io/zh-cn/docs/commands/Run.htm
RunWaitOne(command) {
; WshShell 對象: http://msdn.microsoft.com/en-us/library/aew9yb99
shell := ComObjCreate("WScript.Shell")
; 通過 cmd.exe 執行單條命令
exec := shell.Exec(ComSpec " /C " command)
; 讀取並返回命令的輸出
return exec.StdOut.ReadAll()
}
a := RunWaitOne("node -v")
MsgBox, % a
GUI Submit, NoHide 的作用
https://wyagd001.github.io/zh-cn/docs/commands/Gui.htm#ex2
Gui, Add, Text,, First name: Gui, Add, Text,, Last name: Gui, Add, Edit, vFirstName ym ; ym 選項開始一個新的控件列. Gui, Add, Edit, vLastName Gui, Add, Button, default, OK ; ButtonOK(如果存在)會在此按鈕被按下時運行. Gui, Show,, Simple Input Example return ; 自動運行段結束. 在用戶進行操作前腳本會一直保持空閑狀態. GuiClose: ButtonOK: Gui, Submit, NoHide ; 保存用戶的輸入到每個控件的關聯變量中. MsgBox You entered "%FirstName% %LastName%". ExitApp
GUI 的BUTTON的事件綁定
Gui, Add, Button, w740 h30 Default, FUCK ButtonFUCK: ; 保存用戶的輸入到每個控件的關聯變量中. Gui, Submit, NoHide MsgBox, 123 return
AHK GUI 布局重點教程
核心API:
- ys:新列
- xs:新行,通常結合sction一起使用
- section:形成一個新的塊,通常結合xs一起使用
Gui, Add, Text, gAllSearchA W120 yp+10, 搜索引擎類: Gui, Add, Checkbox, gMySubroutine Checked HwndMyEditHwnd vbd, 百度 Gui, Add, Checkbox, vgoogle, Google Gui, Add, Checkbox, vgithub, Github Gui, Add, Checkbox, vso, Stack Overflow Gui, Add, Checkbox, vsegmentfault, SegmentFault Gui, Add, Checkbox, vcylee, 博客園 Gui, Add, Text, gAllSearchB W120 ys, 翻譯類: Gui, Add, Checkbox, vbdfy, 百度翻譯 Gui, Add, Checkbox, vyoudaofy, 有道翻譯 Gui, Add, Checkbox, vgooglefanyi, Google翻譯 Gui, Add, Text, gAllSearchC W120 ys, 音樂類: Gui, Add, Checkbox, vwy, 網易雲音樂 Gui, Add, Checkbox, vqq, QQ音樂 Gui, Add, Checkbox, vdog, 酷狗音樂 Gui, Add, Checkbox, vxiami, 蝦米音樂 Gui, Add, Text, gAllSearchD W120 ys, 社區類: Gui, Add, Checkbox, vjuejin, 掘金 Gui, Add, Checkbox, vjianshu, 簡書 Gui, Add, Checkbox, vcsdn, CSDN Gui, Add, Checkbox, vzhihu, 知乎 Gui, Add, Text, gAllSearchE W120 ys, 購物類: Gui, Add, Checkbox, vtaobao, 淘寶 Gui, Add, Checkbox, vjingdong, 京東 Gui, Add, Checkbox, vdangdang, 當當 Gui, Add, Checkbox, vamazon, 亞馬遜 Gui, Add, Checkbox, vsuning, 蘇寧易購 Gui, Add, Text, W120 Section xs yp+50, 常用導航: Gui, Add, Link,, <a href="https://github.com">github</a> Gui, Add, Link,, <a href="https://legacy.gitbook.com">gitbook</a> Gui, Add, Link,, <a href="https://www.cnblogs.com/cylee">博客園</a> Gui, Add, Text, W120 ys, 其他: Gui, Add, Link,, <a href="http://youmightnotneedjquery.com/">notjQuery</a> Gui, Add, Link,, <a href="chrome://inspect/#devices">安卓調試</a> Gui, Add, Link,, <a href="https://wyagd001.github.io/zh-cn/docs/Tutorial.htm">AHK官網</a> Gui, Add, Text, W120 ys, 娛樂: Gui, Add, Link,, <a href="https://www.bilibili.com/">嗶哩嗶哩</a> Gui, Add, Link,, <a href="http://www.dilidili.wang/">嘀哩嘀哩</a> Gui, Add, Link,, <a href="http://i.youku.com/u/UNTUzOTAwMzQ0">Ted魔獸</a> Gui, Add, Link,, <a href="http://e.xitu.io/">掘金前端</a> Gui, Add, Text, W120 ys, 代理: Gui, Add, Link,, <a href="http://www.xicidaili.com/nn">西刺</a> Gui, Add, Link,, <a href="https://proxy.l337.tech/txt">l337</a> Gui, Add, Link,, <a href="http://www.66ip.cn/nm.html">66ip</a> Gui, Add, Text, W120 ys, 站長工具: Gui, Add, Link,, <a href="http://tool.oschina.net/codeformat/html">代碼格式化</a> Gui, Add, Link,, <a href="https://tool.lu/html/">代碼美化</a> Gui, Add, Link,, <a href="http://jsbeautifier.org/">jsbeautifier</a> Gui, Add, Link,, <a href="http://tool.chinaz.com/Tools/urlencode.aspx">Urlencode/Unicode</a> Gui, Add, Text, W120 Section xs yp+40, layer/layui: Gui, Add, Link,, <a href="http://layer.layui.com/">layer</a> Gui, Add, Link,, <a href="http://www.layui.com/doc/">layui文檔</a> Gui, Add, Link,, <a href="http://www.layui.com/demo/">layer示例</a> Gui, Add, Link,, <a href="https://github.com/sentsin/layui/">layui-github</a> Gui, Add, Text, W120 ys, Vue: Gui, Add, Link,, <a href="http://vuejs.org/">vue</a> Gui, Add, Link,, <a href="http://vuex.vuejs.org">vuex</a> Gui, Add, Link,, <a href="http://router.vuejs.org ">vue-router</a> Gui, Add, Link,, <a href="https://github.com/opendigg/awesome-github-vue">vue-awesome</a> Gui, Add, Text, W120 ys, ElementUI: Gui, Add, Link,, <a href="http://element-cn.eleme.io/#/zh-CN/component/radio">ElementUI</a> Gui, Add, Link,, <a href="https://github.com/ElemeFE/element/blob/dev/packages/">Element-github</a> Gui, Add, Text, W120 ys, mint-ui: Gui, Add, Link,, <a href="http://elemefe.github.io/mint-ui/#/">mint-ui</a> Gui, Add, Link,, <a href="http://mint-ui.github.io/docs/#/">mint-ui-github</a> Gui, Add, Text, W120 ys, 最近瀏覽: Gui, Add, Link,, <a href="http://guss.one/guss/register.html?code=ec19c0ca">guss</a> Gui, Add, Link,, <a href="http://www.51ym.me/User/Default.aspx">易碼</a> Gui, Add, Link,, <a href="http://www.manbiwang.com/#/">滿幣網</a>
獲取控件GUI控件的對象
再進一步根據API操作就簡單了
Gui, Add, Checkbox, vsuning, 蘇寧易購
; 設置為選中 GuiControl,, suning, 1
設置GUI控件的事件
Gui, Add, Text, gAllSearchD W120 ym, 社區類: AllSearchD: MsgBox, 123return
(hack) 在ahk中使用JavaScript
https://autohotkey.com/boards/viewtopic.php?f=6&t=4555
https://github.com/Lexikos/ActiveScript.ahk
#Include <ActiveScript> script := new ActiveScript("JScript") Result := script.Eval("parseInt(Math.random() * 10 + 1)") MsgBox, % Result
最大化當前窗口和還原當前窗口
!enter:: WinGet, OutputVar, MinMax, A if (OutputVar == 1) { WinRestore, A } else { WinMaximize, A } return
獲取當前桌面路徑:MsgBox, %A_Desktop%
GUI的語法
官方demo:https://wyagd001.github.io/zh-cn/docs/commands/Gui.htm#Examples
官方文檔:https://wyagd001.github.io/zh-cn/docs/commands/Gui.htm
控件列表:https://wyagd001.github.io/zh-cn/docs/commands/Gui.htm#Add
詳細demo:https://www.cnblogs.com/CyLee/p/9198059.html
Gui, Add, Text, W120, 搜索引擎類: Gui, Add, Checkbox, gMySubroutine vbd Checked HwndMyEditHwnd, 百度 Gui, Add, Checkbox, vgoogle, Google Gui, Add, Checkbox, vgithub, Github Gui, Add, Checkbox, vso, Stack Overflow Gui, Add, Text, W120 ym, 翻譯類: Gui, Add, Checkbox, vbdfy, 百度翻譯 Gui, Add, Checkbox, vyoudaofy, 有道翻譯 Gui, Add, Checkbox, vgooglefanyi, Google翻譯 Gui, Add, Text, W120 ym, 音樂類: Gui, Add, Checkbox, vwy, 網易雲音樂 Gui, Add, Checkbox, vqq, QQ音樂 Gui, Add, Checkbox, vdog, 酷狗音樂 Gui, Add, Checkbox, vxiami, 蝦米音樂 Gui, Add, Text, W120 ym, 社區類: Gui, Add, Checkbox, vjuejin, 掘金 Gui, Add, Checkbox, vjianshu, 簡書 Gui, Add, Checkbox, vcsdn, CSDN Gui, Add, Checkbox, vzhihu, 知乎 Gui, Add, Checkbox, vsegmentfault, SegmentFault ; ym 可以 y軸換列,有點類似float:left ,而 xm可以換行,有點類似clear:both Gui, Add, Edit, r9 vFirstName w300 Limit50 ym , 請輸入你要搜索的內容 Gui, Color, E6FFE6 Gui, Margin, 10, 10 Gui, Add, Button, w300 h30, OK Gui, Show,, Simple Input Example return ; +g 其實就是添加吧 MySubroutine: MsgBox, %MyEditHwnd% MsgBox, %A_EventInfo%, %A_GuiEvent%, %A_GuiControl%, %A_Gui% return GuiClose: ButtonOK: Gui, Submit ; 保存用戶的輸入到每個控件的關聯變量中. if (bd == 1) { } return
自定義托盤到圖標
文本插入
~^c:: Clipboard := Send, ^c ClipWait time := A_YYYY . "/" . A_MM . "/" . A_DD . " " . A_Hour . ":" . A_Min . ":" . A_Sec FileAppend, __________________%time%__________________`n`n%Clipboard%`n`n, ./tmp.txt return
字符串操作:去掉空白、去掉換行、獲取長度
if (StrLen(Trim(StrReplace(Clipboard, "`r`n"))) != 0) {}
快速搜索demo
!space:: InputBox, OutputVar, title, what's your Q? if (ErrorLevel == 0) { /* RUN, https://www.zhihu.com/search?type=content&q=%OutputVar% RUN, https://segmentfault.com/search?q=%OutputVar% RUN, https://www.google.com/search?q=%OutputVar% RUN, https://stackoverflow.com/search?q=%OutputVar% */ RUN, https://www.baidu.com/s?wd=%OutputVar% } Return
快速搜索音樂demo
; 快速搜索音樂 !m:: InputBox, OutputVar, title, enter a music name? if (OutputVar != "") { RUN, http://music.163.com/#/search/m/?s=%OutputVar% RUN, https://y.qq.com/portal/search.html#w=%OutputVar% RUN, https://www.xiami.com/search?key=%OutputVar% RUN, http://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord=%OutputVar% } return
(重要) 使用 #include 引入其他代碼段
https://wyagd001.github.io/zh-cn/docs/commands/_Include.htm
; 這里是htinfo.ahk ::test:: MsgBox, 321 return ; 這里是main.ahk #include htinfo.ahk
熱鍵也支持up 和 down到定義
F6 Up:: MsgBox, 123 return
(重要)定義多個熱鍵同個功能
修改托盤圖標,但不能修改編譯好的圖標,其實沒什么意思
Menu, Tray, Icon, ../static/favicon-201806020842523.ico
(重要)其實不需要輸入到文本,也可以觸發的。譬如
::layui:: run, https://github.com/dragon8github/ahk/blob/master/template/layui_template.zip?raw=true return
我們只需要輸入layui 然后按下回車即可,不一定需要輸出到面板。
scite4ahk 編輯器中間的神奇運用:鼠標中鍵列出所有的快捷鍵和定義。包含#include的東西
-12、字符串累加,時間輸出
>^t:: time := A_YYYY . "/" . A_MM . "/" . A_DD . " " . A_Hour . ":" . A_Min . ":" . A_Sec Send, % time return
-11、確認框
MsgBox, 4,, Would you like to continue? (press Yes or No) IfMsgBox Yes MsgBox You pressed Yes. else MsgBox You pressed No.
-10、十分常用的功能,獲取當前定義的快捷鍵A_ThisHotkey
::posa:: SendInput, ( position: absolute`; ) MsgBox, % A_ThisHotkey Return
-9、疊加剪切板
::Test:: clipboard = Var = ( --Start Script sendToAHK = function (key) --print('It was assigned string: ' .. key) local file = io.open("C:\\Users\\TaranWORK\\Documents\\GitHub\\2nd-keyboard-master\\LUAMACROS\\keypressed.txt", "w") -- writing this string to a text file on disk is probably NOT the best method. Feel free to program something better! --Make sure to substitute the path that leads to your own "keypressed.txt" file, using the double backslashes. --print("we are inside the text file") file:write(key) file:flush() --"flush" means "save" file:close() lmc_send_keys('{F24}') -- This presses F24. Using the F24 key to trigger AutoHotKey is probably NOT the best method. Feel free to program something better! end ) RegExMatch(Var, "i)(\b\w+\b)(?CCallout)") Callout(m) { clipboard .= m . "," ; __Var__ = %__Var__% . %m% ;__Array__.Insert(m) ;MsgBox, % __Var__ ;MsgBox m=%m% `n m1=%m1% `n m2=%m2% ; __Var__ .= m return 1 } return
-8、排序
MyVar = 5,3,7,9,1,1,13,999,-4 Sort MyVar, U N D, ; N數值排序, D默認使用逗號作為分隔符, U移除重復項 MsgBox %MyVar% ; 結果是 -4,1,3,5,7,9,13,999*/ Return
-7、累加字符串,和php一樣, .= 即可
Array := [] Array.Insert("d") Array.Insert("a") Array.Insert("b") Array.Insert("c") for index, element in Array ; 在大多數情況下建議使用枚舉的方式. { ; 使用 "Loop", 索引必須是連續的數字, 從 1 到 ; 數組中元素的個數 (或者必須在循環中進行計算). ; MsgBox % "Element number " . A_Index . " is " . Array[A_Index] ; 使用"for",同時提供了索引(或"鍵")及與它關聯 ; 的值,並且索引可以是您選擇的*任何*值. ;MsgBox % "Element number " . index . " is " . element Var .= element MsgBox, % Var }
-6、數組的創建和遍歷
請注意,數組不能直接msg出來,只能通過for遍歷
::arr:: Array := [] Array.Insert("a") Array.Insert("b") Array.Insert("c") Array.Insert("d") for index, element in Array ; 在大多數情況下建議使用枚舉的方式. { ; 使用 "Loop", 索引必須是連續的數字, 從 1 到 ; 數組中元素的個數 (或者必須在循環中進行計算). ; MsgBox % "Element number " . A_Index . " is " . Array[A_Index] ; 使用"for",同時提供了索引(或"鍵")及與它關聯 ; 的值,並且索引可以是您選擇的*任何*值. MsgBox % "Element number " . index . " is " . element } Return
-5、打開文本,等待文本,聚焦文本
+d:: InputBox, OutputVar, title, enter your download url? if (OutputVar != "") { text := ajax(OutputVar) RUN, notepad WinWaitActive, 無標題 - 記事本, , 2 if ErrorLevel { MsgBox, WinWait timed out. } else { ; 這里需要聚焦一下 Winactivate code(text) } } return
-4、正則匹配,這里最難和最坑的其實就是對\b的理解
https://wyagd001.github.io/zh-cn/docs/misc/RegExCallout.htm
::Test:: Var = ( --Start Script sendToAHK = function (key) --print('It was assigned string: ' .. key) local file = io.open("C:\\Users\\TaranWORK\\Documents\\GitHub\\2nd-keyboard-master\\LUAMACROS\\keypressed.txt", "w") -- writing this string to a text file on disk is probably NOT the best method. Feel free to program something better! --Make sure to substitute the path that leads to your own "keypressed.txt" file, using the double backslashes. --print("we are inside the text file") file:write(key) file:flush() --"flush" means "save" file:close() lmc_send_keys('{F24}') -- This presses F24. Using the F24 key to trigger AutoHotKey is probably NOT the best method. Feel free to program something better! end ) ; FoundPos := RegExMatch(Var, "iO)\s(\w+)|(\w+)\s", SubPat) ; MsgBox, % Match ; 保證特征只有一個。那么空格是很好的 ;Haystack = The quick brown fox jumps over the lazy dog. ;RegExMatch(Haystack, "i)(\b\w+\b)(?CCallout)") RegExMatch(Var, "i)(\b\w+\b)(?CCallout)") Callout(m) { MsgBox m=%m% `n m1=%m1% `n m2=%m2% return 1 } return
-3、GUI
https://wyagd001.github.io/zh-cn/docs/commands/Gui.htm
!q:: Var = ( 審查清單 1、確定設計稿的開發友好性(開發成本、無法完美還原效果圖的地方) 2、確定一些特殊的元素是否有合理的邊界處理(如文案超出外層容器的情況) 3、確定頁面的框架結構(Layout) 4、確定跨頁面可復用組件(Site Global Component) 5、確定當前頁面可復用組件(Page Component) ... 把頁面想象成一套房子 - HTML:可以決定網頁架構結構(房子有幾間房,各個區域的用途是什么) - Css:可以決定網頁的樣式和布局(房間的顏色,布局,大小,裝修,主題,色調) - JS:決定頁面具體交互和功能(智能家居,如空調根據溫度自動啟動和調節溫度,燈光自動調節色溫,房門感應自動打開和關閉) ) Gui, font, s14, Microsoft yahei Gui, color, f2f2f2 Gui, Add, Text,, %Var% Gui, Show, W800 H600 X0 Y0 Center AutoSize, 標題 return Return
-2、Send打斷文本的解決方案,利用剪切板和復制黏貼
c(code) { tmp := Clipboard Clipboard := code SendInput, ^V Clipboard := tmp } ::gettop:: Var = ( function getElementTop(element){ try { var actualTop = element.offsetTop; var current = element.offsetParent; while (current !== null){ actualTop += current.offsetTop; current = current.offsetParent; } return actualTop; } catch (e) {} } ) c(Var) Return
-1、網絡下載內容
https://segmentfault.com/a/1190000005041741
::gettop:: whr := ComObjCreate("WinHttp.WinHttpRequest.5.1") ; Open() 的第三個參數代表同步或者異步,現在不用過多關注,true 就可以了 whr.Open("GET", "https://autohotkey.com/download/1.1/version.txt", true) whr.Send() whr.WaitForResponse() version := whr.ResponseText MsgBox, % version Return
::gettop:: whr := ComObjCreate("WinHttp.WinHttpRequest.5.1") ; Open() 的第三個參數代表同步或者異步,現在不用過多關注,true 就可以了 whr.Open("GET", "https://raw.githubusercontent.com/dragon8github/ahk/master/functions/gettop.js", true) whr.Send() TrayTip, 請稍后, 正在為你下載gettop代碼,請保持網絡順暢, 20, 17 whr.WaitForResponse() tmp := Clipboard Clipboard := whr.ResponseText send, ^V Clipboard := tmp TrayTip, 下載成功, (づ ̄3 ̄)づ╭❤~ , 20, 17 Return
1、官方示例
#z::Run https://autohotkey.com ^!n::if WinExist("Untitled - Notepad") WinActivate else Run Notepad return
1.0 、設置終止符(重要)
https://wyagd001.github.io/zh-cn/docs/Hotstrings.htm#EndChars
Hotstring("EndChars", "`n`t ") ; 設置為回車鍵、tab鍵和空格鍵
默認是這個:
Hotstring("EndChars", "-()[]{}:;")
1.1.1 將中文符號強制轉換為英文
; 無視輸入法狀態發送字符串uStr函數 uStr(str) { charList:=StrSplit(str) SetFormat, integer, hex for key,val in charList out.="{U+ " . ord(val) . "}" return out } ,::SendInput % uStr(",") ?::SendInput % uStr("?") .::SendInput % uStr(".") !::SendInput % uStr("!") {::SendInput % uStr("{") }::SendInput % uStr("}") [::SendInput % uStr("[") ]::SendInput % uStr("]") `;::SendInput % uStr(";") >+;::SendInput % uStr(":") (::SendInput % uStr("(") )::SendInput % uStr(")") >+':: a = " SendInput % uStr(a) Return ':: a = ' SendInput % uStr(a) Return
1.1、快速重啟腳本
!r::
send, ^s
reload
Return
1.2、使用常量,獲取年月日
!d:: Send, %A_YYYY%/%A_MM%/%A_DD% Return
1.3、賦值剪切板
!x:: a = fuck you!!!!!! Clipboard = %a% Return
2、快捷輸入
^j::
Send, My First Script
Return
2.1、明白 Send,和 SendInput的區別。前者是慢慢輸入,后者是直接輸入
::dg:: Send, document.getElementById('')`;{left 3} Return ::dg:: SendInput, document.getElementById('')`;{left 3} Return
3、補全
::ftw::Free the whales
輸入 ftw 然后按下【空格、tab、回車、分號,感嘆號等許多鍵時,會自動補全Free the whales】
4、彈出層
esc:: MsgBox Escape!!!! Return
5、彈出層2
::btw:: MsgBox You typed "btw". Return
6、連續
^j:: MsgBox Wow! MsgBox this is Run, Notepad.exe
#IfWinActive 無標題 - 記事本
WinWaitActive, 無標題 - 記事本 Send, 7 lines{!}{enter} SendInput, inside the ctrl{+}j hotkey Return
亮點在於 #IfWinActive 無標題 - 記事本 和 WinWaitActive, 無標題 - 記事本。
前者是判斷當前是否為記事本,后者是等待
7、組合鍵
Numpad0 & Numpad1:: MsgBox You pressed Numpad1 while holding down Numpad0. Return Numpad0 & Numpad2:: Run Notepad Return
同時按下0+1或者0+2試試,就像按Ctrl + C 一樣即可,但必須是右側九宮格
8、案例3的加強版,自動補全不需要其他按鍵
:*:ftw::Free the whales
輸入 ftw就自動變成Free the whales
9、監聽當前窗口,其實案例6就已經展示了。這里再補習一下
#IfWinActive 無標題 - 記事本 #space:: MsgBox You pressed Win+Spacebar in Notepad. Return
新開一個nodepad,然后按下快捷鍵即可。非常實用
10、如何添加注釋
; 這是注釋 #IfWinActive 無標題 - 記事本!q:: MsgBox, You pressed Alt and Q in Notepad. Return #IfWinActive
11、#IfWinActive作用域、代碼塊 下才有效
; Notepad #IfWinActive ahk_class Notepad #space:: MsgBox, You pressed Win+Spacebar in Notepad. Return ::msg::You typed msg in Notepad #IfWinActive
只有在Nodepad下輸入msg + 【enter、space...等鍵】才有效果
12、輸入 j 補全
~j::
Send, ack
Return
13、多個監聽
#i:: Run, http://www.baidu.com/ Return ^p:: Run, notepad.exe Return ~j:: Send, ack Return :*:acheiv::achiev ::achievment::achievement ::acquaintence::acquaintance :*:adquir::acquir ::aquisition::acquisition :*:agravat::aggravat :*:allign::align ::ameria::America
14、組合鍵
^b:: Send, {ctrl down}c{ctrl up} ; 等於按下Ctrl + C ,然后松開Ctrl鍵 SendInput, [b]{ctrl down}v{ctrl up}[/b] ; 輸入[b]然后按下Ctrl + V,松開Ctrl鍵,然后輸入[/b] Return
正式開發不可能那么復雜,而是使用熱鍵,譬如
^b:: Send, ^c ; 等於按下Ctrl + C ,然后松開Ctrl鍵 SendInput, [b]^v[/b] ; 輸入[b]然后按下Ctrl + V,松開Ctrl鍵,然后輸入[/b] Return
15、send換行快速使用enter
>^i:: Send, ( Line 1 Line 2 Apples are a fruit. )
16、函數的使用
Add(x, y) { return x + y } ^j:: Send, % Add(2, 3) Return
17、函數直接Send,表達式,記得要加%
Add(x, y) { Send, % x + y } ^j:: Add(2, 3) Return
18、函數,拼接字符串
Add(x) { return "{{}" x "{}}" } ^j:: Send, % Add("//...") Return
19、新建菜單,大有作為的工具
更多實例請參考:https://wyagd001.github.io/zh-cn/docs/commands/Menu.htm
我的做法和官方的做法不一樣,而且也不符合復用和緩存的理念。但卻非常實用,更關鍵的是能用!如果按照官方的做法是有問題的。在正式實戰的時候
menu,add 默認會去掉重復項,但卻不會去掉分隔符,也就是空值,所以為了方便直接每次顯示完后刪除所有最好了。
!p:: ; Create the popup menu by adding some items to it. Menu, PythonMenu, Add, Item1, PythonHandler Menu, PythonMenu, Add, Item2, PythonHandler Menu, PythonMenu, Add ; Add a separator line. ; Create another menu destined to become a submenu of the above menu. Menu, Submenu1, Add, Item1, PythonHandler Menu, Submenu1, Add, Item2, PythonHandler ; Create a submenu in the first menu (a right-arrow indicator). When the user selects it, the second menu is displayed. Menu, PythonMenu, Add, My Submenu, :Submenu1 Menu, PythonMenu, Add ; Add a separator line below the submenu. Menu, PythonMenu, Add, Item3, MenuHandler ; Add another menu item beneath the submenu. Menu, PythonMenu, Show ; i.e. press the Win-Z hotkey to show the menu. Menu, PythonMenu, DeleteAll return PythonHandler: MsgBox You selected %A_ThisMenuItem% from the menu %A_ThisMenu%. return
20、彈窗獲取用戶輸入
https://wyagd001.github.io/zh-cn/docs/commands/InputBox.htm
#space:: InputBox, OutputVar, title, enter a music name? if (OutputVar!="") MsgBox, That's an awesome name, %OutputVar%.
也可以用 ErrorLevel 來區分用戶點擊的是確認還是取消
InputBox, UserInput, Phone Number, Please enter a phone number., , 640, 480 if ErrorLevel MsgBox, CANCEL was pressed. else MsgBox, You entered "%UserInput%"
21、獲取鼠標位置的顏色
!a:: ; Control+Alt+Z hotkey. MouseGetPos, MouseX, MouseY PixelGetColor, color, %MouseX%, %MouseY% Clipboard = %color% return
22、彈出氣泡提示TrayTip
https://wyagd001.github.io/zh-cn/docs/commands/TrayTip.htm
!a:: MouseGetPos, MouseX, MouseY PixelGetColor, color, %MouseX%, %MouseY% Clipboard = %color% TrayTip, my title, current color is `n %color%, 20, 17 return
23 關閉輸入法
SwitchIME(dwLayout){ HKL:=DllCall("LoadKeyboardLayout", Str, dwLayout, UInt, 1) ControlGetFocus,ctl,A SendMessage,0x50,0,HKL,%ctl%,A } esc:: ; 下方代碼可只保留一個 SwitchIME(0x08040804) ; 英語(美國) 美式鍵盤 ;SwitchIME(0x04090409) ; 中文(中國) 簡體中文-美式鍵盤 return