@woshichuanqilz
2015-11-01T08:15:01.000000Z
字数 12385
阅读 2865
AHK
1. 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, 2000
Send, HelloWorld
Send, !p
Return
}
!p::
{
Sleep, 2000
Send, HelloWorld
Send, !p
Return
}
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, Go
Gui Add, ActiveX, xm w980 h640 vWB, Shell.Explorer
ComObjConnect(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()
return
class WB_events
{
NavigateComplete2(wb, NewURL)
{
GuiControl,, URL, %NewURL% ; Update the URL edit control.
}
}
GuiClose:
ExitApp
VE_Create()
{
Gui 3:Destroy
Gui 3:Default
Gui 3:+ToolWindow +AlwaysOnTop +LabelVEGui +Resize +MinSize -MaximizeBox
Gui 3:Add, Text, x8 y8 w80 h16 +Right, Variable name:
Gui 3:Font, s9, %dbgTextFont%
Gui 3:Font
Gui 3:Add, Radio, x268 y296 w60 h16 %VE_CRLF%, CR+LF
Gui 3:Show, w336 h320, Variable inspection
; Don't select all the text when the window is shown
SendMessage, 0xB1, 0, 0,, ahk_id %VE_Cont_HWND%
}
Active firefox window
; The Way to actvie firefox or Evernote
#NoTrayIcon
DetectHiddenWindows, on
SetTitleMatchMode, RegEx
WindowClassName=ENMainFrame
IfWinNotExist, - 513278236@qq.com - 印象笔记 ahk_class %WindowClassName%
{
MsgBox 1
run "D:\Program Files\ALTRun\快捷方式\Evernote.exe.lnk"
Return
}
Else
IfWinNotActive, - 513278236@qq.com - 印象笔记 ahk_class %WindowClassName%
{
MsgBox 2
WinActivate, 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 Parent
if (OutputVar = "edit1" || OutputVar = "hh_kwd_vlist1")
ControlFocus, Internet Explorer_Server1, ahk_class HH Parent
else
ControlFocus, edit1, ahk_class HH Parent
return
}
Return
}
9. Little Web Browser
#NoEnv
#NoTrayIcon
#SingleInstance force
Gui Add, Edit, w930 r1 vURL, http://ahkscript.org/boards/
Gui Add, Button, x+6 yp w44 Default, Go
Gui Add, ActiveX, xm w980 h640 vWB, Shell.Explorer ; Notice the ActiveX
ComObjConnect(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, NoHide
WB.Navigate(URL)
return
class 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 mail
body =
(
line1
line2
)
Run, mailto:testing@hotmail.com?Subject=Testing&Body=%body%
13. How to use the Gui Submit
; How to use Submit
Gui, 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, vLastName
Gui, Add, Button, default, OK ; The label ButtonOK (if it exists) will be run when the button is pressed.
Gui, Show,, Simple Input Example
return ; 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,,, Win
return 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 = 0
Numpad0Count = 2
else
{
; 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 = 0
Send, = ; ******* This is the action for the 000 key
}
; In all the above cases, we return without further action:
CalledReentrantly = y
return
}
}
; 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 = 0
CalledReentrantly = 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 = n
return
}
; 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 2
a = 0
!p::
{
MsgBox a=%a%
a = 1
Sleep, 20000
Return
}
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 ErrorLevel
break
StringSplit, word_array, line, %A_Space%, . ;Omits periods.
word_array1 := "i)"word_array1 ; Add the case ignore automately
UserInput := 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:
#Persistent
SetTimer, Alert1, 500
return
Alert1:
IfWinNotExist, Video Conversion, Process Complete
return
; Otherwise:
SetTimer, Alert1, Off ; i.e. the timer turns itself off here.
SplashTextOn, , , The video conversion is finished.
Sleep, 3000
SplashTextOff
return
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, No
break
}
WinWait Loop
Loop
{
WinWaitActive, ahk_class HH Parent
WinRestore, A
WinMove,A,,330,170,1205,770
WinWaitNotActive, 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_loopfield
cmd := A_loopfield
}
This my way is to rep the carriage with another word and then process them all.
; Check_Command() {{{2
Check_Command()
{
if not WinExist("ahk_class TXGuiFoundation")
return
clipboard =
this.focusTXGUI()
Send, {tab}
Send, ^a
Send, ^c
ClipWait, 2
cmds := clipboard
clipboard =
Loop, Parse, cmds, `n, `r
{
if A_loopfield
cmd := 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, drive
static ReadContent := name_no_ext . "is running."
SetTimer, Running, 5000
Return
Running:
MsgBox, 0, , %ReadContent%, 1 ; 3 is the count down time
ReadTheContent(ReadContent)
Return
}
copy file according to the path
; filecopytoclipboard
FileToClipboard(PathToCopy,Method="copy")
{
FileCount:=0
PathLength:=0
; Count files and total string length
Loop,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 list
NumPut(1,pPath+0),pPath += 4 ; fWide = 0 -->ANSI,fWide = 1 -->Unicode
Offset:=0
Loop,Parse,PathToCopy,`n,`r ; Rows are delimited by linefeeds (`r`n).
offset += StrPut(A_LoopField,pPath+offset,StrLen(A_LoopField)+1,"UTF-16") * 2
DllCall("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 = 1
Return % 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.exe
WinWaitActive, ahk_class MSPaintApp,, 2
if ErrorLevel
return
MouseClickDrag, L, 150, 250, 150, 150
MouseClickDrag, L, 150, 150, 200, 100
MouseClickDrag, L, 200, 100, 250, 150
MouseClickDrag, L, 250, 150, 150, 150
MouseClickDrag, L, 150, 150, 250, 250
MouseClickDrag, L, 250, 250, 250, 150
MouseClickDrag, L, 250, 150, 150, 250
MouseClickDrag, L, 150, 250, 250, 250
Gosub label
$Enter::
GuiControlGet, OutputVar, FocusV
if (OutputVar = "RepString")
{
gosub, ButtonOk
Return
}
Return
}
ButtonOK:
{
GuiControlGet, ShortCut
GuiControlGet, RepString
Gui, Destroy
if ShortCut =
{
MsgBox, 0, , The Custom Add will be cancel, 2 ; 3 is the count down time
Return
}
Wait for the keydown in the ahk
KeyWait, a, D
MsgBox 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, No
break
}
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 Gameplaza
SetTitleMatchMode, Regex
CoordMode, Mouse, Screen ; plz notice this statement .
Run, g:\2015-3-26 工作代码\UserScore\运行\Debug\ansi\GamePlaza.exe
WinWaitActive, 用户登录 -- Version 6603
ControlSetText, Edit1, livetest2, 用户登录 -- Version 6603
ControlSetText, Edit2, livetest2, 用户登录 -- Version 6603
Send, {Enter}
WinWaitActive, 博乐视频棋牌游戏平台
Sleep, 2000
;Click 118, 446, 2
Click 118, 446
Sleep, 50
Click 118, 446
Sleep, 2000
Click 178, 267
WinWaitActive, 欢乐斗地主
Click 725, 813
Sleep, 1000
Click 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")
ExitApp
ExitFunc()
{
MsgBox Hello World
Return
}
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 live
Sleep, 2000
}
!p::
{
Pause, toggle
Return
}
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
}