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

发布于 2024-11-27 12:47:36 字数 237 浏览 0 评论 0原文

我一直在尝试找到一种以编程方式单击鼠标的方法,但根据我的水平,我发现结果非常紧张。我知道如何定位鼠标以及除点击之外的所有内容。我还知道有一种方法可以用按键事件模拟​​键盘按下。所以这让我想知道,有没有一种方法可以通过按下键盘按键来让鼠标点击? 我想这样做是因为我正在开展一个教育项目,该项目向初学者展示如何在计算机上执行简单的功能,例如如何创建文件或打开某些程序,因此我需要单击鼠标才能根据屏幕进行工作,并且在我的应用程序之外。这可能吗?所有帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白况 2024-12-04 12:47:36

您可以模拟鼠标单击,但下面的代码

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 实际上执行鼠标单击。

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.

〃安静 2024-12-04 12:47:36

这不是 c# 答案,但如果您只是尝试自动化教学内容,这可能会有所帮助。您可以捕获所有操作的视频并突出显示鼠标点击。 Camtasia studio 是一个很好的软件。这是一个付费软件,但还有其他免费的屏幕录制软件。

如果您确实只想自动执行实际的鼠标点击,请尝试使用 Autoit
http://www.autoitscript.com/site/autoit/

用于动画。您可以在屏幕的任何区域编写从键盘按键到鼠标单击的任何内容的脚本。

如果你想尝试一下,点击任意位置,你所要做的就是

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

This is not a c# answer but if you are trying to only automate stuff for teaching, this might help. You can capture a video of all your actions and highlight mouse clicks. A good software for that is the Camtasia studio. 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/

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文