在 C# 中捕获和发送键盘/鼠标输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1112949/
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 08:32:12  来源:igfitidea点击:

Capture and send keyboard / mouse input in C#

c#apieventskeyboardmouse

提问by QueueHammer

I am looking for a way to send and receive keyboard information regardless of what app has focus. I remember back in College seeing a presentation about an old Windows API that let you change the cursor position and send right clicks and such.

我正在寻找一种发送和接收键盘信息的方法,无论哪个应用程序具有焦点。我记得在大学时看到一个关于旧的 Windows API 的演示文稿,它允许您更改光标位置并发送右键单击等。

Besides User32.dll is there a way to do this with the .net framework?

除了 User32.dll 有没有办法用 .net 框架做到这一点?

采纳答案by Kirtan

AFAIK, there's no way to capture the GLOBAL keyboard & mouse events. But there are a few articles on CodeProject which demonstrate the creation of .NET class wrappers for the same.

AFAIK,无法捕获全局键盘和鼠标事件。但是在 CodeProject 上有几篇文章演示了 .NET 类包装器的创建。

You can check them out here:

你可以在这里查看它们:

1) Processing Global Mouse and Keyboard Hooks in C#

1)在 C# 中处理全局鼠标和键盘挂钩

2) Global Mouse and Keyboard Library

2)全局键鼠库

EDIT: BTW, I had created a SMALL keylogger in C# using the 1st library :)

编辑:顺便说一句,我使用第一个库在 C# 中创建了一个小型键盘记录器:)

回答by Jonathan

Some months ago, I used C# to create an app that read from a wii nunchuck and move the mouse. My first option was to use the cursor Class to move the mouse like this

几个月前,我使用 C# 创建了一个应用程序,该应用程序可以从 Wii 双节棍中读取数据并移动鼠标。我的第一个选择是使用光标类像这样移动鼠标

Cursor.Position = new Point(Cursor.Position.X + 10, Cursor.Position.Y + 10);

All was fine, but did't work when playing games because they manage the mouse in a different way, so at the end I used the Global Hooks that Kirtan mentioned here (+1). Based in my experience, I recomend you to use Global Hooks.

一切都很好,但在玩游戏时不起作用,因为它们以不同的方式管理鼠标,所以最后我使用了 Kirtan 在这里提到的全局钩子 (+1)。根据我的经验,我建议您使用 Global Hooks。