Helper Smith
Helper Smith

Reputation: 31

vbs SendKeys only to specific window

I have this VBScript

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "KEYS TO SEND"

This code will send Keys Strokes to Ative Top window

How can I send the keystrokes only to specific window?

Upvotes: 2

Views: 9876

Answers (1)

ivan_pozdeev
ivan_pozdeev

Reputation: 36018

The intended way in WSH is to first activate the window with WshShell.AppActivate as others noted.

As per How do I send key strokes to a window without having to activate it using Windows API? , in WinAPI, you cannot reliably send keystrokes to anything but the active window.

If you intend to try anyway (don't complain if when things go wrong), as per the linked question, you need to send a series of messages to the window with SendMessage.

There's no built-in way in WSH to call WinAPI. So, either use another language whose standard library provides access to WinAPI and/or wrappers over them, or a 3rd-party COM object like DynamicWrapperX.


As a side note, you may wish to consider a dedicated UI automation language like AutoIt or Autohotkey. The former can be called via a COM interface if you still need to use WSH for some reason.

Upvotes: 5

Related Questions