【问题标题】:Set mouse position in C# and .Net Core在 C# 和 .Net Core 中设置鼠标位置
【发布时间】:2018-12-25 08:46:16
【问题描述】:

我正在尝试从将在 Windows 和 Linux 上运行的控制台 .NET Core 应用程序设置鼠标光标位置。是否有任何 API 可以让我在两个操作系统上设置位置?

【问题讨论】:

  • 由于驱动程序 AFAIK,Linux 无法很好地设置鼠标位置
  • 我认为这不太可能。您可能需要确定您在哪个平台上并使用 PInvoke 调用适当的系统函数。对于 Linux,我怀疑调用会有所不同,具体取决于所使用的 GUI...
  • 我想了这么多,有没有 X11 函数可以做到这一点?

标签: c# linux windows .net-core mouse


【解决方案1】:

使用 .NET Core 在 Windows 上获取鼠标光标位置可以在 user32.dll 中使用 GetCursorPos

看这个例子:

class Program
{
    [DllImport("user32.dll")]
    private static extern bool GetCursorPos(out Point lpPoint);

    private static void Main()
    {
        Point mouse = default;
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            GetCursorPos(out mouse);
        }
        else
        {
            // How to do on Linux and OSX?
        }
        Console.WriteLine($"Mouse X:{mouse.X} Y:{mouse.Y}");
    }
}

如果您想下载此示例项目进行测试,请随意:https://github.com/sergiocabral/Sample.CSharpNETCore.GetCursorPosition

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2013-01-26
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多