0

What I want, is to make the computer think I am physically moving the mouse down toward me. I am making a program that should send input into a game. I have succeded to make it receive keyboard input programmatically, but encounter problems with the mouse.

When playing the game normally, it uses a first person perspective with a dot in the middle. When opening menus or maps, it has a cursor, but only then. I have tested several suggestions for mouseinputs, but the game only responds when I am in a mode that uses a cursor that it then moves, like the map or menu, but not in other cases.

What I want, is for my avatar to look down when I am in first perspective normal play mode, which it does when I move the mouse down. From this I infer, any method that that tries to move a cursor is ineffective, since the game mode doesn't have a normal cursor. I need to make the computer itself think that the mouse has been physically moved, never mind cursor.

(For reference, one of the things I have tried is https://gist.github.com/Harmek/1263705), and other examples of using imported SendInput.

Netsrym
  • 11
  • 1
  • 1
  • 1
    Your question is not really answerable right now. So you already tried some stuff. What happened, and what did you expect to happen? – GvS Sep 02 '19 at 19:28
  • 2
    It's very common for first person shooters games to lock mouse input while character is not in menu. Have you looked at `mouse_event` https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-mouse_event – Riddell Sep 02 '19 at 19:33

2 Answers2

2

There are a few ways you can achieve what you want.I've used InputSimulator before and it worked well. You can see example use of it in this question Moving Mouse between to points linearly.

Other methods:

Send Input (Tried): Most basic way to emulate key press and mouse movements. Most games block these nower days due to abuse from scripts i.e. bots, macros, anti-aim/recoil...

Mouse Event: You can find the C++ reference on Microsoft Docs. You can P/Invoke the method and use it. Example Usage Source Code

Game Internal: You find the game angles to your local player in memory and write the new game angles memory you want to look at. There are many examples around on Github.

AutoIT: You can attempt to try it with AutoIT if that works understand how it emulates mouse movement.

Kernel: You can find a great example on this stack overflow question. Thanks to @CaiusJard

Other Sources Simulate keyboard/mouse input in C#


Additional Thoughts

The problem I believe you're going to run into is most of these methods are classed as software inputs which many games and game engines block for security purposes. I believe to truly emulate hardware input at a level you'd need to use Arduino or actual custom hardware as I've never found an unblockable method.

Riddell
  • 1,429
  • 11
  • 22
2

All windows events come from a big queue that windows loops over. You can read and post to the queue you can post mouse events by accessing user32.dll like this:

[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
   public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

in windows all events have hard coded values, in c# we would group them in an enum I guess however in the end it's all just a number, in your code define:

   private const int MOUSEEVENTF_LEFTDOWN = 0x02;
   private const int MOUSEEVENTF_LEFTUP = 0x04;
   private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
   private const int MOUSEEVENTF_RIGHTUP = 0x10;

Then you can play mouse and send the clicks like so:

   public void DoMouseClick(int mouseX, int mouseY)
   {
      mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, mouseX, mouseY, 0, 0);
   }

You can call about all windows dll's by linking them. PostMessageA is no different.It can be used to send about all there is (did you klick the above link?). You can look at the documentation of PostMessageA here. There is a nice link & sample website called pinvoke.net that has quite a few of them.

Please note that it is possible for your antivirus to notice your application when you are posting mouse clicks, if possible try and call the delegate rather then sending mouse coordinates.

If you would like to do automated UI testing then there are plenty of tools that can do that for you. have a look at this blog

Walter Verhoeven
  • 3,867
  • 27
  • 36