WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Shortcut Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Use a Hotkey to Sendkeys to an Active Window

 Keywords:  WinGetActive WinGetLastActive WinState cWndInfo Style Shortcut Hotkey Sendkey Keystroke Last Active Window Win7 ZOrder Z Order Z-order Top TOPMOST WS_VISIBLE 

Question:

I would like to create a WinBatch script that automatically sends keystrokes to the active window, using a hotkey.

For example I would like to use a hotkey Ctrl-Alt-e to auto fill in my email address to the currently active window.

Answer:

Sounds like it should be a simple proposition, yes? No. In order to launch your script using a Hotkey, you will need to create a Windows Shortcut on the desktop. You can then define the hotkey you would like to use in the Shortcuts property. However, when the shortcut is launched via a hotkey on Windows 7, the active window gets lost.

What happens when you use a desktop shortcut is that one of the windows associated with one of the shell's many threads gets the input focus and that will change the z order. After that, the focus and z order top get passed around to different shell threads depending on what the target of the shortcut does.

So now how do you find the last active window from your WinBatch script? This script walks the adjacent and child windows until it finds one that is visible, not minimized or hidden and not a shell window using Control Manger functions.

;***************************************************************************
;** 
;**      Get Last Active Window from Windows Shortcut
;** 
;** Purpose: Get Active Window When Launched from a Shortcut Hotkey Trick
;** Inputs: Must be launched from a windows shortcut.
;** Outputs: Get Last Active Window
;** Requirements:  Requires WinBatch 2011B or newer. Requires the Control Manager extender
;** Revisions:  2013.10.4 Deana Falk
;** 
;** NOTES:When you use a desktop shortcut, one of the windows associated with one of the shell's 
;** many threads gets the input focus and that will change the z order.  After that, the focus and 
;** z order top get passed around to different shell threads depending on what the target of the shortcut does.  
;** This script walks the adjacent and child windows until it finds one that is visible and not a shell window 
;** using Control Manger functions.  
;** 
;** 
;***************************************************************************
If Version( ) < '2011B' 
   Pause('Notice', 'Need 2011B or Newer Version of WinBatch')
   Exit 
EndIf

#DefineFunction WinGetLastActive ()
AddExtender("wwctl44i.dll",0,"wwctl64i.dll")
WS_VISIBLE = 268435456 ;0x10000000L
allwins = WinItemize()
For x = 1 to ItemCount(allwins, @tab)
   title = ItemExtract( x, allwins, @tab )
   hwnd = DllHwnd(title)
   style = cWndInfo( hwnd, 20 )
   If (style & WS_VISIBLE)
      If !(StrLower(title)=="start" || StrLower(title)=="program manager")
         state = WinState( title )
         If state == 2 || state == 3 ; not hidden or iconized.
            break      
         Endif
      EndIf
   EndIf
Next
Return title
#EndFunction

activeWin =  WinGetLastActive()
Pause("Debugging - Active Window",activeWin)

SendKeysTo( activeWin, "Hello World" )

Exit

Article ID:   W18280
Filename:   Use a Hotkey to Sendkeys to an Active Window.txt
File Created: 2013:10:04:10:06:44
Last Updated: 2013:10:04:10:06:44