SendMessage in vb.net?

Discussion in 'Programming General' started by Terrankiller, Sep 24, 2011.

SendMessage in vb.net?
  1. Unread #1 - Sep 24, 2011 at 7:13 AM
  2. Terrankiller
    Joined:
    May 7, 2005
    Posts:
    1,286
    Referrals:
    1
    Sythe Gold:
    1

    Terrankiller Ex-Administrator
    Retired Administrator Visual Basic Programmers

    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.
     
  3. Unread #2 - Sep 24, 2011 at 11:47 AM
  4. blindkilla
    Joined:
    Jun 22, 2005
    Posts:
    1,896
    Referrals:
    0
    Sythe Gold:
    6

    blindkilla Guru
    $25 USD Donor New

    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.
     
  5. Unread #3 - Sep 24, 2011 at 4:52 PM
  6. Nullware
    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0

    Nullware Guru

    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
     
  7. Unread #4 - Sep 24, 2011 at 11:03 PM
  8. Terrankiller
    Joined:
    May 7, 2005
    Posts:
    1,286
    Referrals:
    1
    Sythe Gold:
    1

    Terrankiller Ex-Administrator
    Retired Administrator Visual Basic Programmers

    SendMessage in vb.net?

    Probably should of checked that lol. Now I got to figure out other stuff :(.
     
< Novice C++ question | Java Script Help >

Users viewing this thread
1 guest


 
 
Adblock breaks this site