[关闭]
@woshichuanqilz 2015-11-01T08:15:01.000000Z 字数 12385 阅读 2745

AHK 学习笔记

AHK

Recommand Some Great AHK Lib

Edit_Control_Function


1. Notice that if you haven't packaging the func well the func will exe for a time when the script run

  1. 1. #include xx.ahk ; if the ahk file can be exe you should notice the Q.

2. Use global variable to count the active time of a certain Hotkey
3. Trigger Myself

  1. ; Trigger Itself to make the input loop
  2. ; This idea come up with me whne I learn the AutoHotKey.chm about the hotkey prefix '$'
  3. $!p::
  4. {
  5. Sleep, 2000
  6. Send, HelloWorld
  7. Send, !p
  8. Return
  9. }
  10. !p::
  11. {
  12. Sleep, 2000
  13. Send, HelloWorld
  14. Send, !p
  15. Return
  16. }

4. Create Two window in a script , multithread can be easliy deal with AutoHotKey_L

  1. ; Create two window in a program. The number after the Gui show the different of the Gui
  2. ; Like here if you use Gui (rather than Gui 3:) in the VE_Create, the second window will not show out.
  3. Gui Add, Edit, w930 r1 vURL, http://ahkscript.org/boards/
  4. Gui Add, Button, x+6 yp w44 Default, Go
  5. Gui Add, ActiveX, xm w980 h640 vWB, Shell.Explorer
  6. ComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.
  7. Gui Show
  8. ; Continue on to load the initial page:
  9. ButtonGo:
  10. VE_Create()
  11. return
  12. class WB_events
  13. {
  14. NavigateComplete2(wb, NewURL)
  15. {
  16. GuiControl,, URL, %NewURL% ; Update the URL edit control.
  17. }
  18. }
  19. GuiClose:
  20. ExitApp
  21. VE_Create()
  22. {
  23. Gui 3:Destroy
  24. Gui 3:Default
  25. Gui 3:+ToolWindow +AlwaysOnTop +LabelVEGui +Resize +MinSize -MaximizeBox
  26. Gui 3:Add, Text, x8 y8 w80 h16 +Right, Variable name:
  27. Gui 3:Font, s9, %dbgTextFont%
  28. Gui 3:Font
  29. Gui 3:Add, Radio, x268 y296 w60 h16 %VE_CRLF%, CR+LF
  30. Gui 3:Show, w336 h320, Variable inspection
  31. ; Don't select all the text when the window is shown
  32. SendMessage, 0xB1, 0, 0,, ahk_id %VE_Cont_HWND%
  33. }

Active firefox window

  1. ; The Way to actvie firefox or Evernote
  2. #NoTrayIcon
  3. DetectHiddenWindows, on
  4. SetTitleMatchMode, RegEx
  5. WindowClassName=ENMainFrame
  6. IfWinNotExist, - 513278236@qq.com - 印象笔记 ahk_class %WindowClassName%
  7. {
  8. MsgBox 1
  9. run "D:\Program Files\ALTRun\快捷方式\Evernote.exe.lnk"
  10. Return
  11. }
  12. Else
  13. IfWinNotActive, - 513278236@qq.com - 印象笔记 ahk_class %WindowClassName%
  14. {
  15. MsgBox 2
  16. WinActivate, ahk_class %WindowClassName%
  17. WinWaitActive, ahk_class %WindowClassName%
  18. }
  19. Else
  20. {
  21. WinMinimize, ahk_class %WindowClassName%
  22. Return
  23. }
  24. Return

6. About the MarkDown HotString Rep
8.png-12.7kB

7. Add quick launch to the button

  1. ; Go is the name of the Buttton add the symbol before the button name will make the button pressed, when you send !g(Initial)
  2. Gui, Add, Button, x+6 yp w44 Default, &Go

