Sending keys only to a particular window, possibly unfocused Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Sending keys only to a particular window, possibly unfocused

22 Sep 2018, 21:58

Is there any way to send key sequences so that they only go to the target (initially active) window, even if this becomes unfocused.

See the attachment. I need to run sequences in this grid, but occasionally use 'sleep' to ensure the grid updates before the next keys. The trouble is that it all takes time, and I want to do something else while the script runs. Which means the target window becomes unfocused, and the sequence continues on the new window.

If I just send a long string of keystrokes to the window, it's not so bad. Normally the keys are buffered somewhere in Windows and losing focus isn't an issue. But this also means that I can't halt the sequence because the script's already finished. Which is sometimes close to a disaster! In any case, some actions don't work *unless* I use 'sleep'.

Andrew
Attachments
Capture.PNG
(15.48 KiB) Downloaded 125 times
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: Sending keys only to a particular window, possibly unfocused

23 Sep 2018, 03:05

Yes! There is in fact a way. Since you are pretty vague on what you are trying to accomplish, I will just direct you to the documentation for ControlSend and ControlClick. Using these, you can send key presses and mouse clicks to any window, even if it's minimized or out of focus.
I have no idea what I'm doing.
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Sending keys only to a particular window, possibly unfocused

23 Sep 2018, 16:25

No joy there. I've tried
controlSend, , {Down}{Down}, ahk_exe KeyingTool.exe
and nothing happened, even when the window was active.

Andrew
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 00:40

Hmmm, not sure. Is the program you want to send key presses to publicly available? I can try messing around and see if I can get anything to work.
I have no idea what I'm doing.
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 01:21

Not really - it requires signing up. Also, it's not that much of an issue. I've noticed that some apps appear to be quite recalcitrant when it comes to AHK. A
buttshark
Posts: 62
Joined: 22 Apr 2017, 20:57

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 01:56

Well, you did mention that this program is pretty laggy and requires some delays. Perhaps:

Code: Select all

ControlSend,, {Down down}, ahk_exe KeyingTool.exe
sleep, 100
ControlSend,, {Down up}, ahk_exe KeyingTool.exe
sleep, 100
ControlSend,, {Down down}, ahk_exe KeyingTool.exe
sleep, 100
ControlSend,, {Down up}, ahk_exe KeyingTool.exe
If that doesn't work, I'm afraid I'm all out of ideas :/
I have no idea what I'm doing.
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 03:43

No, fraid not - nothing moves at all.

I did some more investigations using the following:

Code: Select all

WinGet, TgtExe, ProcessName, A
ControlSend, , {Down}{Down}, ahk_exe %TgtExe%
This works for Notepad, but not for Word (winword.exe) or the keying tool (or my own editor, FWIW). All of them work if I just go:

Code: Select all

send {Down}{Down}
So it looks like I'm missing something.

Andrew
WingbtZ
Posts: 22
Joined: 09 Aug 2018, 04:41

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 04:06

You mentiond that nothing is moveing, did you use SetTitleMatchMode?

Code: Select all

SetTitleMatchMode, 2
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 04:12

No difference. Not surprising really, since I'm using the full name of the process. A
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Sending keys only to a particular window, possibly unfocused  Topic is solved

24 Sep 2018, 04:48

3 possible ideas.

Code: Select all

ControlSend, ahk_parent, {Down 2}, A

ControlFocus, Edit1, A ;replace 'Edit1' with the appropriate control (if there is a control, use AHK's window spy or ControlGetFocus to find out)
ControlSend, Edit1, {Down 2}, A

WinGet, hWnd, ID, A
ControlFocus,, % "ahk_id " hWnd
ControlSend, ahk_parent, {Down 2}, A
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 18:04

Ok, I think we're there.
Firstly ahk_parent doesn't make any difference (I'd already tried that).

The following code works for me. I was slowed down by the suggested ControlFocus, which should have been ControlGetFocus.

Code: Select all

  WinGet, TgtExe, ProcessName, A            ; get process name of active
  ControlGetFocus Fctrl, A                     ;get the current focused control
  loop {
    if GetKeyState("Esc", "P") {
      SoundBeep
      BREAK
      }  
    ControlSend, %Fctrl%, {Down 1}, ahk_exe %TgtExe%
    sleep 100
    }
This moves down the document until the Esc key is pressed, and it doesn't matter if the window becomes inactive. It pauses if the window is minimised, but continues after restore. It works in all my test apps, including Word, my editor and the keyingtool.

The sleep command ensures the Esc *is* seen. Otherwise the keys are just buffered and pausing or stopping becomes very difficult.

Andrew
Gabby
Posts: 28
Joined: 19 Aug 2017, 03:55

Re: Sending keys only to a particular window, possibly unfocused

24 Sep 2018, 22:00

Not happy.
I tried this with my app and it was unreliable. I slowed it down with sleeps until it was useless and it was still unreliable with this app. And that was with the app active the whole time. The key sequence was as follows (repeated):

- ^C and check clipboard, maybe exit
- F3 to copy the cell above.
- Down to the cell below.

This works fine with just 'send' (to the activewindow).

So I guess my conclusion is though this approach works, more or less, it won't work with some commands and some apps.

Andrew

Return to “Ask For Help”

Who is online

Users browsing this forum: ark, LC99, mikeyww and 37 guests