简体   繁体   English

模拟鼠标事件? C#

[英]Simulating mouse events? C#

I'm trying to create a windows forms program which makes the computer act as if I pressed a mouse-button. 我正在尝试创建一个Windows窗体程序,使计算机就像按下鼠标按钮一样。 I want to control the events manually (timing is not decided in advance) and it needs to possible to press and hold, so the mouse-button release should be a separate event. 我想手动控制事件(时间不提前决定),并且需要按住,因此鼠标按钮释放应该是一个单独的事件。

The following information should not change the code, just help you further understand my situation: 以下信息不应更改代码,只是帮助您进一步了解我的情况:

  • The purpose is to allow user input from a Xbox 360 controller (compatible with PC) to steer/control the computer that it is connected to. 目的是允许来自Xbox 360控制器(与PC兼容)的用户输入来控制/控制它所连接的计算机。
  • The best solution that I have so far found is the "Windows.Forms.SendKeys" method but it only works for keyboard events. 到目前为止,我找到的最佳解决方案是“Windows.Forms.SendKeys”方法,但它仅适用于键盘事件。

Thanks in advance! 提前致谢! :D :d

I simulate mouse events like this 我模拟像这样的鼠标事件

    [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
    public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
    private const int MOUSEEVENTF_LEFTDOWN = 0x02;
    private const int MOUSEEVENTF_LEFTUP = 0x04;
    private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
    private const int MOUSEEVENTF_RIGHTUP = 0x10;

usage example: 用法示例:

    public static void leftClick()
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM