@woshichuanqilz
2015-11-01T08:15:01.000000Z
字数 12385
阅读 2992
AHK1. Notice that if you haven't packaging the func well the func will exe for a time when the script run
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
; Trigger Itself to make the input loop; This idea come up with me whne I learn the AutoHotKey.chm about the hotkey prefix '$'$!p::{Sleep, 2000Send, HelloWorldSend, !pReturn}!p::{Sleep, 2000Send, HelloWorldSend, !pReturn}
4. Create Two window in a script , multithread can be easliy deal with AutoHotKey_L
; Create two window in a program. The number after the Gui show the different of the Gui; Like here if you use Gui (rather than Gui 3:) in the VE_Create, the second window will not show out.Gui Add, Edit, w930 r1 vURL, http://ahkscript.org/boards/Gui Add, Button, x+6 yp w44 Default, GoGui Add, ActiveX, xm w980 h640 vWB, Shell.ExplorerComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.Gui Show; Continue on to load the initial page:ButtonGo:VE_Create()returnclass WB_events{NavigateComplete2(wb, NewURL){GuiControl,, URL, %NewURL% ; Update the URL edit control.}}GuiClose:ExitAppVE_Create(){Gui 3:DestroyGui 3:DefaultGui 3:+ToolWindow +AlwaysOnTop +LabelVEGui +Resize +MinSize -MaximizeBoxGui 3:Add, Text, x8 y8 w80 h16 +Right, Variable name:Gui 3:Font, s9, %dbgTextFont%Gui 3:FontGui 3:Add, Radio, x268 y296 w60 h16 %VE_CRLF%, CR+LFGui 3:Show, w336 h320, Variable inspection; Don't select all the text when the window is shownSendMessage, 0xB1, 0, 0,, ahk_id %VE_Cont_HWND%}
Active firefox window
; The Way to actvie firefox or Evernote#NoTrayIconDetectHiddenWindows, onSetTitleMatchMode, RegExWindowClassName=ENMainFrameIfWinNotExist, - 513278236@qq.com - 印象笔记 ahk_class %WindowClassName%{MsgBox 1run "D:\Program Files\ALTRun\快捷方式\Evernote.exe.lnk"Return}ElseIfWinNotActive, - 513278236@qq.com - 印象笔记 ahk_class %WindowClassName%{MsgBox 2WinActivate, ahk_class %WindowClassName%WinWaitActive, ahk_class %WindowClassName%}Else{WinMinimize, ahk_class %WindowClassName%Return}Return
6. About the MarkDown HotString Rep

