SendKey to a window NOT in focus - but keep focus?
Fyi, I'm fairly new to VB.
Anyway, to the point.
I have two applications, both running in full screen.
What I want to accomplish is that while one of the applications are running in focus (full screened), I want my VB program to run in the background and send a key on a regular basis to the other application (the one that is NOT in focus) without having to tab out of the focused application.
I managed to achieve some of this partly with the help of FindWindow and ShowWindow:
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const SW_HIDE = 0
Private Const SW_NORMAL = 1
Private Const SW_MAXIMIZE = 3
Private Const SW_SHOW = 5
Private Const SW_MINIMIZE = 6
Private Const SW_RESTORE = 9
Dim hinst As Long
Dim hinst2 As Long
hinst = FindWindow(vbNullString, "PrgNotInFocus") ' The program that should not be in focus, but have keys sent to
hinst2 = FindWindow(vbNullString, "PrgInFocus") ' Should be in focus at all times
If Not IsNull(hinst) Then
ShowWindow hinst, SW_NORMAL
SendKeys (1)
ShowWindow hinst, SW_MAXIMIZE
End If
This allows me to bring forth the window that I don't want in focus and "makes it press 1".
But that's hardly the effect I'm looking for, since it:
-Tabs out of my current application and focuses the wrong one
-Requires the window I want to SendKeys to to be focused
Suggestions?
Re: SendKey to a window NOT in focus - but keep focus?
If there's something that needs to be clarified, just say so.
Re: SendKey to a window NOT in focus - but keep focus?
SendKeys requires the receiving window to be with input focus. No way around it. Its also flakey that way if the user accidentally presses a key or clicks somewhere taking focus away. This will screw up your automation.
Use FindWindow, FindWindowEx and SendMessage APIs as a solid way to transfering information to the desired app.