Results 1 to 10 of 10

Thread: How to send a key to a minimized window

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    28

    How to send a key to a minimized window

    im pretty new here and i hope i posted in the correct section . if not im sorry.
    But i have a problem so i want to Send one key to a minimized form if u know what i mean like running it in the background its the key "F5" that i want to send
    . It works if the window is focused but as soon as i start my internet it refreshs the sites there and not where it was ment to be .
    I hope anyone can help me .

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    28

    Re: How to send a key to a minimized window

    anyone ?

  3. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to send a key to a minimized window

    How are you sending the program the F5 key now?

    If you know the handle of the window that is accepting keys (could be the main window or an edit window, etc, ) then sending the key down/up to that handle may work. For example I have a VB6 app that has its form set to KeyPreview = True so it detects keys pressed on it, lets call it "VB6-TestApp", even when minimized it detects the F5 key sent to it from this VB10 code...

    Code:
     
    Public Class Form1
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        Private Declare Function PostMessage Lib "user32.dll" Alias "PostMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Private Const WM_KEYUP As Integer = &H101
        Private Const WM_KEYDOWN As Integer = &H100
        Private Const VK_F5 As Integer = &H74 ' http://msdn.microsoft.com/en-us/library/dd375731%28v=vs.85%29.aspx
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' find window with title bar text "VB6-TestApp"
            Dim r As IntPtr = FindWindow(Nothing, "VB6-TestApp")
            ' if window found Send F5 to the handle
            If r.ToInt32 <> 0 Then  'Debug.Print("Window found!")
                PostMessage(r, WM_KEYDOWN, VK_F5, 0)
                PostMessage(r, WM_KEYUP, VK_F5, 0)
            Else
                Debug.Print("Window NOT found!")
            End If
        End Sub
    End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    28

    Re: How to send a key to a minimized window

    Quote Originally Posted by Edgemeal View Post
    How are you sending the program the F5 key now?

    If you know the handle of the window that is accepting keys (could be the main window or an edit window, etc, ) then sending the key down/up to that handle may work. For example I have a VB6 app that has its form set to KeyPreview = True so it detects keys pressed on it, lets call it "VB6-TestApp", even when minimized it detects the F5 key sent to it from this VB10 code...
    ok sounds good im sending it with a timer to the program with sendkeys.send
    I will try this now

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    28

    Re: How to send a key to a minimized window

    hmm i dont know it doesnt work fine ...
    i just want to make a project that refreshes the website after 9 seconds also if it is minimized
    But now it doesnt send keys at all

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    26

    Re: How to send a key to a minimized window

    Try This. SendKeys.Send("{F5}").

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    28

    Re: How to send a key to a minimized window

    Quote Originally Posted by Marvin777 View Post
    Try This. SendKeys.Send("{F5}").
    Thats what i had before but then it works for every window not just in my application....

  8. #8
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to send a key to a minimized window

    Quote Originally Posted by killerrj8 View Post
    hmm i dont know it doesnt work fine ...
    i just want to make a project that refreshes the website after 9 seconds also if it is minimized
    But now it doesnt send keys at all
    Then you're probably using the wrong handle in that code. If it is a web browser control on a VB form you are trying to send the F5 key to then you need to use the web browser control handle, not the forms handle.

    If you just need to refresh after 9 seconds then why not just add a timer that runs once, set timer to 9000 and start it when that certain URL is detected in the browser or something.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    28

    Re: How to send a key to a minimized window

    Quote Originally Posted by Edgemeal View Post
    Then you're probably using the wrong handle in that code. If it is a web browser control on a VB form you are trying to send the F5 key to then you need to use the web browser control handle, not the forms handle.

    If you just need to refresh after 9 seconds then why not just add a timer that runs once, set timer to 9000 and start it when that certain URL is detected in the browser or something.


    And how can i focuse on the webbrowser ?
    Im not that good in programming sry

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: How to send a key to a minimized window

    Quote Originally Posted by killerrj8 View Post
    And how can i focuse on the webbrowser ?
    Im not that good in programming sry
    When you post/send a message to a control handle the control normally doesn't need to have focus, it only needs to accept/support the messages sent to it.

    If you made both apps then a simple way around it is to just have your browser app save the web browser control handle when it starts up somewhere, like the registry, or a file, etc,. Now your other VB apps just need to retrieve that handle value from where the browser app saved it.

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