简体   繁体   English

如何在C#中通过按键盘按钮模拟鼠标单击

[英]how do you simulate a mouse click by pressing a keyboard button in C#

I have been trying to find a way to make a mouse click programmatically but the results I find are quit strainous based on my level. 我一直在尝试找到一种以编程方式使鼠标单击的方法,但是根据我的水平,我发现结果并不十分理想。 I know how to position the mouse and everything but the click. 我知道如何放置鼠标以及除点击以外的所有内容。 I also know that there is a way to simulate a keyboard press with key events. 我也知道,有一种方法可以模拟带有按键事件的键盘按下。 So this got me to wonder, is there a way to make the mouse click by pressing a keyboard key? 所以这让我想知道,是否有一种方法可以通过按下键盘按键来使鼠标单击? I want to do this because I'm working on a educational project that shows beginners how to do simple functions on the computer, like how to create a file or open certain programs, so I need the mouse click to work based on the screen and outside of my app. 之所以要这样做,是因为我正在从事一个教育项目,该项目向初学者展示了如何在计算机上执行简单功能,例如如何创建文件或打开某些程序,因此我需要单击鼠标才能在屏幕和在我的应用程序之外。 Is this possible? 这可能吗? all help will be appreciated. 所有帮助将不胜感激。

you can simulate Mouse Click though following code 您可以通过以下代码模拟鼠标单击

using System.Runtime.InteropServices;
[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);

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

public void DoMouseClick()
    {
        //Call the imported function with the cursor's current position
        int X = Cursor.Position.X;
        int Y = Cursor.Position.Y;
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, 0);
    }

mouse_event actually perform the moouse click. mouse_event实际上执行了moouse点击。

This is not ac# answer but if you are trying to only automate stuff for teaching, this might help. 这不是ac#答案,但是如果您尝试仅自动进行教学,这可能会有所帮助。 You can capture a video of all your actions and highlight mouse clicks. 您可以捕获所有操作的视频并突出显示鼠标单击。 A good software for that is the Camtasia studio. Camtasia工作室就是一个很好的软件。 It is a paid software but there are other free screen recording software out there. 这是一个付费软件,但那里还有其他免费的屏幕录制软件。

If you really want to just automate actual mouse clicks, try using Autoit http://www.autoitscript.com/site/autoit/ 如果您真的只想自动实际点击鼠标,请尝试使用Autoit http://www.autoitscript.com/site/autoit/

for animations. 用于动画。 You can script anything from keyboard keypresses to mouse clicks in any area of the screen. 您可以在屏幕的任何区域编写任何脚本,从键盘按键到鼠标单击。

In case you want to try it out, to click on any position, all you would do is 如果您想尝试一下,单击任何位置,您所要做的就是

MouseClick("primary", x-coordinate, y-coordinate, number-of-clicks) 
// number of clicks = 2 for double click
$pos = MouseGetPos()
$pos[0] // contains the x coordinate of current mouse pos
$pos[1] // contains the y coord of current position

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

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