Page 1 of 1

Environment detection and sending

Posted: 01 Mar 2018, 14:38
by Algreth
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?

Re: Environment detection and sending

Posted: 01 Mar 2018, 20:05
by Exaskryz
Try WinGet instead of ControlGet to get a window's Hwnd.

Re: Environment detection and sending

Posted: 01 Mar 2018, 20:18
by Algreth
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.

Re: Environment detection and sending

Posted: 01 Mar 2018, 20:31
by Exaskryz
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.

Re: Environment detection and sending

Posted: 01 Mar 2018, 20:50
by Algreth
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.

Re: Environment detection and sending

Posted: 01 Mar 2018, 21:35
by Exaskryz
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.