postmessage & Virtual Keys to another application only partially working

22-degrees

Well-known member
Joined
Feb 11, 2012
Messages
156
Location
South East Ireland
Programming Experience
1-3
Hi all,

I was using postmessage and Virtual Keys to send the F5 keystroke to another non vb-project application.

This worked just fine and the application refreshed without needing focus as it was supposed to.

Now however, I need to find and replace a string on the same application window before ordering the refresh.

But the only part of the sequence that is working is the Refresh part..

I tried setting focus with
VB.NET:
AppActivate
but this changed nothing aside from visually focussing on the window.. Same result, only the refresh part seems to work.

The keys are defined as follows:

VB.NET:
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

    Private Const VK_LCONTROL As Integer = &HA2
    Private Const VK_keyH As Integer = &H48
    Private Const VK_key5 As Integer = &H35
    Private Const VK_key0 As Integer = &H30
    Private Const VK_SPACE As Integer = &H20
    Private Const VK_TAB As Integer = &H9
    Private Const VK_RETURN As Integer = &HC

and the postmessage as follows:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim r As IntPtr = FindWindow(myWindowTitle)


        If r.ToInt32 <> 0 Then

            AppActivate(myWindowTitle)


[COLOR=#008000]' Ctrl + H = Find/Replace[/COLOR]
            PostMessage(r, WM_KEYDOWN, VK_LCONTROL, 0)
            PostMessage(r, WM_KEYDOWN, VK_keyH, 0)
            PostMessage(r, WM_KEYUP, VK_keyH, 0)
            PostMessage(r, WM_KEYUP, VK_LCONTROL, 0)

[COLOR=#008000]' String to Find[/COLOR]
            PostMessage(r, WM_KEYDOWN, VK_SPACE, 0)
            PostMessage(r, WM_KEYUP, VK_SPACE, 0)
            PostMessage(r, WM_KEYDOWN, VK_key5, 0)
            PostMessage(r, WM_KEYUP, VK_key5, 0)
            PostMessage(r, WM_KEYDOWN, VK_key0, 0)
            PostMessage(r, WM_KEYUP, VK_key0, 0)

[COLOR=#008000]' Tab to Replace box[/COLOR]
            PostMessage(r, WM_KEYDOWN, VK_TAB, 0)
            PostMessage(r, WM_KEYUP, VK_TAB, 0)

[COLOR=#008000]' String to Replace[/COLOR]
            PostMessage(r, WM_KEYDOWN, VK_SPACE, 0)
            PostMessage(r, WM_KEYUP, VK_SPACE, 0)
            PostMessage(r, WM_KEYDOWN, VK_key5, 0)
            PostMessage(r, WM_KEYUP, VK_key5, 0)
            PostMessage(r, WM_KEYDOWN, VK_key0, 0)
            PostMessage(r, WM_KEYUP, VK_key0, 0)
            PostMessage(r, WM_KEYDOWN, VK_key0, 0)
            PostMessage(r, WM_KEYUP, VK_key0, 0)

[COLOR=#008000]' Tab to Replace Button[/COLOR]
            PostMessage(r, WM_KEYDOWN, VK_TAB, 0)
            PostMessage(r, WM_KEYUP, VK_TAB, 0)
            PostMessage(r, WM_KEYDOWN, VK_TAB, 0)
            PostMessage(r, WM_KEYUP, VK_TAB, 0)
            PostMessage(r, WM_KEYDOWN, VK_TAB, 0)
            PostMessage(r, WM_KEYUP, VK_TAB, 0)
            PostMessage(r, WM_KEYDOWN, VK_TAB, 0)
            PostMessage(r, WM_KEYUP, VK_TAB, 0)
            PostMessage(r, WM_KEYDOWN, VK_TAB, 0)
            PostMessage(r, WM_KEYUP, VK_TAB, 0)

[COLOR=#008000]' Hit Enter at Replace Button[/COLOR]
            PostMessage(r, WM_KEYDOWN, VK_RETURN, 0)
            PostMessage(r, WM_KEYUP, VK_RETURN, 0)

[COLOR=#008000]' Refresh the Window[/COLOR]
            PostMessage(r, WM_KEYDOWN, VK_F5, 0)
            PostMessage(r, WM_KEYUP, VK_F5, 0)


        Else
            Beep()
            Beep()
            Beep()
            MessageBox.Show(myWindow & " not accessable.")
        End If



Am I missing something here? Is there a reason why only the refresh part of this sequence is being processed?

This is my first time doing something like this so apologies if the answer is staring me in the face.
 
So i have a temporary solution using sendkeys instead of postmessage.. It did not work at first until i identified a piece of software I had running that was stealing the focus and once i turned off this software, the keys were sent successfully with sendkeys

However still no change with trying to use postmessage.. Only the refresh command "F5" is successfully carried out.. Anyone have any ideas? I would really like to be able to perform the other commands without needing to call focus.. It seems odd to me that the window can refresh without needing focus but can not do the other things.
 
Back
Top