= AutoHotkey Examples =
== Installation ==
[[https://autohotkey.com/download/|Download]] and run as a portable app:
REM --- Run as Default User ---
C:\apps\AutoHotkey> AutoHotkeyU64.exe .\scripts\AutoHotkey.ahk
Some applications require that AutoHotkey runs as ''admin'' before getting full access to the target application window:
REM --- Run as Admin ---
C:\apps\AutoHotkey> RUNAS /PROFILE /USER:administrator "AutoHotkeyU64.exe .\scripts\AutoHotkey.ahk"
== Actions ==
=== Click ===
Using ControlClick:
ControlClick, x300 y15, ahk_exe emuapp32.exe ; coordinates relative to client
ControlClick, x310 y65, ahk_exe emuapp32.exe,,,, NA ; coordinates relative to Window
ControlClick, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x300 y15 ; coordinates relative to Client
; Check for errors (i.e. it's not blank or zero.)
if ErrorLevel {
MsgBox, The ControlClick failed.
} else {
MsgBox, The ControlClick succeeded.
}
Using mouse clicks:
; Click method:
Click 310, 65 ; coordinates relative to Window
; MouseClick method:
MouseClick, left, 310, 65 ; coordinates relative to Window
Using menu clicks:
Menu, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x300 y15 ; coordinates relative to client
=== Keyboard ===
Send, {LAlt}e ; menu: E - Edit
Send, {LAlt down}e{LAlt up} ; menu: E - Edit
; Ctrl-C to copy using Send
SetKeyDelay, 500, 500 ; delay after each keystroke sent by Send and ControlSend
Send, {ctrl down}c{ctrl up} ; Copy using send
; Ctrl-C to copy using SendInput
SendInput {ctrl down}c{ctrl up}
; Ctrl-C to copy using ControlSend
ControlSend, , {ctrl down}c{ctrl up}, ahk_exe myapp.exe
=== Messages or Dialogs ===
Send a message with ''MsgBox'':
; Format:
; MsgBox, Text
; MsgBox [, Options, Title, Text, Timeout]
MsgBox, 4, "Yes/No", "Do you want to continue?", 5 ; Yes/No dialog with 5 sec timeout
IfMsgBox No {
return
}
; Show mouse coordinates
MouseGetPos, MouseX, MouseY
MsgBox The current mouse cursor position is X%MouseX% Y%MouseY%.
Send a message with ''SplashTextOn'':
; Format:
; SplashTextOn [, Width, Height, Title, Text]
SplashTextOn, , , Displays only a title bar.
Sleep, 2000
SplashTextOn, 400, 300, Clipboard, The clipboard contains:`n%clipboard%
WinMove, Clipboard, , 0, 0 ; Move the splash window to the top left corner.
MsgBox, Press OK to dismiss the SplashText
SplashTextOff
Send a messag with ''ToolTip'':
ToolTip, Hello World!!!, 100, 15 ; display ToolTip
Sleep, 5000 ; pause program execution for 5 secs
ToolTip, , 100, 15 ; clear ToolTip
=== Run Application ===
Running an application:
Run, C:\Program Files (x86)\es32-Bosanova\EmuApp32.exe
Adding a delay:
sleep, 500 ; 1/2 second delay
Check if window is active:
if WinExist("ahk_exe notepad.exe") { ; Select window NOTEPAD
WinActivate ; use the window found above
; Display window name
WinGet, OutputVar, ProcessName
MsgBox, %OutputVar%
; Retrieve which control of the target window has input focus, if any.
ControlGetFocus, OutputVar
MsgBox, %OutputVar%
}
== Example Script ==
Create a script ''AutoHotkey.ahk'':
;-------------------------------------------------------------------------------
; Main loop
;-------------------------------------------------------------------------------
^j:: ; Control+J hotkey
getOrderDataFromSystem36()
return
^!m:: ; Control+Alt+M hotkey.
getNewNotepadTab()
return
^!z:: ; Control+Alt+Z hotkey.
getMouseClick()
return
;-------------------------------------------------------------------------------
; Get order data from System36
;-------------------------------------------------------------------------------
getOrderDataFromSystem36()
{
if WinExist("ahk_exe emuapp32.exe") { ; Select window ES32 Emulator
WinActivate ; use the window found above
; --- Select All (using Mouse clicks on menu) ---
; MouseClick, left, 55, 40 ; Menu: Edit
; MouseClick, left, 100, 140 ; Menu: Edit > Select All
; MouseClick, left, 55, 40 ; Menu: Edit
; MouseClick, left, 90, 65 ; Menu: Edit > Copy
; Sleep, 500
; ;Send, {LAlt}e ; menu: E - Edit
; ;Send, {LAlt down}e{LAlt up} ; menu: E - Edit
; ;Sleep, 1000
; ;Send, e ; menu: E - Edit
; ;Sleep, 1000
; ;Send, s ; menu option: Select All
;
; ; SetKeyDelay, 500, 500 ; delay after each keystroke sent by Send and ControlSend
; ;Send, {ctrl down}c{ctrl up} ; Copy using send
; ;SendInput {ctrl down}c{ctrl up} ; Copy using SendInput
;
; --- Select All (using click on toolbar) ---
SetControlDelay, 0 ; smallest possible delay
ControlClick, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x300 y15 ; coordinates relative to client
Sleep, 500
if ErrorLevel { ; i.e. it's not blank or zero.
;MsgBox, The ControlClick for ToolbarWindow322 ... 'Select All' failed.
ToolTip, The ControlClick for ToolbarWindow322 ... 'Select All' failed., 100, 15
} else {
;MsgBox, The ControlClick for ToolbarWindow322 ... 'Select All' succeeded.
ToolTip, The ControlClick for ToolbarWindow322 ... 'Select All' succeeded., 100, 15
}
; ;Click 310, 66
; MouseClick, left, 310, 65
Sleep, 500
ToolTip, , 100, 15 ; clear ToolTip
;Menu, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x300 y15 ; coordinates relative to client
; --- Copy to Clipboard (using click on toolbar) ---
emptyClipboard()
; ;ControlClick, x325 y15, ahk_exe emuapp32.exe ; client
;ControlClick, x335 y70, ahk_exe emuapp32.exe,,,, NA ; coordinates relative to client
;ControlClick, ToolbarWindow322, Click, 1,
ControlClick, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x230 y15 ; coordinates relative to client
; ;Click 335, 15
; MouseClick, left, 335, 15
Sleep, 500
; ;ControlSend, , {ctrl down}c{ctrl up}, ahk_exe emuapp32.exe
; --- Copy to Clipboard (using Keyboard) ---
;copyToClipboard()
; --- Clear Selection (using click on toolbar) ---
;ControlClick, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x325 y15 ; coordinates relative to client
; --- Paste from Clipboard ---
pasteFromClipboard()
} else {
;MsgBox, Unable to open EmuApp32.exe
Run, C:\Program Files (x86)\es32-Bosanova\EmuApp32.exe
}
}
;-------------------------------------------------------------------------------
; Get a new tab in Notepad++ editor
;-------------------------------------------------------------------------------
getNewNotepadTab()
{
if WinExist("ahk_exe notepad++.exe") { ; Select window ES32 Emulator
WinActivate ; use the window found above
; Display window name
;WinGet, OutputVar, ProcessName
;MsgBox, %OutputVar%
; Call File > New (using Mouse)
;MouseClick, left, 25, 45 ; Menu: File
;MouseClick, left, 45, 65 ; Menu: New
; Call File > New (using Keyboard)
SendInput {ctrl down}n{ctrl up} ; new tab (ctrl-N)
} else {
OutputVar := "Not able to find Notepad++"
MsgBox, %OutputVar%
}
}
;-------------------------------------------------------------------------------
; Empty clipboard
;-------------------------------------------------------------------------------
emptyClipboard()
{
clipboard = ; Empty the clipboard
}
;-------------------------------------------------------------------------------
; Copy data to clipboard
;-------------------------------------------------------------------------------
copyToClipboard()
{
emptyClipboard()
;Send, ^c
Send, {ctrl down}c{ctrl up}
ClipWait, 2
if ErrorLevel {
MsgBox, The attempt to copy text onto the clipboard failed (error %ErrorLevel%).
return
}
MsgBox, clipboard = %clipboard%
}
;-------------------------------------------------------------------------------
; Paste data from clipboard
;-------------------------------------------------------------------------------
pasteFromClipboard()
{
;if WinExist("Untitled - Notepad") { ; Select window using win name
if WinExist("ahk_exe notepad.exe") { ; Select window using app exe
WinActivate ; use the window found above
MsgBox Found Notepad
} else {
Run, Notepad.exe
MsgBox Running Notepad
}
Sleep, 500
;Send, %clipboard%.
;SendInput, {ctrl down}v{ctrl up}
Send, {ctrl down}v{ctrl up}
}
;-------------------------------------------------------------------------------
; Get and display mouse click coordinates
;-------------------------------------------------------------------------------
getMouseClick()
{
MouseGetPos, MouseX, MouseY
;PixelGetColor, color, %MouseX%, %MouseY%
;MsgBox The color at the current cursor position is %color%.
MsgBox The current mouse cursor position is X%MouseX% Y%MouseY%.
}