Results 1 to 3 of 3

Thread: SendKey to a window NOT in focus - but keep focus?

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    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?

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2007
    Posts
    11

    Re: SendKey to a window NOT in focus - but keep focus?

    If there's something that needs to be clarified, just say so.

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width