简体   繁体   English

C# .NET - SendKeys、SendInput、SendMessage、InputInjector 和 Cursor.Z52F5E0BC3869BCE5F58E25C38659BCE5F58E1ZC77 之间的区别

[英]C# .NET - Difference between SendKeys, SendInput, SendMessage, InputInjector and Cursor.Position

I am learning and building my first UWP test app, and need a way to simulate:我正在学习和构建我的第一个 UWP 测试应用程序,并且需要一种模拟方法:

  • relative mouse movement相对鼠标移动
  • absolute mouse positioning绝对鼠标定位
  • keyboard typing (not necessarily key presses/releases)键盘打字(不一定是按键/释放)
  • fine tuned x&y scrolling (so I can scroll by any amount)微调 x&y 滚动(所以我可以滚动任意数量)

I have come across the following methods for doing this, but can't figure out which ones are modern / best for UWP apps or best in general for my purposes:我遇到了以下方法来执行此操作,但无法确定哪些方法是现代的/最适合 UWP 应用程序,或者一般来说最适合我的目的:

  • SendKeys (A C# wrapper for SendInput of some sort?) SendKeys (某种形式的SendInput的 C# 包装器?)
  • SendInput (A win32 API for simulating events, but is it best for UWP?) SendInput (用于模拟事件的 win32 API,但它最适合 UWP 吗?)
  • SendMessage (Used for directly typing into focused applications?) SendMessage (用于直接输入重点应用程序?)
  • InputInjector (A more modern but limited way of simulating inputs, can't absolutely position cursor?) InputInjector (一种更现代但有限的模拟输入方式,不能绝对 position cursor?)
  • Cursor.Position (A function for cursor movement and positing) Cursor.Position (function 和运动)

There are so many methods and approaches to this problem, and I'm not entirely sure which of these is most supported or recommended for UWP apps, or yields the best results.有很多方法和方法可以解决这个问题,我不完全确定 UWP 应用程序最支持或推荐哪种方法,或者产生最佳结果。

The purpose of this project is to be able to control my PC (move the mouse, type), by interacting with through my phone.这个项目的目的是能够通过我的手机进行交互来控制我的电脑(移动鼠标、打字)。 For example my phone becomes a trackpad, or I can type in my phone's soft keyboard and it types into my PC.例如,我的手机变成了触控板,或者我可以输入手机的软键盘,然后它会输入我的 PC。 The PC hosts a server on the local network, and the phone send input data packets to this server. PC 在本地网络上托管一个服务器,电话将输入数据包发送到该服务器。 The server receives these input data packets, and executes them (which is where I need the ability to simulate keyboard/mouse events).服务器接收这些输入数据包,并执行它们(这是我需要能够模拟键盘/鼠标事件的地方)。 Very similar to Remote Mouse .非常类似于Remote Mouse

So my questions are:所以我的问题是:

  • What are the differences between these methods?这些方法之间有什么区别? (Like Windows Forms or Win32??) (如 Windows Forms 或 Win32??)
  • Which is best for UWP apps / my need here?哪个最适合 UWP 应用程序/我在这里需要?
  • Are there any better (not listed) solutions?有没有更好(未列出)的解决方案?

This is my first look into this stuff (C#, .NET, Windows dev) so any and all information is very helpful.这是我第一次看到这些东西(C#、.NET、Windows 开发)所以任何和所有信息都非常有用。

Thanks for your help!谢谢你的帮助!
Dan:D丹:D

Edit编辑

Further research has shown that InputInjector is under the UWP reference, SendKeys and Cursor.Position are both under the .NET reference.进一步的研究表明, InputInjector在 UWP 参考下, SendKeysCursor.Position都在 Z22DAZ6BB0EF8259078 参考下。 Does this mean that InputInjector is the most ideal?这是否意味着InputInjector是最理想的?

After researching some more, I found that InjectedInput is the only one included in the UWP API.在研究了一些之后,我发现InjectedInput是 UWP API 中唯一包含的一个。

To clarify, when developing a Windows application, in Visual Studio you must select one "type" to use, be it WPF, Windows Forms, Win32 or UWP. To clarify, when developing a Windows application, in Visual Studio you must select one "type" to use, be it WPF, Windows Forms, Win32 or UWP. UWP is the only one (mostly) that can be uploaded to the Microsoft Store. UWP 是唯一(大部分)可以上传到 Microsoft Store 的。

This meant that I could only use methods inside the UWP API, in this case WinRT was a part of UWP and InjectedInput is a part of WinRT.这意味着我只能使用 UWP API 中的方法,在这种情况下,WinRT 是 UWP 的一部分,而InjectedInput是 WinRT 的一部分。

It supports absolute mouse positioning with the "Absolute" option, relative mouse movement with the "Move" option, and scrolling with the "Wheel" and "HWheel" options used in InjectedInputMouseOptions alongside InjectedInputMouseInfo .它支持使用"Absolute"选项进行绝对鼠标定位,使用"Move"选项进行相对鼠标移动,以及使用InjectedInputMouseOptionsInjectedInputMouseInfo中使用的"Wheel""HWheel"选项进行滚动。 Keyboard input can be done with InjectedInputKeyOptions alongside InjectedInputKeyboardInfo .键盘输入可以通过InjectedInputKeyOptionsInjectedInputKeyboardInfo来完成。

Use the "Option" variant class to modify the effect of the input (such as selecting which options to change), and then use InputInjector with its TryCreate() method to instantiate it, along with the relevant InjectMouseInput or InjectKeyboardInput to execute the input injection.使用"Option"变体 class 来修改输入的效果(例如选择要更改哪些选项),然后使用InputInjector及其TryCreate()方法对其进行实例化,并与相关的InjectMouseInputInjectKeyboardInput一起执行输入注入.

This sample code alongside its related blog post is fantastic for understanding the basic useage, it jumps straight to the chase. 这个示例代码及其相关 的博客文章非常适合理解基本用法,它直接进入正题。

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

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