Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Need Help on Finishing Custom Module for SendInput

OK, I have been trying to do this for several weeks now on and off, and I THOUGHT I had it figured out. I used several examples and have tried to mold everything into a single module. I ALMOST have it doing all the jobs I want it to except for one, but when I try to test the aspects that I have done already it does not work partially hehe.



OK, I will list both the Module, as well as the short and simple form event that I am using to test it. I will also attach the Project if I can, which is done in VS 2005.



What I want it to do:

One single module to provide several simple to use Subs/Functions which are listed below.



1. Send(Text) to send any text string.

2. MoveMouse(Pointer) to move the mouse pointer.

3. ClickMouse(Button, Number of clicks) to do the button clicks.



Number 1 works now, at least the way I am testing it does. 2 and 3 do NOT work with my project the way it is. I don't get any errors, all that happens is that the mouse pointer changes to the hourglass for the duration of the program (which it should be moving to a spot +50, +50 from it's current location and left clicking once).



LAST THING!

One aspect I wish to add which I dont know how to do yet is to not only be able to send(text) but ALSO have the Send Sub look for Button Press Commands as well. (For example {Enter}, {F1}). I would like to be able to do: Send("Hello World! {Enter}") and have it work. It will saend the "Hello World! part but I am not good enough yet to do all the string stuff to ID the Button Presses and add them in.



OK, now for my code:



SendInput Module:



'===============================================================

'

' SENDKEY MODULE

' by: BILL C.

' date: DECEMBER 23, 2006

' IDE: VISUAL STUDIO .NET 2005 / VISUAL BASIC

'

' SUBS:

' SEND(TEXT AS STRING)

' MOVEMOUSE(COORDINATES AS POINTER)

' CLICKMOUSE(BUTTON AS STRING, NUMBEROFCLICKS AS INTEGER

'

'===============================================================)



OptionExplicitOn

OptionStrictOn

Imports System.Runtime.InteropServices

Imports System.Windows.Forms

Imports System.Drawing

Imports System.Diagnostics

Module SendInput

'===========================================================

'

' ENUMERATIONS

'

'===========================================================

PrivateEnum MOUSEEVENTF AsInteger

MOVE = &H1

LEFTDOWN = &H2

LEFTUP = &H4

RIGHTDOWN = &H8

RIGHTUP = &H10

MIDDLEDOWN = &H20

MIDDLEUP = &H40

XDOWN = &H80

XUP = &H100

VIRTUALDESK = &H400

WHEEL = &H800

ABSOLUTE = &H8000

EndEnum

PrivateEnum KEYEVENTF AsInteger

EXTENDEDKEY = 1

KEYUP = 2

[UNICODE] = 4

SCANCODE = 8

EndEnum

PrivateEnum InputType AsInteger

Mouse = 0

Keyboard = 1

Hardware = 2

EndEnum

'===========================================================

'

' STRUCTURES

'

'===========================================================

PrivateStructure Input

Dim dwType AsInteger

Dim mi As MOUSEINPUT

Dim ki As KEYBDINPUT

Dim hi As HARDWAREINPUT

EndStructure

PrivateStructure KEYBDINPUT

Dim wVk As Int16

Dim wScan As Int16

Dim dwFlags As KEYEVENTF

Dim time As Int32

Dim dwExtraInfo As IntPtr

EndStructure

PrivateStructure HARDWAREINPUT

Dim uMsg As Int32

Dim wParamL As Int16

Dim wParamH As Int16

EndStructure

PrivateStructure MOUSEINPUT

Dim dx As Int32

Dim dy As Int32

Dim mouseData As Int32

Dim dwFlags As MOUSEEVENTF

Dim time As Int32

Dim dwExtraInfo As IntPtr

EndStructure

'===========================================================

'

' API CALLS

'

'===========================================================

PrivateDeclareFunction SendInput Lib"user32.dll" (ByVal cInputs AsInteger, _

ByRef pInputs As Input, ByVal cbSize AsInteger) AsInteger

'===========================================================

'

' FUNCTIONS & SUBS (SUPPORT)

'

'===========================================================

