Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ensure my application retains focus while SendKeys is called? (Is there a better way than using Sleep?)

Tags:

vbscript

I am updating some scripts which need to switch focus to an application, send some keystrokes, before returning focus to another application.

Option Explicit

dim objShell 
set objShell = WScript.CreateObject("WScript.Shell") 

objShell.AppActivate "AnApplication"
WScript.Sleep 1000 

objShell.SendKeys("%{I}")
...
objShell.SendKeys("{END}") 

WScript.Sleep 1000
objShell.AppActivate "AnotherApplication"

Set objShell = Nothing

I researched some improvements to make to these scripts, and one thing I wished to do was to remove the Sleep statements to speed up the execution of the scripts. In researching this, I found that it's suggested that you check the return value of AppActivate before continuing, to effectively make the script wait until the application has focus and can be sent keystrokes.

I tried updating my script to do this -

Option Explicit

dim objShell 
set objShell = WScript.CreateObject("WScript.Shell")

While Not objShell.AppActivate "AnApplication" 
    Sleep 300
Wend 

objShell.SendKeys("%{I}")
...
objShell.SendKeys("{END}")

While Not objShell.AppActivate "AnotherApplication"
    Sleep 300
Wend 

However the keystrokes seem to only send after focus has been returned to AnotherApplication.

Is there a way to do this to ensure that AnApplication has focus while the keystrokes are sent, without using Sleep?

like image 580
Eilidh Avatar asked Apr 30 '14 12:04

Eilidh


People also ask

What is the use of sendkeys method?

This method places keystrokes in a key buffer. In some cases, you must call this method before you call the method that will use the keystrokes. For example, to send a password to a dialog box, you must call the SendKeys method before you display the dialog box.

How to save a file using sendkeys in Windows 10?

You do Ctrl + S button-through keyboard to save a file). It is recommended to use SendKeys as the last option while you are automating any task. The reason behind this is, as we already discussed, SendKeys sends the keystrokes to the application that is currently active.

How to use VBA sendkeys to automate tasks?

You can use the SendKeys to automate the small tasks like saving an Excel File or Closing an Excel File etc. Use the SendKeys method as a last option while you are trying to automate the things. This is a guide to VBA SendKeys.

Does this dialog window steal focus from the main application window?

This dialog window should absolutely steal focus from the main application window. . That won't help for the special case of launching applications from explorer.exe (taskbar, start menu, Windows Explorer). The app windows are not child windows of explorer.


1 Answers

As I mentioned in my comment, there's no need to put AppActivate() in a loop. It's a synchronous call and the function shouldn't return until it's activated the window (or failed trying).

If you're concerned about your window losing focus, you can call AppActivate() again after sending a few keystrokes or you can call it before every set of keystrokes.

For example:

If Not MySendKeys("AnApplication", "%{I}") Then MsgBox "Could not send %{I}"
If Not MySendKeys("AnApplication", "{End}") Then MsgBox "Could not send {End}"

Function MySendKeys(strApp, strKeys)
    If objShell.AppActivate(strApp) Then
        objShell.SendKeys strKeys
        MySendKeys = True
    End If
End Function
like image 91
Bond Avatar answered Oct 26 '22 03:10

Bond