Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

uwp

I am learning and building my first UWP test app, and need a way to simulate:

  • 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)

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:

  • SendKeys (A C# wrapper for SendInput of some sort?)
  • SendInput (A win32 API for simulating events, but is it best for UWP?)
  • SendMessage (Used for directly typing into focused applications?)
  • InputInjector (A more modern but limited way of simulating inputs, can't absolutely position cursor?)
  • Cursor.Position (A function for cursor movement and positing)

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.

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. The PC hosts a server on the local network, and the phone send input data packets to this server. 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.

So my questions are:

  • What are the differences between these methods? (Like Windows Forms or Win32??)
  • Which is best for UWP apps / my need here?
  • 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.

Thanks for your help!
Dan :D

Edit

Further research has shown that InputInjector is under the UWP reference, SendKeys and Cursor.Position are both under the .NET reference. Does this mean that InputInjector is the most ideal?

like image 930
dantechguy Avatar asked Oct 12 '25 08:10

dantechguy


1 Answers

After researching some more, I found that InjectedInput is the only one included in the 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. UWP is the only one (mostly) that can be uploaded to the 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.

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. Keyboard input can be done with InjectedInputKeyOptions alongside InjectedInputKeyboardInfo.

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.

This sample code alongside its related blog post is fantastic for understanding the basic useage, it jumps straight to the chase.

like image 77
dantechguy Avatar answered Oct 14 '25 05:10

dantechguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!