PrivateSub DoMouse(ByVal flags As MOUSEEVENTF, ByVal newPoint As Point)

Dim input AsNew Input

Dim mi AsNew MOUSEINPUT

input.dwType = InputType.Mouse

input.mi = mi

input.mi.dwExtraInfo = IntPtr.Zero

input.mi.dx = CInt(newPoint.X * (65535 / Screen.PrimaryScreen.Bounds.Width))

input.mi.dy = CInt(newPoint.Y * (65535 / Screen.PrimaryScreen.Bounds.Height))

input.mi.time = 0

input.mi.mouseData = 0

input.mi.dwFlags = flags

Dim cbSize AsInteger = Marshal.SizeOf(GetType(Input))

Dim result AsInteger = SendInput(1, input, cbSize)

If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)

EndSub

PrivateSub DoKeyBoard(ByVal flags As KEYEVENTF, ByVal key As Keys)

Dim input AsNew Input

Dim ki AsNew KEYBDINPUT

input.dwType = InputType.Keyboard

input.ki = ki

input.ki.wVk = Convert.ToInt16(key)

input.ki.wScan = 0

input.ki.time = 0

input.ki.dwFlags = flags

input.ki.dwExtraInfo = IntPtr.Zero

Dim cbSize AsInteger = Marshal.SizeOf(GetType(Input))

Dim result AsInteger = SendInput(1, input, cbSize)

If result = 0 Then Debug.WriteLine(Marshal.GetLastWin32Error)

EndSub

'=======================================================

'

' FUNCTIONS & SUBS (MAIN)

'

' Send(Message As String)

' MoveMouse(Location As Point)

' ClickMouse(Button As String, Clicks As Integer)

'

'=======================================================

PublicSub Send(ByVal Text AsString)

ForEach c AsCharIn Text

Dim key AsUInteger = CUInt(Convert.ToInt16(c))

DoKeyBoard(0, CType(key, Keys))

DoKeyBoard(KEYEVENTF.KEYUP, CType(key, Keys))

Next

EndSub

PublicSub MoveMouse(ByVal Loc As Point)

DoMouse(MOUSEEVENTF.MOVE Or MOUSEEVENTF.ABSOLUTE, Loc)

EndSub

PublicSub ClickMouse(ByVal Button AsString, ByVal Clicks AsInteger)

Dim A AsInteger

SelectCase Button

Case"Left"

For A = 1 To Clicks

DoMouse(MOUSEEVENTF.LEFTDOWN, New Point(0, 0))

DoMouse(MOUSEEVENTF.LEFTUP, New Point(0, 0))

Next

Case"Middle"

For A = 1 To Clicks

DoMouse(MOUSEEVENTF.MIDDLEDOWN, New Point(0, 0))

DoMouse(MOUSEEVENTF.MIDDLEUP, New Point(0, 0))

Next

Case"Right"

For A = 1 To Clicks

DoMouse(MOUSEEVENTF.RIGHTDOWN, New Point(0, 0))

DoMouse(MOUSEEVENTF.RIGHTUP, New Point(0, 0))

Next

EndSelect

EndSub

EndModule



Simple Form for testing:

OptionExplicitOn

OptionStrictOn

Imports System.Threading

Imports System.Windows.Forms

Imports System.Drawing

Imports System.Runtime.InteropServices

PublicClass Form1

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim AppPath As String = "C:\Windows\Notepad.Exe"

Dim Loc As Point

Loc.X = 5

Loc.Y = 5

Process.Start(AppPath)

Thread.Sleep(2000)

Send("Hello World From Tiger to You!")

Thread.Sleep(2000)

MoveMouse(Loc)

Thread.Sleep(1000)

DoUntil Loc.Y > 1100

Loc.X += 50

Loc.Y += 50

MoveMouse(Loc)

Thread.Sleep(250)

ClickMouse("Left", 1)

Thread.Sleep(1000)

Loop

Me.Close()

EndSub

EndClass

THANK YOU FOR ANY HELP YOU CAN OFFER!
[8749 byte] By [Leopardfist] at [2007-11-11 10:04:28]