Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

generating mouse events OUTSIDE my own window

Status
Not open for further replies.

andreiaron

Programmer
Jul 18, 2002
6
RO
Hi, i need to generate mouse events outsidee the windows i create. (preferably c#, but a c++ solution will do as well)

i need to create a program that will "move" the mouse pointer, and click on windows (not windows that I myself create) and type random keys around the OS, but without using the physical mouse or keyboard. I am using this for a communications program that will give remote acces to the mouse and keyboard.

I know how to do this inside forms (windows) that i myself create, but i have no idea how to capture/create mouse events simply inside the (eg. outside the windows my program creates).

thanks, and i would appreciate the help,

Andrei
 
Check out the AttachThreadInput API call. Combine it with SendInput (assuming W98, NT4 SP3 or better, W2K or WinXP), and you should have a solution. Or use keyb_input and mouse_input.

Only thing you can't do is piggyback onto a system thread.

Here's an example in VB, which should give you enough for a C# solution:
[tt]
Option Explicit
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
Private Declare Function GetThreadDesktop Lib "user32" (ByVal dwThread As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
Private Declare Function apiSetFocus Lib "user32" Alias "SetFocus" (ByVal hwnd As Long) As Long
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down

Private Sub Command1_Click()
Shell "notepad", vbNormalNoFocus
End Sub

Private Sub Command2_Click()
Dim hwnd As Long
Dim TargetThread As Long
Dim result As Long
Dim strTest As String
Dim lp As Long


hwnd = FindWindow(vbNullString, "Untitled - Notepad")
TargetThread = GetWindowThreadProcessId(hwnd, 0&)
AttachThreadInput GetCurrentThreadId, TargetThread, True ' Attach
apiSetFocus hwnd
strTest = UCase("Test")
For lp = 1 To Len(strTest)
keybd_event Asc(Mid$(strTest, lp, 1)), 0, 0, 0
Next

mouse_event MOUSEEVENTF_MOVE + MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0
mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
AttachThreadInput GetCurrentThreadId, TargetThread, False ' detach
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top