8. GuiControlGet And ControlGet Use the AHKInfo and MySpy

  1. ; AHK Help ,jump between the two window
  2. #IfWinActive, ahk_class HH Parent
  3. {
  4. Tab::
  5. {
  6. ControlGetFocus, OutputVar, ahk_class HH Parent
  7. if (OutputVar = "edit1" || OutputVar = "hh_kwd_vlist1")
  8. ControlFocus, Internet Explorer_Server1, ahk_class HH Parent
  9. else
  10. ControlFocus, edit1, ahk_class HH Parent
  11. return
  12. }
  13. Return
  14. }

9. Little Web Browser

  1. #NoEnv
  2. #NoTrayIcon
  3. #SingleInstance force
  4. Gui Add, Edit, w930 r1 vURL, http://ahkscript.org/boards/
  5. Gui Add, Button, x+6 yp w44 Default, Go
  6. Gui Add, ActiveX, xm w980 h640 vWB, Shell.Explorer ; Notice the ActiveX
  7. ComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.
  8. Gui Show
  9. ; Continue on to load the initial page:
  10. ButtonGo:
  11. Gui Submit, NoHide
  12. WB.Navigate(URL)
  13. return
  14. class WB_events
  15. {
  16. NavigateComplete2(wb, NewURL)
  17. {
  18. GuiControl,, URL, %NewURL% ; Update the URL edit control.
  19. }
  20. }
  21. GuiClose:
  22. ExitApp

10. Dectect any input

  1. use input to solve this Q.

11. Restart the script

  1. ; you can use the 'reload' func
  2. ^!r::Reload ; Assign Ctrl-Alt-R as a hotkey to restart the script.

12. Send Mail

  1. ; Just open the mail
  2. body =
  3. (
  4. line1
  5. line2
  6. )
  7. Run, mailto:testing@hotmail.com?Subject=Testing&Body=%body%

13. How to use the Gui Submit

  1. ; How to use Submit
  2. Gui, Add, Text,, First name:
  3. Gui, Add, Text,, Last name:
  4. Gui, Add, Edit, vFirstName ym ; The ym option starts a new column of controls.
  5. Gui, Add, Edit, vLastName
  6. Gui, Add, Button, default, OK ; The label ButtonOK (if it exists) will be run when the button is pressed.
  7. Gui, Show,, Simple Input Example
  8. return ; End of auto-execute section. The script is idle until the user does something.
  9. GuiClose:
  10. ButtonOK:
  11. Gui, Submit ; Save the input from the user to each control's associated variable.
  12. MsgBox You entered "%FirstName% %LastName%".
  13. ExitApp

14.

  1. ; Hold the mouse on the taskbar to ajust the volume
  2. ; What is MouseIsOver, I write it
  3. #If MouseIsOver("ahk_class Shell_TrayWnd")
  4. WheelUp::Send {Volume_Up}
  5. WheelDown::Send {Volume_Down}
  6. MouseIsOver(WinTitle) {
  7. MouseGetPos,,, Win
  8. return WinExist(WinTitle . " ahk_id " . Win)
  9. }

