interact ( string script , long _maxDelay ) : void
Launch an AutoHotKey script to interact with applications (by simulating the click of the mouse and the keys of the keyboard for example). This function is synchronous, i.e. the execution of the Grimport script is interrupted until the AHK script is finished.
Example
interact("""Run, Notepad.exe
SetTitleMatchMode 2
WinWait Untitled
Send, my text
Click 100, 200, right""")
Run notepad, wait a window with a title containing "Untitled" is shown, write "my text", and make a right click download("https://www.idia-tech.com/ressources_importables/FindText.ahk", path("tmp")+"FindText.ahk")
interact("""
#Include """+path("tmp")+"""FindText.ahk
#SingleInstance Force
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.CoordMode, Mouse, Screen
CoordMode, Pixel, Screen
SetTitleMatchMode, 2
WinActivate, MyApp
Sleep 1000
t1:=A_TickCount, X:=Y:=""
Text:="|<>*131\$197.000000001"
if (ok:=FindText(X, Y, 2925-150000, 651-150000, 2925+150000, 651+150000, 0, 0, Text))
{
FindText().Click(X, Y, "L")
}""")
An example of usage of FindText to click on a text (you can obtain the appropriate code using FindText GUI, here a tutorial and a video).
Scripting
Command | Behavior |
---|---|
Run, Notepad.exe | Launch Notepad |
Send, my text | Write "my text" |
Send, {F1} | Press F1 |
Send, {ctrl down}c{ctrl up} | CTRL+C |
Send, ^s | CTRL+S |
ControlSend,, {Alt down}f{Alt up}, Untitled - Notepad | Sends the text ALT+F in the window of Notepad without activating it or bringing it to the foreground |
Sleep, 1000 | Sleep 1 second |
Click 100, 200 | Click relatively to the active window X,Y |
Click 100, 200, right | Right click |
Click 2 | Double click on the current position |
WinWait, Untitled - Notepad | Waits until the specified window exists. |
WinClose | Close the current window |
WinActivate, Calculator | Active the window "Calculator" |
WinMaximize, Main Panel | Maximize the window "Main Panel" |
ControlClick, Change | Click on the button "Change" |
ControlGetText, VariableTextOutput , Controler | Put the text of a controler in the variable VariableTextOutput. The HWND of a control is typically retrieved via ControlGet Hwnd, MouseGetPos, or DllCall. |
ControlGetFocus, VariableTextOutput | Put the text of the focused controler in the variable VariableTextOutput |
ControlFocus, OK, Some Window Title | Set focus to the OK button |
; my comment | Put a comment |
SetTitleMatchMode 2 | 1: A window's title must start with the specified WinTitle to be,a match.,
2: A window's title can contain WinTitle anywhere inside it to be a match., 3:,A window's title must exactly match WinTitle to be a match. RegEx: Use regex, ex : WinActivate Untitled.*Notepad. |
#SingleInstance Force | If the AHK instance does not close automatically, this forces the replacement of the script |
#Requires AutoHotkey v1 | There are 2 versions of AutoHotKey, you can specify v1 or v2. This may be important, as the syntax varies from one version to the next. |
#NoEnv | Recommended for performance and compatibility with future AutoHotkey releases. |
CoordMode, Pixel, Screen | Recommended for multi-screen. |
Special touch
Key Name | Resulting Keystroke |
---|---|
{F1} - {F24} | Function keys. For example: {F12} is the F12 key. |
{!} | ! |
{#} | # |
{+} | + |
{^} | ^ |
{{} | { |
{}} | } |
{Enter} | ENTER key on the main keyboard |
{Escape} or {Esc} | ESCAPE |
{Space} | SPACE (this is only needed for spaces that appear either at the beginning or the end of the string to be sent -- ones in the middle can be literal spaces) |
{Tab} | TAB |
{Backspace} or {BS} | Backspace |
{Delete} or {Del} | Delete |
{Insert} or {Ins} | Insert |
{Up} | Up-arrow key on main keyboard |
{Down} | Down-arrow down key on main keyboard |
{Left} | Left-arrow key on main keyboard |
{Right} | Right-arrow key on main keyboard |
{Home} | Home key on main keyboard |
{End} | End key on main keyboard |
{PgUp} | Page-up key on main keyboard |
{PgDn} | Page-down key on main keyboard |
{CapsLock} | CapsLock (using SetCapsLockState is more reliable on Win 2k/XP). Sending {CapsLock} might require SetStoreCapslockMode Off beforehand. |
{ScrollLock} | ScrollLock (see also: SetScrollLockState) |
{NumLock} | NumLock (see also: SetNumLockState) |
{Control} or {Ctrl} | CONTROL (technical info: sends the neutral virtual key but the left scan code) |
{LControl} or {LCtrl} | Left CONTROL key (technical info: sends the left virtual key rather than the neutral one) |
{RControl} or {RCtrl} | Right CONTROL key |
{Control Down} or {Ctrl Down} | Holds the CONTROL key down until {Ctrl Up} is sent. To hold down the left or right key instead, use {RCtrl Down} and {RCtrl Up}. |
{Alt} | ALT (technical info: sends the neutral virtual key but the left scan code) |
{LAlt} | Left ALT key (technical info: sends the left virtual key rather than the neutral one) |
{RAlt} | Right ALT key (or AltGr, depending on keyboard layout) |
{Alt Down} | Holds the ALT key down until {Alt Up} is sent. To hold down the left or right key instead, use {RAlt Down} and {RAlt Up}. |
{Shift} | SHIFT (technical info: sends the neutral virtual key but the left scan code) |
{LShift} | Left SHIFT key (technical info: sends the left virtual key rather than the neutral one) |
{RShift} | Right SHIFT key |
{Shift Down} | Holds the SHIFT key down until {Shift Up} is sent. To hold down the left or right key instead, use {RShift Down} and {RShift Up}. |
{LWin} | Left Windows key |
{RWin} | Right Windows key |
{LWin Down} | Holds the left Windows key down until {LWin Up} is sent |
{RWin Down} | Holds the right Windows key down until {RWin Up} is sent |
{AppsKey} | Windows App key (invokes the right-click or context menu) |
{Sleep} | Computer SLEEP key. |
{ASC nnnnn} | Sends an ALT+nnnnn keypad combination, which can be used to generate special characters that don't exist on the keyboard. To generate ASCII characters, specify a number between 1 and 255. To generate ANSI characters (standard in most languages), specify a number between 128 and 255, but precede it with a leading zero, e.g. {Asc 0133}.Unicode characters may be generated by specifying a number between 256 and 65535 (without a leading zero). However, this is not supported by all applications. For alternatives, see the section below. |
{U+nnnn} | [v1.0.90+]: Sends a Unicode character where nnnn is the hexadecimal value of the character excluding the 0x prefix. This typically isn't needed in Unicode versions of AutoHotkey, where Send and ControlSend automatically support Unicode text.If the character doesn't map to a virtual keycode, SendInput() or WM_CHAR is used to send the character and the current Send mode has no effect. |
{vkXX} | Sends a keystroke that has virtual key XX and scan code YYY. For example: Send {vkFFsc159}. If the sc or vk portion is omitted, the most appropriate value is sent in its place. |
{scYYY} | |
{vkXXscYYY} | The values for XX and YYY are hexadecimal and can usually be determined from the main window's View->Key history menu item. See also: Special Keys |
{Numpad0} - {Numpad9} | Numpad digit keys (as seen when Numlock is ON). For example: {Numpad5} is the digit 5. |
{NumpadDot} | Numpad Period (as seen when Numlock is ON). |
{NumpadEnter} | Enter key on keypad |
{NumpadMult} | Numpad Multiply |
{NumpadDiv} | Numpad Divide |
{NumpadAdd} | Numpad Add |
{NumpadSub} | Numpad Subtract |
{NumpadDel} | Delete key on keypad (this key and the following Numpad keys are used when Numlock is OFF) |
{NumpadIns} | Insert key on keypad |
{NumpadClear} | Clear key on keypad (usually the '5' key when Numlock is OFF). |
{NumpadUp} | Up-arrow key on keypad |
{NumpadDown} | Down-arrow key on keypad |
{NumpadLeft} | Left-arrow key on keypad |
{NumpadRight} | Right-arrow key on keypad |
{NumpadHome} | Home key on keypad |
{NumpadEnd} | End key on keypad |
{NumpadPgUp} | Page-up key on keypad |
{NumpadPgDn} | Page-down key on keypad |
{Browser_Back} | Select the browser "back" button |
{Browser_Forward} | Select the browser "forward" button |
{Browser_Refresh} | Select the browser "refresh" button |
{Browser_Stop} | Select the browser "stop" button |
{Browser_Search} | Select the browser "search" button |
{Browser_Favorites} | Select the browser "favorites" button |
{Browser_Home} | Launch the browser and go to the home page |
{Volume_Mute} | Mute/unmute the master volume. Usually equivalent to SoundSet, +1, , mute. |
{Volume_Down} | Reduce the master volume. Usually equivalent to SoundSet -5. |
{Volume_Up} | Increase the master volume. Usually equivalent to SoundSet +5. |
{Media_Next} | Select next track in media player |
{Media_Prev} | Select previous track in media player |
{Media_Stop} | Stop media player |
{Media_Play_Pause} | Play/pause media player |
{Launch_Mail} | Launch the email application |
{Launch_Media} | Launch media player |
{Launch_App1} | Launch user app1 |
{Launch_App2} | Launch user app2 |
{PrintScreen} | Print Screen |
{CtrlBreak} | Ctrl+break |
{Pause} | Pause |
{Click [Options]} | Sends a mouse click using the same options available in the Click command. For example, {Click} would click the left mouse button once at the mouse cursor's current position, and {Click 100, 200} would click at coordinates 100, 200 (based on CoordMode). To move the mouse without clicking, specify 0 after the coordinates; for example: {Click 100, 200, 0}. The delay between mouse clicks is determined by SetMouseDelay (not SetKeyDelay). |
{WheelDown}, {WheelUp}, {WheelLeft}, {WheelRight}, {LButton}, {RButton}, {MButton}, {XButton1}, {XButton2} | Sends a mouse button event at the cursor's current position (to have control over position and other options, use {Click} above). The delay between mouse clicks is determined by SetMouseDelay. WheelLeft/Right require v1.0.48+, but have no effect on operating systems older than Windows Vista. |
{Blind} | 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.{Blind} also causes SetStoreCapslockMode to be ignored; that is, the state of Capslock is not changed. Finally, {Blind} omits the extra Control keystrokes that would otherwise be sent; such keystrokes prevent: 1) Start Menu appearance during LWin/RWin keystrokes; 2) menu bar activation during Alt keystrokes.Blind-mode is used internally when remapping a key. For example, the remapping a::b would produce: 1) "b" when you type "a"; 2) uppercase B when you type uppercase A; and 3) Control-B when you type Control-A.{Blind} is not supported by SendRaw and ControlSendRaw. Furthermore, it is not completely supported by SendPlay, especially when dealing with the modifier keys (Control, Alt, Shift, and Win). |
{Raw} | Sends the keystrokes exactly as they appear rather than translating {Enter} to an ENTER keystroke, ^c to Control-C, etc. Although the string {Raw} need not occur at the beginning of the string, once specified, it stays in effect for the remainder of the string. |
Composed key command
Symbol | Description |
---|---|
# | Win (Windows logo key) |
! | Alt |
^ | Control |
+ | Shift |
& | An ampersand may be used between any two keys or mouse buttons to combine them into a custom hotkey. |
On Linux
This function is also linux compatible. Follow this installation procedure on Linux to get started.sudo apt-get install -y python3 python3-tk python3-dev
sudo apt-get install -y scrot
sudo apt-get install -y pip
pip install pyautogui
pip install opencv-python
pip install Pillow
You can then write in interact a pyautogui script in Python. Documentation.
See also
pythonParameters
script
If Windows, your AutoHotKey script
If Linux, your PyAutoGUI script
If Linux, your PyAutoGUI script
_maxDelay (optional)
By default, the script will have 5 minutes to run (the function is blocked until the end of this time or until the script ends normally).
If you set the value to null, there is no maximum time limit, and the script must terminate to exit the function. In this case, if an error occurs, you'll have to kill the Grimport process to exit the program.
If you specify a time limit in milliseconds, the script will have this amount of time to run (the function is blocked until the end of this time limit or until the script terminates normally).
If you set the value to null, there is no maximum time limit, and the script must terminate to exit the function. In this case, if an error occurs, you'll have to kill the Grimport process to exit the program.
If you specify a time limit in milliseconds, the script will have this amount of time to run (the function is blocked until the end of this time limit or until the script terminates normally).