Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Send keystrokes to background window?


  • Please log in to reply
14 replies to this topic
Hardy
  • Members
  • 6 posts
  • Last active: Feb 13 2005 04:32 PM
  • Joined: 10 Feb 2005
Hi,
I'm new to AutoHotkey, it's absolutely great (read about it in German c't magazine). Thanks for this cool piece of software!

Is it possible to send keystrokes to a background window?

Thanks for advice!

Hardy

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Take a look at the ControlSend function. If it doesn't work for you (it most likely will), maybe Rajat can help you out with some SendMessage codes.

  • Guests
  • Last active:
  • Joined: --

ControlSend function. If it doesn't work for you (it most likely will), maybe Rajat can help you out with some SendMessage codes.


Thanks, but
ControlSend, WindowName, Keystroke
or
ControlSend, ahk_class 12345, Keystroke
doesn't do anything on my XP system, neither if my window is in the foreground nor in the background... :?:

Hardy

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
That's because that's not the correct syntax. Please take a look at the documentation for it. I included the link to it in my previous post, and here I'll include another one:

[link]

Hardy
  • Members
  • 6 posts
  • Last active: Feb 13 2005 04:32 PM
  • Joined: 10 Feb 2005
:oops: getting already better now:

It's working when the window is in fore- or background but not when it's in the taskbar...
The window should stay in the taskbar when the keystrokes are applied...

Thanks
Hardy

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
That's strange. Try this code:

settitlematchmode,2

capslock::
controlsend,,Test., - Notepad
return

If CapsLock doesn't send "Test." to Notepad even when it's minimized, it might be a bug and you should report it. If it does, though, the problem probably lies in your script. Could you please post it, so we can help you directly rather than little by little?

Hardy
  • Members
  • 6 posts
  • Last active: Feb 13 2005 04:32 PM
  • Joined: 10 Feb 2005
Seems to be a bug then.
Your code works as long as the window is on screen (active or in background) but when I minimize notepad it's not sent...

Does it work on your system?

I'm working with XP SP 2 on a AthlonXP 2400+ computer...

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Yes, it does; same here, XP SP2.

Hardy
  • Members
  • 6 posts
  • Last active: Feb 13 2005 04:32 PM
  • Joined: 10 Feb 2005
Really funny!
So do I'll post this issue as a possible bug or do you have any idea if there could be another reason for my problem?
I hope that there are any country specific differences in the system (but I really cannot imagine).
Many thanks so far for your help!

Hardy

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
Ya, sure, I guess you can post it as a bug; if you ran that code as a script by itself, and it didn't work when it was minimized with just one Notepad window open, then it might be a bug. I can't imagine what kind of bug though.

Edit: Wait... make sure you download the latest version of AutoHotkey first. Install it, then run the script again. It might not fix anything, but if you have a much older version than it was probably fixed since you got it.

polyethene
  • Administrators
  • 5517 posts
  • Last active: Oct 19 2014 02:44 PM
  • Joined: 26 Oct 2012

Thanks, but

ControlSend, WindowName, Keystroke
or
ControlSend, ahk_class 12345, Keystroke
doesn't do anything on my XP system, neither if my window is in the foreground nor in the background... :?:

Hardy

I'm not sure if this might be the problem but the second parameter should be the ClassNN or the name/text of the control. Open AutoIt3 Window Spy in the AutoHotkey installation directory and use that to get the control to send the keystrokes too.

Example: ControlSend, Edit1, Test, NotePad

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
I know it to be a cold, hard fact that Notepad's Edit1 is it's topmost control, which ControlSend sends to if that parameter is blank. Also, there's no chance that his Notepad is different, since his OS is exactly the same as mine.

Still, Hardy, you could try Titan's suggestion. If it doesn't work without the parameter, though, it probably won't work with it (in Notepad at least).

Hardy
  • Members
  • 6 posts
  • Last active: Feb 13 2005 04:32 PM
  • Joined: 10 Feb 2005
After restarting my system this morning I noticed that your code works with a minimized notepad window too as you described!

The problem with my own script remains so I post it here:
settitlematchmode,2
ControlSend,, ^g, GetRight
This should send the hotkey for 'Automatic Download' to my GetRight program even when minimized.
I also tried to access the function by opening the download pulldown menu and choose the appropriate function from there (!da) and I tried different paramaters for the control.
Nothing changed, it works in the fore- and background but not when minimized.

Hardy

jonny
  • Members
  • 2951 posts
  • Last active: Feb 24 2008 04:22 AM
  • Joined: 13 Nov 2004
In this case PostMessage seems to be ideal for the task; however, the process of getting the right message to send can be difficult if you don't know exactly what you're doing, so before you even attempt it there's probably another, easier command you want to look at:

WinMenuSelectItem (link)

Here is an example script that works when Notepad is minimized. It inserts the time and date (under the edit menu):

settitlematchmode,2
return

capslock::
winmenuselectitem, - Notepad,,Edit,Time/Date
return

But don't get too excited if it works. The help file specifically states that it might not work on some windows when they're minimized (Your GetRight might be one of these). Also, it won't work on non-standard menu bars. This is easier to figure out; just look at GetRight's menubar and see if it looks and feels like the menubars of other programs (for instance, Notepad has a standard menubar, but the latest versions of Microsoft Word don't).

Hardy
  • Members
  • 6 posts
  • Last active: Feb 13 2005 04:32 PM
  • Joined: 10 Feb 2005
winmenuselectitem doesnt work at all (with GetRight), I didnt checkout other programs...