15. Send the key 3 times convernt to a certain char

  1. Download This Script | Other Sample Scripts | Home
  2. #MaxThreadsPerHotkey 5 ; Allow multiple threads for this hotkey.
  3. $Numpad0::
  4. #MaxThreadsPerHotkey 1
  5. ; Above: Use the $ to force the hook to be used, which prevents an
  6. ; infinite loop since this subroutine itself sends Numpad0, which
  7. ; would otherwise result in a recursive call to itself.
  8. SetBatchLines, 100 ; Make it run a little faster in this case.
  9. DelayBetweenKeys = 30 ; Adjust this value if it doesn't work.
  10. if A_PriorHotkey = %A_ThisHotkey%
  11. {
  12. if A_TimeSincePriorHotkey < %DelayBetweenKeys%
  13. {
  14. if Numpad0Count =
  15. Numpad0Count = 2 ; i.e. This one plus the prior one.
  16. else if Numpad0Count = 0
  17. Numpad0Count = 2
  18. else
  19. {
  20. ; Since we're here, Numpad0Count must be 2 as set by
  21. ; prior calls, which means this is the third time the
  22. ; the key has been pressed. Thus, the hotkey sequence
  23. ; should fire:
  24. Numpad0Count = 0
  25. Send, = ; ******* This is the action for the 000 key
  26. }
  27. ; In all the above cases, we return without further action:
  28. CalledReentrantly = y
  29. return
  30. }
  31. }
  32. ; Otherwise, this Numpad0 event is either the first in the series
  33. ; or it happened too long after the first one (e.g. perhaps the
  34. ; user is holding down the Numpad0 key to auto-repeat it, which
  35. ; we want to allow). Therefore, after a short delay -- during
  36. ; which another Numpad0 hotkey event may re-entrantly call this
  37. ; subroutine -- we'll send the key on through if no reentrant
  38. ; calls occurred:
  39. Numpad0Count = 0
  40. CalledReentrantly = n
  41. ; During this sleep, this subroutine may be reentrantly called
  42. ; (i.e. a simultaneous "thread" which runs in parallel to the
  43. ; call we're in now):
  44. Sleep, %DelayBetweenKeys%
  45. if CalledReentrantly = y ; Another "thread" changed the value.
  46. {
  47. ; Since it was called reentrantly, this key event was the first in
  48. ; the sequence so should be suppressed (hidden from the system):
  49. CalledReentrantly = n
  50. return
  51. }
  52. ; Otherwise it's not part of the sequence so we send it through normally.
  53. ; In other words, the *real* Numpad0 key has been pressed, so we want it
  54. ; to have its normal effect:
  55. Send, {Numpad0}
  56. return

16. About Thread

  1. ; Show 1 when you trigger the hotkey for second time
  2. ; If you do a little research you will find that the things about the thread, different thread share the same resource for the program
  3. ; And another thing is that we just get one hot key default
  4. #MaxThreadsPerHotkey 2
  5. a = 0
  6. !p::
  7. {
  8. MsgBox a=%a%
  9. a = 1
  10. Sleep, 20000
  11. Return
  12. }

17. Process the input according to the init file.

  1. ; The tech we use here is read the ini file and regular expression to replace the phrase we don't need.
  2. InputProcess(ByRef UserInput)
  3. {
  4. Loop
  5. {
  6. FileReadLine, line, Inputprocess.ini, %A_Index%
  7. if ErrorLevel
  8. break
  9. StringSplit, word_array, line, %A_Space%, . ;Omits periods.
  10. word_array1 := "i)"word_array1 ; Add the case ignore automately
  11. UserInput := RegExReplace(UserInput, word_array1, word_array2)
  12. }
  13. Return
  14. }

18. How to get the Active window timely.

  1. ; Wait for certain window to active.
  2. ; Example #2: Wait for a certain window to appear and then alert the user:
  3. #Persistent
  4. SetTimer, Alert1, 500
  5. return
  6. Alert1:
  7. IfWinNotExist, Video Conversion, Process Complete
  8. return
  9. ; Otherwise:
  10. SetTimer, Alert1, Off ; i.e. the timer turns itself off here.
  11. SplashTextOn, , , The video conversion is finished.
  12. Sleep, 3000
  13. SplashTextOff
  14. return

19. ObjBind
The aim of the func is to get the func pointer for certain object.

  1. ; getLine like the func pointer here.
  2. ; What's the meaning of the objbind do you get it now?
  3. ; So, the func we get is pointing to certain obj.
  4. file := FileOpen(A_ScriptFullPath, "r")
  5. getLine := ObjBindMethod(file, "ReadLine")
  6. MsgBox % %getLine%() ; Shows the first line of this file.

20. Command Parameters for the ahk execuate file.

  1. Loop, %0% ; For each parameter:
  2. {
  3. param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
  4. MsgBox, 4,, Parameter number %A_Index% is %param%. Continue?
  5. IfMsgBox, No
  6. break
  7. }

WinWait Loop

  1. Loop
  2. {
  3. WinWaitActive, ahk_class HH Parent
  4. WinRestore, A
  5. WinMove,A,,330,170,1205,770
  6. WinWaitNotActive, ahk_class HH Parent
  7. }

Plz compare the to method.

This is the Array's method to get the last line of the paragraph.

  1. Loop, Parse, cmds, `n, `r
  2. {
  3. if A_loopfield
  4. cmd := A_loopfield
  5. }

This my way is to rep the carriage with another word and then process them all.

  1. ; Check_Command() {{{2
  2. Check_Command()
  3. {
  4. if not WinExist("ahk_class TXGuiFoundation")
  5. return
  6. clipboard =
  7. this.focusTXGUI()
  8. Send, {tab}
  9. Send, ^a
  10. Send, ^c
  11. ClipWait, 2
  12. cmds := clipboard
  13. clipboard =
  14. Loop, Parse, cmds, `n, `r
  15. {
  16. if A_loopfield
  17. cmd := A_loopfield
  18. }
  19. this.Run_Command(cmd)
  20. }

If you want to use a variable in label which was set in the main func, you will need the key word static.

  1. VoicePromptForRunningScript()
  2. {
  3. ; Set the content for read.
  4. SplitPath, A_ScriptName, name, dir, ext, name_no_ext, drive
  5. static ReadContent := name_no_ext . "is running."
  6. SetTimer, Running, 5000
  7. Return
  8. Running:
  9. MsgBox, 0, , %ReadContent%, 1 ; 3 is the count down time
  10. ReadTheContent(ReadContent)
  11. Return
  12. }

copy file according to the path

  1. ; filecopytoclipboard
  2. FileToClipboard(PathToCopy,Method="copy")
  3. {
  4. FileCount:=0
  5. PathLength:=0
  6. ; Count files and total string length
  7. Loop,Parse,PathToCopy,`n,`r
  8. {
  9. FileCount++
  10. PathLength+=StrLen(A_LoopField)
  11. }
  12. pid:=DllCall("GetCurrentProcessId","uint")
  13. hwnd:=WinExist("ahk_pid " . pid)
  14. ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
  15. hPath := DllCall("GlobalAlloc","uint",0x42,"uint",20 + (PathLength + FileCount + 1) * 2,"UPtr")
  16. pPath := DllCall("GlobalLock","UPtr",hPath)
  17. NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file list
  18. NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
  19. Offset:=0
  20. Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
  21. offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2
  22. DllCall("GlobalUnlock","UPtr",hPath)
  23. DllCall("OpenClipboard","UPtr",hwnd)
  24. DllCall("EmptyClipboard")
  25. DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP
  26. ; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations
  27. ; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)
  28. mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")
  29. str := DllCall("GlobalLock","UPtr",mem)
  30. if (Method="copy")
  31. DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)
  32. else if (Method="cut")
  33. DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)
  34. else
  35. {
  36. DllCall("CloseClipboard")
  37. return
  38. }
  39. DllCall("GlobalUnlock","UPtr",mem)
  40. cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")
  41. DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)
  42. DllCall("CloseClipboard")
  43. return
  44. }

