Environment detection and sending

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Algreth
Posts: 3
Joined: 01 Mar 2018, 11:58

Environment detection and sending

01 Mar 2018, 14:38

I am trying to make a basic test script that identifies a active environment, then passes keys to it whether it remains in focus or leaves focus. I found a note by someone mentioning that for better stability of ControlSend it should be preceded by Control focus, so I am attempting that combination. However, I can't get it to work:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

!s:: ;select window to send to
	ControlGet,v_h,Hwnd
return

!c:: ;check the window selection
	MsgBox "The selected window has Hwnd " %v_h%
return

!q:: ;send the keys to selected window
ControlFocus,,ahk_id %v_h%
Loop, 9
{
	ControlSend,,s,ahk_id %v_h%
	Sleep, 500
}
return
The hotkey to display the message box leaves a blank hwnd. The controlSend doesn't do anything. The documentation for msgbox seems to indicate that I'm using the variable correctly. What's up here?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Environment detection and sending

01 Mar 2018, 20:05

Try WinGet instead of ControlGet to get a window's Hwnd.
Algreth
Posts: 3
Joined: 01 Mar 2018, 11:58

Re: Environment detection and sending

01 Mar 2018, 20:18

Exaskryz wrote:Try WinGet instead of ControlGet to get a window's Hwnd.
Using WinGet results in the same behavior; the variable comes out as blank in the MsgBox.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Environment detection and sending

01 Mar 2018, 20:31

Oops, try WinGet, v_h, ID, A where A is interpreted to be the active window (don't change it). When I used just WinGet, v_h, ID it also came back blank for me.
Algreth
Posts: 3
Joined: 01 Mar 2018, 11:58

Re: Environment detection and sending

01 Mar 2018, 20:50

Thanks, that's a step in the right direction! The MsgBox shows the ID now. Two more questions:

1) What does the "A" fall under in the options for WinGet? Following the sequence listed in the documentation, it should correspond to WinText. Reading the entry of WinText, this "A" should only pick the appropriate window if the title contains the substring "A". What if there is no such character in the window title?

2) While the ID is being correctly recorded now, the hotkey !q as written will not write characters to my active window. Everything is run as Admin. Do I need to specify additional information in the "control" which I have left blank? Trying to put the variable ID in the control slot doesn't work either.
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Environment detection and sending

01 Mar 2018, 21:35

A is the WinTitle parameter. v_h is the OutputVar and ID is the cmd (command).

The ControlSend may require an actual control value (and same for ControlFocus), instead of it being omitted. You can also try ahk_parent in that parameter as well. It is very likely that if ahk_parent doesn't work, you'd need to use a control value specific to what you're targeting. For example, in Notepad, it would be the Edit1 control. You can use window spy to identify this in your target program. Window Spy can be accessed by right-clicking the tray icon (by the system clock) of a running script.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 169 guests