Results 1 to 2 of 2

Thread: [2.0/3.0] SendMessage or PostMessage to send keystrokes.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2007
    Posts
    26

    [2.0/3.0] SendMessage or PostMessage to send keystrokes.

    I'm wondering how to use SendMessage() or PostMessage() to send keystrokes to an inactive window. Could someone provide me with a simple code that sends a few characters and the enter button to notepad, while it's in the background?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0/3.0] SendMessage or PostMessage to send keystrokes.

    This code is VB but it should be fairly obvious what it's doing:
    vb.net Code:
    1. Dim loginDialogueHandle As IntPtr = IntPtr.Zero
    2.  
    3. 'Get the handle of the POS login dialogue.
    4. Do
    5.     Threading.Thread.Sleep(50)
    6.     loginDialogueHandle = NativeMethods.FindWindow(My.Resources.RmsPosFormClassNameString, "Login")
    7. Loop Until loginDialogueHandle <> IntPtr.Zero
    8.  
    9. 'TODO: Hide login window in release.
    10. 'NativeMethods.ShowWindow(loginDialogueHandle, NativeConstants.SW_HIDE)
    11.  
    12. 'Get the handles of the user ID and password fields and the OK button.
    13. Dim userIDFieldHandle As IntPtr = NativeMethods.FindWindowEx(loginDialogueHandle, _
    14.                                                              IntPtr.Zero, _
    15.                                                              My.Resources.RmsPosTextBoxClassNameString, _
    16.                                                              String.Empty)
    17. Dim passwordFieldHandle As IntPtr = NativeMethods.FindWindowEx(loginDialogueHandle, _
    18.                                                                userIDFieldHandle, _
    19.                                                                My.Resources.RmsPosTextBoxClassNameString, _
    20.                                                                String.Empty)
    21. Dim frameHandle As IntPtr = NativeMethods.FindWindowEx(loginDialogueHandle, _
    22.                                                        IntPtr.Zero, _
    23.                                                        My.Resources.RmsPosFrameClassNameString, _
    24.                                                        String.Empty)
    25. Dim okButtonHandle As IntPtr = NativeMethods.FindWindowEx(frameHandle, _
    26.                                                           IntPtr.Zero, _
    27.                                                           My.Resources.RmsPosButtonClassNameString, _
    28.                                                           "OK")
    29.  
    30. If userIDFieldHandle <> IntPtr.Zero Then
    31.     'Enter the user ID.
    32.     NativeMethods.SendMessage(userIDFieldHandle, _
    33.                               NativeConstants.WM_SETTEXT, _
    34.                               0, _
    35.                               "1")
    36. End If
    37.  
    38. If passwordFieldHandle <> IntPtr.Zero Then
    39.     'Enter the password.
    40.     NativeMethods.SendMessage(passwordFieldHandle, _
    41.                               NativeConstants.WM_SETTEXT, _
    42.                               0, _
    43.                               "password")
    44. End If
    45.  
    46. If okButtonHandle <> IntPtr.Zero Then
    47.     'Click the OK button.
    48.     NativeMethods.SendMessage(okButtonHandle, _
    49.                               NativeConstants.BM_CLICK, _
    50.                               0, _
    51.                               0)
    52. End If
    Here are the API declarations, which are in C#:
    Code:
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent,
    					IntPtr hwndChildAfter,
    					string lpszClass,
    					string lpszWindow);
    
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern int SendMessage(IntPtr hwnd,
    					int wMsg,
    					int wParam,
    					int lParam);
    
    [DllImport("user32.dll", CharSet=CharSet.Auto)]
    public static extern int SendMessage(IntPtr hwnd,
    					int wMsg,
    					int wParam,
    					string lParam);
    You should download and install the API Viewer, which can help you with unmanaged function and constant declarations.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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