Results 1 to 9 of 9

Thread: [RESOLVED] Sending keys to previous window... ?

  1. #1

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Resolved [RESOLVED] Sending keys to previous window... ?

    Ok, i know how to sendkeys, thats easy, say if i wanted Control + F i would do
    VB Code:
    1. Sendkeys "^F", 0

    Thats simple enough, BUT i need to send Ctrl + F to the last active window, would fire the Find dialog.

    How would i go about sending the keys to the previously active window ?

    Note: My application will be run from a shortcut key on a keyboard and this is why its needed to send its keys to the previous window, it Unloads itself after its task is complete.
    Zeegnahtuer?

  2. #2
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Sending keys to previous window... ?

    Doubtful you could get the previously activated window without your program being open to detect it.

    Or is your program open and only activated by the keyboard?

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  3. #3

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Sending keys to previous window... ?

    Quote Originally Posted by chemicalNova
    Doubtful you could get the previously activated window without your program being open to detect it.

    Or is your program open and only activated by the keyboard?

    chem
    No, i'm afraid its opened entirely from the keyboard, i thought there might be some way of getting the previous hWnd property...
    Are you sure theres no known workaround for this ?

    If not i might need to rethink it...Hmm.

    EDIT: I've got another idea here, i can sendkeys Alt n Tab, record the hWnd of the currently active program, then send Ctrl + F to it, then close my program, do you think this could work ?
    Also, if you do, could you show me how... hehe.
    Not too sure on sending keys to other windows you see...
    Last edited by thegreatone; Oct 13th, 2005 at 03:26 PM.
    Zeegnahtuer?

  4. #4
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Sending keys to previous window... ?

    Like This

    VB Code:
    1. AppActivate "Title of the window to activate"
    2.    DoEvents
    3.    Sendkeys "^F", 0

    AppActivate allows you to put focus on any application window as long as you know what is on title bar.

    Of course it is TOTALLY unreliable, but it does get the job done in a quick and dirty way.

  5. #5
    Hyperactive Member umilmi81's Avatar
    Join Date
    Sep 2005
    Location
    Sterling Heights, Mi.
    Posts
    335

    Re: Sending keys to previous window... ?

    If you are going to be controling other apps through sendkeys and appactivate be sure to also include the sleep lib

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    and use liberal amounts of sleep and doevents.

  6. #6

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Sending keys to previous window... ?

    Help with the following code, it doesn't seem to return the previous programs titlebar...

    VB Code:
    1. Private Declare Function GetForegroundWindow Lib "user32" () As Long
    2. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    3. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    4. Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    5. Private Declare Function FlashWindow Lib "user32" (ByVal hwnd As Long, ByVal bInvert As Long) As Long
    6. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    7. Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    8.  
    9. Private Sub Form_Activate()
    10. Dim MyStr As String
    11. Dim PrevhWnd As Long
    12. SendKeys "%" & vbKeyTab
    13. Sleep (500)
    14. PrevhWnd = GetForegroundWindow() 'Find out what window has focus
    15. SetForegroundWindow PrevhWnd
    16. MyStr = String(GetWindowTextLength(PrevhWnd) + 1, Chr$(0))
    17. GetWindowText PrevhWnd, MyStr, Len(MyStr)
    18. 'Text1.Text = PrevhWnd
    19. 'ShowWindow PrevhWnd, 6
    20. Sleep (250)
    21. MsgBox MyStr
    22. AppActivate MyStr
    23. DoEvents
    24. SendKeys "^F", 0
    25. 'SendKeys "^F", 1
    26. 'Unload Me
    27. End Sub
    Zeegnahtuer?

  7. #7
    G&G Moderator chemicalNova's Avatar
    Join Date
    Jun 2002
    Location
    Victoria, Australia
    Posts
    4,246

    Re: Sending keys to previous window... ?

    Chucking it in Form_Load does the job for me..

    Should brush up on your Windoez Messages and the order they are executed in

    chem

    Visual Studio 6, Visual Studio.NET 2005, MASM

  8. #8

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Sending keys to previous window... ?

    Quote Originally Posted by chemicalNova
    Should brush up on your Windoez Messages and the order they are executed in
    chem
    Explain what you mean there Chem...
    I'll test this is Form_Load when i get back home in around 5 hours...

    Thanks for the help.
    Zeegnahtuer?

  9. #9

    Thread Starter
    Frenzied Member thegreatone's Avatar
    Join Date
    Aug 2003
    Location
    Oslo, Norway. Mhz:4800 x12
    Posts
    1,333

    Re: Sending keys to previous window... ?

    Ok, back home, everything seems to work fine (Placed code in Load Event)
    Such a nice workaround for getting the previous hWnd. I'm still sure theres a better way though.

    For now, resolved.
    Zeegnahtuer?

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