How to simulate mouse clicks and keystrokes in F #

I am making a program that should move the mouse and automatically click in the places where I specify in the code. Right now, I managed to move the cursor using this line: Cursor.Position <- System.Drawing.Point (x, y)

What I haven't figured out yet is how to simulate mouseclicks or keypresses. The only thing I found about this is the SendKeys class from MSDN (http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx). I tried to simulate keypress with this class, but I get an error at runtime.

Used string: SendKeys.Send ("{ENTER}")

The error message I get is: "SendKeys cannot work inside this application because the application does not process Windows messages. Either modify the application to process messages, or use the SendKeys.SendWait method."

So, I replaced it with the SendWait method, but it still does not send keypress. How should I do it? What I really want the finished program to be able to execute is sending keys and mouse clicks to another program that is already open in the background. For example, automatically paint the image in Paint.

+5
source share
3 answers

Win32, Windows - SendInput. . (. ). VS2010 #, subversion .

Win32, , API- SendInput F #. .

open System
open System.Runtime.InteropServices

type MOUSEINPUT = struct
    val dx:int
    val dy:int
    val mouseData:int
    val dwFlags:int
    val time:int
    val dwExtraInfo:int
    new(_dx, _dy, _mouseData, _dwFlags, _time, _dwExtraInfo) = {dx=_dx; dy=_dy; mouseData=_mouseData; dwFlags=_dwFlags; time=_time; dwExtraInfo=_dwExtraInfo}
end

type INPUT = struct
    //need to escape traditional INPUT identifier here since "type" is a reserved word
    //though could use any other identifier name if so desired
    val ``type``:int //0 is mouse
    val mi:MOUSEINPUT
    new(_type, _mi) = {``type``=_type; mi=_mi}
end

let MOUSEEVENTF_RIGHTDOWN = 0x0008
let MOUSEEVENTF_RIGHTUP = 0x0010

[<DllImport("user32.dll", SetLastError=true)>]
extern uint32 SendInput(uint32 nInputs, INPUT* pInputs, int cbSize)

let mutable inputRightDown = INPUT(0, MOUSEINPUT(0, 0, 0, MOUSEEVENTF_RIGHTDOWN, 0, 0))
let mutable inputRightUp = INPUT(0, MOUSEINPUT(0, 0, 0, MOUSEEVENTF_RIGHTUP, 0, 0))

SendInput(uint32 1, &&inputRightDown, Marshal.SizeOf(inputRightDown))
SendInput(uint32 1, &&inputRightUp, Marshal.SizeOf(inputRightUp))

, , , SendMessage/PostMessage API. , , , SendInput . - .

+3

SendInput Win32 API.

+2
open System.Runtime.InteropServices

module InteropWithNative = 
    [<DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)>]
    extern void mouse_event(System.Int64, System.Int64, System.Int64, System.Int64, System.Int64)

    let MOUSEEVENTF_LEFTDOWN    = 0x02L
    let MOUSEEVENTF_LEFTUP      = 0x04L
    let MOUSEEVENTF_RIGHTDOWN   = 0x08L
    let MOUSEEVENTF_RIGHTUP     = 0x10L

    let  MouseLeftClick () = 
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0L, 0L, 0L, 0L)
        mouse_event(MOUSEEVENTF_LEFTUP, 0L, 0L, 0L, 0L)

:

    InteropWithNative.MouseLeftClick ()
0

All Articles