Simulating mouse and keyboard

hellven

Member
Joined
Oct 12, 2006
Messages
6
Programming Experience
Beginner
I need to do this for a little project of mine but I don't know how:

1. Bring another program that is already running into focus. (I didn't make this second program).
2. Find the first textbox on this other program and type some text into it.
3. Have my program simulate a user typing.

Is this possible and where do I start?

So for example. Assume that the other program is notepad, and that is is already running. I need my application to bring notepad into focus, type some text, and then press ENTER. No, the application that I want to do this for isn't notepad but I thought you'd understand what I wanted better if I gave an example.
 
You have to use sendmessage API. For example if you want to type the letter A into the Notepad program.

VB.NET:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
    (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr
    Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Public Const WM_CHAR = &H102

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim hWnd As IntPtr = FindWindow("Notepad", vbNullString)
        Dim ExhWnd As IntPtr = FindWindowEx(hWnd, IntPtr.Zero, "Edit", vbNullString)
        SendMessage(ExhWnd, WM_CHAR, Keys.A, 0)
    End Sub

Make sure notpad is running when trying out this code.
 
1. Activating other window is not difficult, it can be done with WSH script method Shell.AppActivate or Win32 method SetForegroundWindow

2. Finding a textbox may be very difficult, even if you manage to iterate all child classes textboxes may have various class names and may even change each time application executes. If you're targeting a particular application it would be easier to just send the necessary number of TAB keys or some other key combination that will get to that textbox field. You may not be able to accomplish this.

3. Sending keys is done with .Net class SendKeys, it sends the specified keypresses to active window.
 
John, would you be kind enough to provide some examples?

I'm really struggling here.

Luke I played around with that code and sure enough it works for notepad but when I tried it for other applications I couldn't get it to work.
 
Not sure about your request here, the target environment and all. The SendKeys class is really easy to work with as you've seen from all the examples on the web.

Below is the 'hello notepad' example. It finds a Notepad process, activates the window, waits 200ms to give the window a split second to pop out, sends 'hello', then sends 'Ctrl+A' which is the shortcut for 'Select All', then activate help menu with shortcut 'Alt+H' and sends 'DownArrow' to bring attention to About menuitem and presses Enter to show that dialog.
VB.NET:
[SIZE=2][COLOR=#0000ff]Private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) _[/SIZE]
[SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] Button1.Click[/SIZE]
[SIZE=2]automate()[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
 
[SIZE=2][COLOR=#0000ff]Declare [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] SetForegroundWindow [/SIZE][SIZE=2][COLOR=#0000ff]Lib[/COLOR][/SIZE][SIZE=2][COLOR=#800000]"user32.dll"[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] hwnd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32) [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Int32[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] automate()[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] pcs() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Process = Process.GetProcessesByName([/SIZE][SIZE=2][COLOR=#800000]"notepad"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] pcs.Length > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2] SetForegroundWindow(pcs(0).MainWindowHandle.ToInt32)[/SIZE]
[SIZE=2] Threading.Thread.Sleep(100)[/SIZE]
[SIZE=2] SendKeys.Send([/SIZE][SIZE=2][COLOR=#800000]"hello^a%h[SIZE=2][COLOR=#800000]{DOWN}~[/COLOR][/SIZE]"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
Remember all keystrokes can be sent to automate keying from one control field to another just like using the keyboard in the 'real' world with {TAB} and shortcuts etc, see the linked documentation for how to send special keys.
 
Last edited:
Sorry about the late reply, John I got the following error when I ran your code:

"Value of type "System.IntPtr' cannot be converted to 'Integer'."


The following line was highlighted:

SetForegroundWindow(pcs(0).MainWindowHandle)


What's wrong?
 
ok, convert the handle intptr to int32 then, use the .ToInt32 method. like this ......MainWindowHandle.ToInt32
I've corrected the original code example too.
 
Back
Top