0

Hi i need help with my program, i made a console application in c# and Leap Motion Device, my program simulates a virtual mouse, moving the cursor and Left/Right click, using

[System.Runtime.InteropServices.DllImport("user32.dll")]

and

public static void MouseClickLeft(int x, int y) 
        {

            mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
        }

        public static void MouseClickRight(int x, int y)
        {
            mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0);
            mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
        }

I need run this program with another app, but the mouse control only works if the Console application its running as the main window, its stops working when i do the left click in the desktop or another app. Im beginner in programming with c#, there is anything that i can do to make this running in the background and still moving the mouse?

2 Answers2

2

To receive Leap Motion tracking data when your application is not the foreground application, you have to set the background frames policy:

controller.SetPolicyFlags(Controller.PolicyFlag.POLICYBACKGROUNDFRAMES);

You can call this function anytime, but the result is asynchronous, so the policyFlags property won't show the change right away.

Charles Ward
  • 1,388
  • 1
  • 8
  • 13
0

Take a look at my Key Clone application for an example how this is done: https://github.com/Mataniko/WowKeyclone/blob/master/Interop.cs

In my case i'm setting a hook with WH_KEYBOARD_LL in your case use WH_MOUSE_LL.

Additional information on setting hooks can be found at: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644959(v=vs.85).aspx

Let me know in a comment if you need more information.

Mataniko
  • 2,212
  • 16
  • 18
  • You can basically use the entire class with a couple of edits – Mataniko Jun 26 '13 at 05:20
  • I take a look at your program , but i cant understand, i want to make this console app work with a Game Maker .exe, like the output of the mouse app turns into the input of the game. – Hugo Frauches Jun 26 '13 at 14:11
  • You will have to hook into that window handle and send input. This is not a straight forward thing to do. Look at the KeyHandler.cs file in the same project on how to send those events to another window. There are also a lot of examples on SO: http://stackoverflow.com/questions/10355286/programmatically-mouse-click-in-another-window – Mataniko Jun 26 '13 at 15:05
  • i used `[DllImport("user32.dll")] static extern UInt32 SendInput(UInt32 nInputs, [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] INPUT[] pInputs, Int32 cbSize);` and System.Windows.Forms with Cursor.Position and it works, thx! – Hugo Frauches Jun 27 '13 at 00:40
  • Great, if your problem is solved make sure you accept an answer, good luck with your application. – Mataniko Jun 27 '13 at 06:15