VS 2010 Send Message: KeyDown, KeyUp to an Application?-VBForums
Results 1 to 5 of 5

Thread: Send Message: KeyDown, KeyUp to an Application?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    75

    Send Message: KeyDown, KeyUp to an Application?

    Been searching for awhile on how to do this...

    Question:

    How do you make Visual Basic (2010) Send Message: KeyDown, KeyUp to an Application?

    Note: SendKeys.Send("{Down}") etc. won't work.

    Example Script: (This Works, But Only Does Key Press Events)

    Code:
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            Try
                AppActivate(1612) ' App ID
                SendKeys.Send("{" & C1.Text & "}") ' Keys To Send
            Catch ' Error Handling
                MsgBox(ErrorToString) ' Error Message If Any
            End Try
        End Sub
    Any Help Would Be Greatly Appreciated, Thanks

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Send Message: KeyDown, KeyUp to an Application?

    SendKeys.Send(Keys.Down)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: Send Message: KeyDown, KeyUp to an Application?

    You can use the window handle of the control (not the one of the application, nor the process handle). Some controls also have an ID that does not change between restarts so it is possible to use that instead whenever possible.

    The constants of the different keys are in winuser.h
    vb.net Code:
    1. Private Declare Auto Function SendMessageKeyUpDown Lib "user32" Alias "SendMessage" _
    2.     (ByVal hwnd As IntPtr, ByVal wMsg As Integer, _
    3.     ByVal wParam As Integer, ByVal lParam As IntPtr) As IntPtr
    4.  
    5.  
    6.     Const WM_KEYDOWN As Integer = &H100
    7.     Const WM_KEYUP As Integer = &H101
    8.  
    9.  
    10.     'this will send a down arrow
    11.     Const VK_DOWN As Integer = &H28
    12.  
    13.  
    14.         'for first time experiments try to get the handle manually, using C++ or some clone thereof
    15.         Dim ControlWindowHandle As IntPtr = CType(TheHandleAsInteger, IntPtr)
    16.  
    17.         'The actual messages
    18.         SendMessageKeyUpDown(ControlWindowHandle, WM_KEYDOWN, VK_DOWN, IntPtr.Zero)
    19.         SendMessageKeyUpDown(ControlWindowHandle, WM_KEYUP, VK_DOWN, IntPtr.Zero)
    VB 2005, Win Xp Pro sp2

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2012
    Posts
    75

    Re: Send Message: KeyDown, KeyUp to an Application?

    Thanks Half, Looks Like What I'm Looking For. +rep

    Dunfiddin: SendKeys.Send(Keys.Down) sends the number 40 :X

    Windows Handle Isnt To Hard In VB:

    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindow(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
        Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, ByVal childAfter As IntPtr, ByVal lclassName As String, ByVal windowTitle As String) As IntPtr
        End Function
    
        Dim topWindow As IntPtr = FindWindow("classname", "windowcaption")
        Dim childWindow As IntPtr = FindWindowEx(topWindow, New IntPtr(0), "classname", "windowcaption")
    Last edited by VBbbq; Nov 7th, 2012 at 05:13 PM.

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Send Message: KeyDown, KeyUp to an Application?

    SendKeys.Send(Keys.Down) sends the number 40 :X
    Yes it does. Forgot the conversion. It's been one of those days!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.