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
Sending keys only to a particular window, possibly unfocused Topic is solved
Sending keys only to a particular window, possibly unfocused
- Attachments
-
- Capture.PNG
- (15.48 KiB) Downloaded 125 times
Re: Sending keys only to a particular window, possibly unfocused
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.
Re: Sending keys only to a particular window, possibly unfocused
No joy there. I've tried
controlSend, , {Down}{Down}, ahk_exe KeyingTool.exe
and nothing happened, even when the window was active.
Andrew
controlSend, , {Down}{Down}, ahk_exe KeyingTool.exe
and nothing happened, even when the window was active.
Andrew
Re: Sending keys only to a particular window, possibly unfocused
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.
Re: Sending keys only to a particular window, possibly unfocused
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
Re: Sending keys only to a particular window, possibly unfocused
Well, you did mention that this program is pretty laggy and requires some delays. Perhaps:
If that doesn't work, I'm afraid I'm all out of ideas :/
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
I have no idea what I'm doing.
Re: Sending keys only to a particular window, possibly unfocused
No, fraid not - nothing moves at all.
I did some more investigations using the following:
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:
So it looks like I'm missing something.
Andrew
I did some more investigations using the following:
Code: Select all
WinGet, TgtExe, ProcessName, A
ControlSend, , {Down}{Down}, ahk_exe %TgtExe%
Code: Select all
send {Down}{Down}
Andrew
Re: Sending keys only to a particular window, possibly unfocused
You mentiond that nothing is moveing, did you use SetTitleMatchMode?
Code: Select all
SetTitleMatchMode, 2
Re: Sending keys only to a particular window, possibly unfocused
No difference. Not surprising really, since I'm using the full name of the process. A
Re: Sending keys only to a particular window, possibly unfocused Topic is solved
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Sending keys only to a particular window, possibly unfocused
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.
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
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
}
The sleep command ensures the Esc *is* seen. Otherwise the keys are just buffered and pausing or stopping becomes very difficult.
Andrew
Re: Sending keys only to a particular window, possibly unfocused
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
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
Who is online
Users browsing this forum: Google [Bot], mikeyww and 324 guests