PDA

View Full Version : Real time simulated Mouse and Keyboard input



treluk
06-12-2011, 06:17 PM
So I've been putzing around with my C# bot application a lot lately. After a night out with geek friends and a few beers got my mind rolling on a few different topics to consider. The first one being realistic mouse and keyboard inputs to the active window.

So I decided that I didn't want to have the C# program load different macros to perform mouse and keyboard inputs and that I was going to programmatically make them in C#.



public void RelocateActiveWindow();
{
SetCursorPos(240, 110);
mouse_event(MouseEventType.LeftDown, Cursor.Position.X, Cursor.Position.Y, 240, 110);
System.Threading.Thread.Sleep(1000);
Cursor.Position = new System.Drawing.Point(41, 18);
mouse_event(MouseEventType.LeftUp, Cursor.Position.X, Cursor.Position.Y, 41, 18);
}



Is a generalization of how I can grab a window, and drag it to where i want it to go, but it moves SO fast. Anyone know a method to slow it down to the point it looks realistic? And if i can be done, does it send the appropriate IO sequences to the program window so that it looks realistic from a 3rd party standpoint? Like i'm assuming when you move the mouse, it actually inputs/outputs the exact pixel for the entire length of the mouse movement call, which would be impossible to replicate with my knowledge in C#.

Next, the way i input keyboard presses also worries me. I'm currently a method that I'm sure is not the most appropriate way to be doing this.




SendKeys.Send("testingtesting123");



Does anyone have any ideas or documentation outlining better ways of handling this within a C# program?

Thanks in advance for any suggestions.

tehgeek
06-12-2011, 07:30 PM
I had this problem with my macro as well, and my solution was to do simple math on each cord (+1 or -1 based on current mouse position), and move it 1 pixel each at a time...

I dont write C, but I will try to lay out how I have it working

Current Mouse Position, X into Var %currentmousex, Y into var %currentmousey
Where I want to move the mouse, X into var %movemousex, Y into %movemousey

Then Start the IF Statements and Math
IF %currentmousex is greater than %movemousex
%currentmousex - 1
IF %currentmousex is less than %movemousex
%currentmousex + 1
(repeat with the Y Cords)
Move the mouse to the modified cords of %currentmousex, %currentmousey
repeat

That is how I corrected that. You can also throw in random delays between each pixel move to make the timing more random each and every time.

I hope that is easy for you to understand

treluk
06-12-2011, 07:57 PM
Cool. Thats a neat concept, and I definately understand what you're doing, I am going to try this out, but i'm afraid it wont solve the problem i'm having of the mouse not "ACTUALLY" being at that pixel, it will move virtually, but my X/Y coordinates display program only shows the mouse really moving when its clicked on the button, but not until then. Have any ideas for keyboard commands? :)

Jabaa
06-12-2011, 09:45 PM
write it on a for, and use a sleep, 1 for fast 3 or 5 for slow movement

treluk
06-13-2011, 12:50 AM
write it on a for, and use a sleep, 1 for fast 3 or 5 for slow movement

Thanks man :) Also, what kind of sleep are you using, i'm only familiar with the System.Threading.Thread.Sleep(MS) command, but this seems to hang the entire form until it's done sleeping. I'd like to invoke a background thread for the mouse and keyboard hooks?

Dodgy
06-13-2011, 02:26 AM
Thanks man :) Also, what kind of sleep are you using, i'm only familiar with the System.Threading.Thread.Sleep(MS) command, but this seems to hang the entire form until it's done sleeping. I'd like to invoke a background thread for the mouse and keyboard hooks?

Your form hangs because you are using a thread sleeping function in the form UI thread. You should run your main code inside a separate thread (or easier a background worker).

A quick (but hacky) fix is to call Application.DoEvents() inside your loop, which basically allows the form UI to do whatever it needs to and prevents hanging. But I'd go with threading (however in some cases threading is not always viable).

The open source I posted has the code which does exactly what you are asking, why not just use that part of it =)

treluk
06-13-2011, 02:34 AM
I intend on it once i've gotten that far into it. LOL - Baby steps.

Jabaa
06-13-2011, 08:15 AM
i use a Threading.Thread.Sleep(2) for normal mousemoves.

for my application i use 2 background worker, one for the shield monitoring and one for the mining cycle.
for the hotkey handling i use the main application

Slav2
06-22-2011, 02:31 PM
I think best way to code anything like this in C# is to learn C++ a bit and look into autohotkey source code. To move mouse slower AHK uses incremental mouse movement. If X is mouse speed, then second point of the mouse coord should be at the point where total path is divided by X. Remained path devided by X again on the second iteration and again and again until minimal path became less then a few pixels. If you make delay between iterations like 10 ms and speed like 20, you will have movement very close to real mouse movement with hand. I would recommend to use 10 ms delay because if you plan to use your bot on windows XP there is no shorter delay. 10ms is smallest and sometimes not very precise (depending on CPU).

I would recommend to start from http://inputsimulator.codeplex.com/
Nice library with one exception. Dont use unicode characters with modifiers to send hotkeys.