double press a certain key

  1. isDoublePress(ms = 300) {
  2. Return (A_ThisHotKey = A_PriorHotKey) && (A_TimeSincePriorHotkey <= ms)
  3. }

Return the value of the bool expression

  1. MsgBox % FoundPos = 1
  2. Return % FoundPos = 1

About the StringLower

  1. StringLower, OutputVar, InputVar [, T]
  2. StringUpper, OutputVar, InputVar [, T]

Drag a picture by ahk you will get a little house here.

  1. MouseClickDrag, left, 0, 200, 600, 400
  2. ; The following example opens MS Paint and draws a little house:
  3. Run, mspaint.exe
  4. WinWaitActive, ahk_class MSPaintApp,, 2
  5. if ErrorLevel
  6. return
  7. MouseClickDrag, L, 150, 250, 150, 150
  8. MouseClickDrag, L, 150, 150, 200, 100
  9. MouseClickDrag, L, 200, 100, 250, 150
  10. MouseClickDrag, L, 250, 150, 150, 150
  11. MouseClickDrag, L, 150, 150, 250, 250
  12. MouseClickDrag, L, 250, 250, 250, 150
  13. MouseClickDrag, L, 250, 150, 150, 250
  14. MouseClickDrag, L, 150, 250, 250, 250

Gosub label

  1. $Enter::
  2. GuiControlGet, OutputVar, FocusV
  3. if (OutputVar = "RepString")
  4. {
  5. gosub, ButtonOk
  6. Return
  7. }
  8. Return
  9. }
  10. ButtonOK:
  11. {
  12. GuiControlGet, ShortCut
  13. GuiControlGet, RepString
  14. Gui, Destroy
  15. if ShortCut =
  16. {
  17. MsgBox, 0, , The Custom Add will be cancel, 2 ; 3 is the count down time
  18. Return
  19. }

Wait for the keydown in the ahk

  1. KeyWait, a, D
  2. MsgBox Hello World

Get the paras from cmd line

  1. Loop, %0% ; For each parameter:
  2. {
  3. param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
  4. MsgBox, 4,, Parameter number %A_Index% is %param%. Continue?
  5. IfMsgBox, No
  6. break
  7. }

read the content of the para

  1. Loop, %0% ; For each parameter:
  2. {
  3. param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.
  4. ReadTheContent(param)
  5. }
  6. return

Plz notice the coordMode

  1. #NoEnv
  2. #NoTrayIcon
  3. #SingleInstance force
  4. ; Operate the Gameplaza
  5. SetTitleMatchMode, Regex
  6. CoordMode, Mouse, Screen ; plz notice this statement .
  7. Run, g:\2015-3-26 工作代码\UserScore\运\Debug\ansi\GamePlaza.exe
  8. WinWaitActive, 用户登录 -- Version 6603
  9. ControlSetText, Edit1, livetest2, 用户登录 -- Version 6603
  10. ControlSetText, Edit2, livetest2, 用户登录 -- Version 6603
  11. Send, {Enter}
  12. WinWaitActive, 博乐视频棋牌游戏平台
  13. Sleep, 2000
  14. ;Click 118, 446, 2
  15. Click 118, 446
  16. Sleep, 50
  17. Click 118, 446
  18. Sleep, 2000
  19. Click 178, 267
  20. WinWaitActive, 欢乐斗地主
  21. Click 725, 813
  22. Sleep, 1000
  23. Click 1052, 1011

plz notice the "loop, read"

  1. Loop, Read, C:\Guest List.txt ; This loop retrieves each line from the file, one at a time.
  2. {
  3. Array.Insert(A_LoopReadLine) ; Append this line to the array.
  4. }

If you interupt the script the OnExit will not be trigger

  1. OnExit("ExitFunc")
  2. ExitApp
  3. ExitFunc()
  4. {
  5. MsgBox Hello World
  6. Return
  7. }

Using the < or > to use the left or right modify key

  1. < Use the left key of the pair. e.g. <!a is the same as !a except that only the left Alt key will trigger it.
  2. > Use the right key of the pair.

  1. When {Blind} is the first item in the string, the program avoids releasing Alt/Control/Shift/Win if they started out in the down position. For example, the hotkey +s::Send {Blind}abc would send ABC rather than abc because the user is holding down the Shift key.

If you want to send Click withou t moving the mouse , the function controlclick should be your first option


It's a very useful trick to pause the script when you exe a long time series work

  1. loop, 10
  2. {
  3. MsgBox live
  4. Sleep, 2000
  5. }
  6. !p::
  7. {
  8. Pause, toggle
  9. Return
  10. }

If the process doesn't exsist then run it.

  1. RunTheProcess:
  2. {
  3. Process, Exist, altrun.exe ; check to see if AutoHotkey.exe is running
  4. {
  5. ProgramPath := "D:\Program Files\ALTRun\ALTRun.exe"
  6. If ! errorLevel
  7. {
  8. IfExist, %ProgramPath%
  9. Run, %ProgramPath%
  10. Return
  11. }
  12. else
  13. {
  14. Return
  15. }
  16. }
  17. Return
  18. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注