模拟鼠标移动 (C#)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1949609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:08:08  来源:igfitidea点击:

Simulating mouse movement (C#)

c#mousemove

提问by Vivelin

How would I go about making a small program that keeps moving the mouse upwards?

我将如何制作一个不断向上移动鼠标的小程序?



For those who would like to know, tt's a tiny one-use thing. I'm playing Star Wars The Force Unleashed. In console versions, you would hold the right stick upwards. In the badconsole port on the PC, you're using your mouse. Maybe you can understand my frustration.

对于那些想知道的人来说,这是一个很小的一次性使用的东西。我在玩星球大战原力释放。在控制台版本中,您将向上握住右摇杆。在PC 上的控制台端口中,您正在使用鼠标。或许你能理解我的无奈。

采纳答案by John Knoeller

SetCursorPoswill do this in unmanaged code. I'm not sure if there's a .NET method to do the same, but you can always use com interop. It's in User32.dll

SetCursorPos将在非托管代码中执行此操作。我不确定是否有 .NET 方法可以做同样的事情,但您始终可以使用 com interop。它在 User32.dll 中

[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);

a little google produces this as a SetCursorPos equivalent for .net

一个小谷歌将其作为.netSetCursorPos 等价物生成

System.Windows.Form.Cursor.Position

回答by Christian V

I don't know the game but internally windows sends out messages for mouse move. These are sent by the SendMessage()API call to the application. In C# you would either have to use the same Win32 API directly or perhaps by creating/sending an event? Not sure how to send an event to another running application though.

我不知道游戏,但在内部窗口发送鼠标移动消息。这些由SendMessage()API 调用发送到应用程序。在 C# 中,您必须直接使用相同的 Win32 API 还是通过创建/发送事件?虽然不确定如何将事件发送到另一个正在运行的应用程序。

The SendMessage() Win32 API call defined in C# would look like this:

C# 中定义的 SendMessage() Win32 API 调用如下所示:

[DllImport("user32.dll")]
        public static extern int SendMessage(
              int hWnd,     // handle to destination window
              uint Msg,     // message
              long wParam,  // first message parameter
              long lParam   // second message parameter
              );

回答by Woot4Moo

Did you try using a controller or joystick?

您是否尝试使用控制器或操纵杆?

http://www.merconnet.com/product_info.php?products_id=44

http://www.merconnet.com/product_info.php?products_id=44

回答by jball

This is a problem for mechanical programming, not CSharp.

这是机械编程的问题,而不是 CSharp。

  • You need to plug in a second mouse, and get a cheap variable speed corded drill.
  • Put a sanding drum on the drill, wrap it with striped paper, and set it to the lowest speed.
  • Then put a bracket on the non-rotating body of the drill that will hold the mouse with its sensor close to the spinning striped paper.
  • Ensure the mouse is facing the right way to simulate an up movement.
  • 您需要插入第二个鼠标,并获得便宜的变速有线电钻。
  • 在钻头上放一个砂鼓,用条纹纸包好,调到最低速度。
  • 然后在钻头的非旋转体上放一个支架,将鼠标及其传感器靠近旋转的条纹纸。
  • 确保鼠标朝向正确的方向来模拟向上运动。

To execute the program, flip the power switch on the power strip the drill is plugged into.

要执行该程序,请翻转电钻插入的电源板上的电源开关。

Edit: if you want more portable code, use a cordless drill. Batteries not included.

编辑:如果您想要更便携的代码,请使用无绳电钻。不包括电池。