• Log in or sign up for an account here: https://www.sythe.org/login

SendMessage in vb.net?

Terrankiller

Ex-Administrator
Joined
May 7, 2005
Sythe Gold
1
Messages
1,286
Location
California
SendMessage in vb.net?
It might just be because I am too tired to figure out what I am doing wrong. I am attempting to use the SendMessage API to simulate keystrokes. Any suggestions? I will sleep on this and check this thread tomorrow.

Code:
'Declaration

Public Declare Auto Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

'Constants

Public Const WM_KEYDOWN As Integer = &H100
Public Const WM_KEYUP As Integer = &H101
Public Const WM_CHAR As Integer = &H102

'TypeKeys Function
'Why does this not work? Freaking tired.
Public Sub TypeKeys(ByVal hWND As IntPtr, ByVal sText As String, ByVal iWait As Integer)

Try

For i = 1 To sText.Length

Call SendMessage(hWND, WM_KEYDOWN, Asc(Mid(sText, i, 1)), 0)
Call SendMessage(hWND, WM_CHAR, Asc(Mid(sText, i, 1)), 0)
Call SendMessage(hWND, WM_KEYUP, Asc(Mid(sText, i, 1)), 0)

Next i

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Edit:

I currently think as of this moment:

Call SendMessage(hWND, WM_KEYDOWN, Asc(Mid(sText, i, 1)), 0)

>>Asc(Mid(sText, i, 1))<<

Is the problem. I am just too tired to figure out how to convert that value into an acceptable parameter in order to execute the SendMessage method.

Also:

It might also be the API declaration itself that is the problem.

Another Also:

I think I will have to read up on this method.
 
SendMessage in vb.net?
I've had problems with things such as simulating keystrokes with .NET. A lot of the time the window needs to have focus or not be minimized.

The only thing I can think of is maybe you need to send a the ascii keycode for a key instead of WM_KEYUP although that should be an enum with keycodes.

Try asking on stackoverflow, I'm sure you'll get some working solutions there.
 
SendMessage in vb.net?
Are you maybe not specifying the handle of the exact control you want the message to be sent to and just using the form`s hWND.

This works for me using your code unchanged.
Code:
    Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
        TypeKeys(Me.TextBox1.Handle, "Testing", 0)
    End Sub
 
SendMessage in vb.net?
Probably should of checked that lol. Now I got to figure out other stuff :(.
 
Runescape Gamblingrunescape gambling
Back
Top