0

I've got a problem with simulating in game's process this action: - mouse_down - mouse_move - mouse_up

When I do it manually, that's the spy++ output: enter image description here

mouse_down:

enter image description here

mouse_move:

enter image description here

mouse_up:

enter image description here

So I've tried to simulate that with below code start x,y: (618,392) final x,y: (618,432) but It's not working.

uint Iparm = makeDWord((ushort)x, (ushort)y);
IntPtr xd = new IntPtr(Iparm);
uint Iparm2 = makeDWord((ushort)x2, (ushort)y2);
IntPtr xd2 = new IntPtr(Iparm2);

SendMessage(UltraBot.p, (uint)0x201, (IntPtr)0x1, xd); // down button (start x,y)
SendMessage(UltraBot.p, (uint)0x200, (IntPtr)0x1, xd2); // move (final x,y)
SendMessage(UltraBot.p, (uint)0x202, (IntPtr)0x0, xd2); // up button (final x,y)

Here's the spy++ output after using the code:

enter image description here

I don't really know why it's not working. I've been using this way to simulate keys for some time, actions like: ctrl+q, mouse clicks and so werent any problem. What's more, IT WORKED FOR ME ONCE, but just once. I've stuck in here. Thanks for any help :)

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Patryk
  • 3,042
  • 11
  • 41
  • 83

1 Answers1

2

You cannot simulate mouse input by sending mouse messages (such as WM_LBUTTONDOWN and WM_MOUSEMOVE) using the SendMessage function.

Yes, that's what it looks like is happening when you monitor the messages with Spy++, but there's a lot more going on behind the scenes.

To simulate mouse or keyboard input properly, you need to use the SendInput function. The P/Invoke declaration to call it from C# looks like this:

[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Don't I need game window to be focused while using SendInput? – Patryk May 04 '12 at 07:49
  • No, you do not. That's not how the `SendInput` function works. It is not analogous to `SendMessage`. @pat – Cody Gray - on strike May 04 '12 at 07:50
  • actually, sendMessage works for me now, I've added few sleeps (works even though window is minimized). But I'd try SendInput aswell, if it's better(???) if You gave me an example of using it – Patryk May 04 '12 at 08:00
  • `SendMessage` will *not* work correctly to simulate keyboard and mouse input. If you've made it work, it's only by accident. There's an example in the linked documentation. – Cody Gray - on strike May 04 '12 at 08:00
  • can I specify the window i want to send input in SendInput function and is it possible to send that input even though the window is minimized? I couldn't find anywhere on the net SendInput with arguments including pointer to MainWindowHandle or BaseModule pointer. With SendMessage, I could have send the input relative to the window (eg. mouseclick x=0,y=0 clicked in the left top corner of the game). Is that possible with SendInput? – Patryk May 04 '12 at 08:24
  • No, you do not use `SendInput` to send input to a particular window. You're injecting the input into the input queue. This works at a much lower level than messages, which is why it's not subject to the same shortcomings. You can specify the position of the cursor with `SendInput`, though. That information is contained in the [`MOUSEINPUT` structure](http://msdn.microsoft.com/en-us/library/windows/desktop/ms646273.aspx) that you pass to the function within the `INPUT` structure. – Cody Gray - on strike May 04 '12 at 08:26
  • ok, but is it possible to send that inpit to a minimized window or not, I've read on the net that its impossible and I do not know what to think – Patryk May 04 '12 at 08:42
  • See this question: [SendInput to minimized window](http://stackoverflow.com/q/6165428) – Cody Gray - on strike May 04 '12 at 08:51