This is an old revision of the document!
AutoHotkey Examples
Installation
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: <alt>E - Edit Send, {LAlt down}e{LAlt up} ; menu: <alt>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
:
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 <code> === Run Application === Running an application: <code autoit> 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: <alt>E - Edit ; ;send, {LAlt down}e{LAlt up} ; menu: <alt>E - Edit ; ;sleep, 1000 ; ;send, e ; menu: <alt>E - Edit ; ;sleep, 1000 ; ;send, s ; menu option: Select All ; ; ; SetKeyDelay, 500, 500 ; ;send, {ctrl down}c{ctrl up} ; Copy using send ; ;SendInput {ctrl down}c{ctrl up} ; Copy using SendInput ; ; ; Display window name ; WinGet, OutputVar, ProcessName ; ;MsgBox, "ahk_exe emuapp32.exe" ; ;ControlGetFocus, OutputVar ; MsgBox, %OutputVar% ; --- Select All (using click on toolbar) --- SetControlDelay -1 ;ControlClick, x300 y15, ahk_exe emuapp32.exe ; client ;ControlClick, x310 y65, ahk_exe emuapp32.exe,,,, NA ; using relative coordinates ControlClick, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x305 y15 ; using relative coordinates sleep, 5000 if ErrorLevel { ; i.e. it's not blank or zero. MsgBox, The ControlClick for ToolbarWindow322 ... 'Select All' failed. } else { MsgBox, The ControlClick for ToolbarWindow322 ... 'Select All' succeeded. } ; ;Click 310, 66 ; MouseClick, left, 310, 65 ;sleep, 500 ;Menu, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x305 y15 ; using relative coordinates ; --- Copy (using click on toolbar) --- emptyClipboard() ; ;ControlClick, x325 y15, ahk_exe emuapp32.exe ; client ;ControlClick, x335 y70, ahk_exe emuapp32.exe,,,, NA ; using relative coordinates ;ControlClick, ToolbarWindow322, Click, 1, ControlClick, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x230 y15 ; using relative coordinates ; ;Click 335, 15 ; MouseClick, left, 335, 15 sleep, 5000 ; ; ;ControlSend, , {ctrl down}c{ctrl up}, ahk_exe emuapp32.exe ; --- Clear Selection (using click on toolbar) --- ;ControlClick, ToolbarWindow322, ahk_exe emuapp32.exe,,,, NA x325 y15 ; using relative coordinates ; --- Copy to Clipboard --- ;copyToClipboard() 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% } } ;------------------------------------------------------------------------------- ; Paste data to 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} } ;------------------------------------------------------------------------------- ; Paste 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% } ;------------------------------------------------------------------------------- ; Empty clipboard ;------------------------------------------------------------------------------- emptyClipboard() { clipboard = ; Empty the clipboard } ;------------------------------------------------------------------------------- ; 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%. }