7. Add quick launch to the button
; Go is the name of the Buttton add the symbol before the button name will make the button pressed, when you send !g(Initial)Gui, Add, Button, x+6 yp w44 Default, &Go
8. GuiControlGet And ControlGet Use the AHKInfo and MySpy
; AHK Help ,jump between the two window#IfWinActive, ahk_class HH Parent{Tab::{ControlGetFocus, OutputVar, ahk_class HH Parentif (OutputVar = "edit1" || OutputVar = "hh_kwd_vlist1")ControlFocus, Internet Explorer_Server1, ahk_class HH ParentelseControlFocus, edit1, ahk_class HH Parentreturn}Return}
9. Little Web Browser
#NoEnv#NoTrayIcon#SingleInstance forceGui Add, Edit, w930 r1 vURL, http://ahkscript.org/boards/Gui Add, Button, x+6 yp w44 Default, GoGui Add, ActiveX, xm w980 h640 vWB, Shell.Explorer ; Notice the ActiveXComObjConnect(WB, WB_events) ; Connect WB's events to the WB_events class object.Gui Show; Continue on to load the initial page:ButtonGo:Gui Submit, NoHideWB.Navigate(URL)returnclass WB_events{NavigateComplete2(wb, NewURL){GuiControl,, URL, %NewURL% ; Update the URL edit control.}}GuiClose:ExitApp
10. Dectect any input
use input to solve this Q.
11. Restart the script
; you can use the 'reload' func^!r::Reload ; Assign Ctrl-Alt-R as a hotkey to restart the script.
12. Send Mail
; Just open the mailbody =(line1line2)Run, mailto:testing@hotmail.com?Subject=Testing&Body=%body%
13. How to use the Gui Submit
; How to use SubmitGui, Add, Text,, First name:Gui, Add, Text,, Last name:Gui, Add, Edit, vFirstName ym ; The ym option starts a new column of controls.Gui, Add, Edit, vLastNameGui, Add, Button, default, OK ; The label ButtonOK (if it exists) will be run when the button is pressed.Gui, Show,, Simple Input Examplereturn ; End of auto-execute section. The script is idle until the user does something.GuiClose:ButtonOK:Gui, Submit ; Save the input from the user to each control's associated variable.MsgBox You entered "%FirstName% %LastName%".ExitApp
14.
; Hold the mouse on the taskbar to ajust the volume; What is MouseIsOver, I write it#If MouseIsOver("ahk_class Shell_TrayWnd")WheelUp::Send {Volume_Up}WheelDown::Send {Volume_Down}MouseIsOver(WinTitle) {MouseGetPos,,, Winreturn WinExist(WinTitle . " ahk_id " . Win)}
15. Send the key 3 times convernt to a certain char
Download This Script | Other Sample Scripts | Home#MaxThreadsPerHotkey 5 ; Allow multiple threads for this hotkey.$Numpad0::#MaxThreadsPerHotkey 1; Above: Use the $ to force the hook to be used, which prevents an; infinite loop since this subroutine itself sends Numpad0, which; would otherwise result in a recursive call to itself.SetBatchLines, 100 ; Make it run a little faster in this case.DelayBetweenKeys = 30 ; Adjust this value if it doesn't work.if A_PriorHotkey = %A_ThisHotkey%{if A_TimeSincePriorHotkey < %DelayBetweenKeys%{if Numpad0Count =Numpad0Count = 2 ; i.e. This one plus the prior one.else if Numpad0Count = 0Numpad0Count = 2else{; Since we're here, Numpad0Count must be 2 as set by; prior calls, which means this is the third time the; the key has been pressed. Thus, the hotkey sequence; should fire:Numpad0Count = 0Send, = ; ******* This is the action for the 000 key}; In all the above cases, we return without further action:CalledReentrantly = yreturn}}; Otherwise, this Numpad0 event is either the first in the series; or it happened too long after the first one (e.g. perhaps the; user is holding down the Numpad0 key to auto-repeat it, which; we want to allow). Therefore, after a short delay -- during; which another Numpad0 hotkey event may re-entrantly call this; subroutine -- we'll send the key on through if no reentrant; calls occurred:Numpad0Count = 0CalledReentrantly = n; During this sleep, this subroutine may be reentrantly called; (i.e. a simultaneous "thread" which runs in parallel to the; call we're in now):Sleep, %DelayBetweenKeys%if CalledReentrantly = y ; Another "thread" changed the value.{; Since it was called reentrantly, this key event was the first in; the sequence so should be suppressed (hidden from the system):CalledReentrantly = nreturn}; Otherwise it's not part of the sequence so we send it through normally.; In other words, the *real* Numpad0 key has been pressed, so we want it; to have its normal effect:Send, {Numpad0}return
16. About Thread
; Show 1 when you trigger the hotkey for second time; If you do a little research you will find that the things about the thread, different thread share the same resource for the program; And another thing is that we just get one hot key default#MaxThreadsPerHotkey 2a = 0!p::{MsgBox a=%a%a = 1Sleep, 20000Return}
17. Process the input according to the init file.
; The tech we use here is read the ini file and regular expression to replace the phrase we don't need.InputProcess(ByRef UserInput){Loop{FileReadLine, line, Inputprocess.ini, %A_Index%if ErrorLevelbreakStringSplit, word_array, line, %A_Space%, . ;Omits periods.word_array1 := "i)"word_array1 ; Add the case ignore automatelyUserInput := RegExReplace(UserInput, word_array1, word_array2)}Return}
18. How to get the Active window timely.
; Wait for certain window to active.; Example #2: Wait for a certain window to appear and then alert the user:#PersistentSetTimer, Alert1, 500returnAlert1:IfWinNotExist, Video Conversion, Process Completereturn; Otherwise:SetTimer, Alert1, Off ; i.e. the timer turns itself off here.SplashTextOn, , , The video conversion is finished.Sleep, 3000SplashTextOffreturn
19. ObjBind
The aim of the func is to get the func pointer for certain object.
; getLine like the func pointer here.; What's the meaning of the objbind do you get it now?; So, the func we get is pointing to certain obj.file := FileOpen(A_ScriptFullPath, "r")getLine := ObjBindMethod(file, "ReadLine")MsgBox % %getLine%() ; Shows the first line of this file.
20. Command Parameters for the ahk execuate file.
Loop, %0% ; For each parameter:{param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.MsgBox, 4,, Parameter number %A_Index% is %param%. Continue?IfMsgBox, Nobreak}
WinWait Loop
Loop{WinWaitActive, ahk_class HH ParentWinRestore, AWinMove,A,,330,170,1205,770WinWaitNotActive, ahk_class HH Parent}
This is the Array's method to get the last line of the paragraph.
Loop, Parse, cmds, `n, `r{if A_loopfieldcmd := A_loopfield}
This my way is to rep the carriage with another word and then process them all.
; Check_Command() {{{2Check_Command(){if not WinExist("ahk_class TXGuiFoundation")returnclipboard =this.focusTXGUI()Send, {tab}Send, ^aSend, ^cClipWait, 2cmds := clipboardclipboard =Loop, Parse, cmds, `n, `r{if A_loopfieldcmd := A_loopfield}this.Run_Command(cmd)}
If you want to use a variable in label which was set in the main func, you will need the key word static.
VoicePromptForRunningScript(){; Set the content for read.SplitPath, A_ScriptName, name, dir, ext, name_no_ext, drivestatic ReadContent := name_no_ext . "is running."SetTimer, Running, 5000ReturnRunning:MsgBox, 0, , %ReadContent%, 1 ; 3 is the count down timeReadTheContent(ReadContent)Return}
copy file according to the path
; filecopytoclipboardFileToClipboard(PathToCopy,Method="copy"){FileCount:=0PathLength:=0; Count files and total string lengthLoop,Parse,PathToCopy,`n,`r{FileCount++PathLength+=StrLen(A_LoopField)}pid:=DllCall("GetCurrentProcessId","uint")hwnd:=WinExist("ahk_pid " . pid); 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)hPath := DllCall("GlobalAlloc","uint",0x42,"uint",20 + (PathLength + FileCount + 1) * 2,"UPtr")pPath := DllCall("GlobalLock","UPtr",hPath)NumPut(20,pPath+0),pPath += 16 ; DROPFILES.pFiles = offset of file listNumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->UnicodeOffset:=0Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2DllCall("GlobalUnlock","UPtr",hPath)DllCall("OpenClipboard","UPtr",hwnd)DllCall("EmptyClipboard")DllCall("SetClipboardData","uint",0xF,"UPtr",hPath) ; 0xF = CF_HDROP; Write Preferred DropEffect structure to clipboard to switch between copy/cut operations; 0x42 = GMEM_MOVEABLE(0x2) | GMEM_ZEROINIT(0x40)mem := DllCall("GlobalAlloc","uint",0x42,"uint",4,"UPtr")str := DllCall("GlobalLock","UPtr",mem)if (Method="copy")DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x05)else if (Method="cut")DllCall("RtlFillMemory","UPtr",str,"uint",1,"UChar",0x02)else{DllCall("CloseClipboard")return}DllCall("GlobalUnlock","UPtr",mem)cfFormat := DllCall("RegisterClipboardFormat","Str","Preferred DropEffect")DllCall("SetClipboardData","uint",cfFormat,"UPtr",mem)DllCall("CloseClipboard")return}
double press a certain key
isDoublePress(ms = 300) {Return (A_ThisHotKey = A_PriorHotKey) && (A_TimeSincePriorHotkey <= ms)}
Return the value of the bool expression
MsgBox % FoundPos = 1Return % FoundPos = 1
About the StringLower
StringLower, OutputVar, InputVar [, T]StringUpper, OutputVar, InputVar [, T]
Drag a picture by ahk you will get a little house here.
MouseClickDrag, left, 0, 200, 600, 400; The following example opens MS Paint and draws a little house:Run, mspaint.exeWinWaitActive, ahk_class MSPaintApp,, 2if ErrorLevelreturnMouseClickDrag, L, 150, 250, 150, 150MouseClickDrag, L, 150, 150, 200, 100MouseClickDrag, L, 200, 100, 250, 150MouseClickDrag, L, 250, 150, 150, 150MouseClickDrag, L, 150, 150, 250, 250MouseClickDrag, L, 250, 250, 250, 150MouseClickDrag, L, 250, 150, 150, 250MouseClickDrag, L, 150, 250, 250, 250
Gosub label
$Enter::GuiControlGet, OutputVar, FocusVif (OutputVar = "RepString"){gosub, ButtonOkReturn}Return}ButtonOK:{GuiControlGet, ShortCutGuiControlGet, RepStringGui, Destroyif ShortCut ={MsgBox, 0, , The Custom Add will be cancel, 2 ; 3 is the count down timeReturn}
Wait for the keydown in the ahk
KeyWait, a, DMsgBox Hello World
Get the paras from cmd line
Loop, %0% ; For each parameter:{param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.MsgBox, 4,, Parameter number %A_Index% is %param%. Continue?IfMsgBox, Nobreak}
read the content of the para
Loop, %0% ; For each parameter:{param := %A_Index% ; Fetch the contents of the variable whose name is contained in A_Index.ReadTheContent(param)}return
Plz notice the coordMode
#NoEnv#NoTrayIcon#SingleInstance force; Operate the GameplazaSetTitleMatchMode, RegexCoordMode, Mouse, Screen ; plz notice this statement .Run, g:\2015-3-26 工作代码\UserScore\运行\Debug\ansi\GamePlaza.exeWinWaitActive, 用户登录 -- Version 6603ControlSetText, Edit1, livetest2, 用户登录 -- Version 6603ControlSetText, Edit2, livetest2, 用户登录 -- Version 6603Send, {Enter}WinWaitActive, 博乐视频棋牌游戏平台Sleep, 2000;Click 118, 446, 2Click 118, 446Sleep, 50Click 118, 446Sleep, 2000Click 178, 267WinWaitActive, 欢乐斗地主Click 725, 813Sleep, 1000Click 1052, 1011
plz notice the "loop, read"
Loop, Read, C:\Guest List.txt ; This loop retrieves each line from the file, one at a time.{Array.Insert(A_LoopReadLine) ; Append this line to the array.}
If you interupt the script the OnExit will not be trigger
OnExit("ExitFunc")ExitAppExitFunc(){MsgBox Hello WorldReturn}
Using the < or > to use the left or right modify key
< 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.> Use the right key of the pair.
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
loop, 10{MsgBox liveSleep, 2000}!p::{Pause, toggleReturn}
If the process doesn't exsist then run it.
RunTheProcess:{Process, Exist, altrun.exe ; check to see if AutoHotkey.exe is running{ProgramPath := "D:\Program Files\ALTRun\ALTRun.exe"If ! errorLevel{IfExist, %ProgramPath%Run, %ProgramPath%Return}else{Return}}Return}