There is only one method of comprehensive simulation of mouse and keyboard input: P/Invoked raw Windows API
SendInput
, see
http://msdn.microsoft.com/en-us/library/ms646304%28v=vs.85%29.aspx[
^].
Note that there are legacy functions
keybd_event
and
mouse_event
, but they were superseded by
SendInput
which is recommended to use.
The simulation is done on lower level and does not depend on and Windows, Forms, etc. The simulation is done as the events came from real keyboard and mouse.
That should answer your general questions and the question #2. Now, about your question #1, "the difference". First, please understand that no question in the format "what's the difference between {0} and {1}?" can be logically correct. Try to answer such questions on the topics you're well familiar with and you will feel it. The property
Cursor.Position
is actually
System.Windows.Forms.System.Windows.Forms.Cursor.Position
, it is limited to
System.Windows.Forms
and to the function of reading and setting the cursor coordinates. If you need just that, it should be your preferred method. The function
SendInput
does a lot more; look by yourself.
From the other hand, using P/Invoke totally breaks your application platform compatibility. If you use this function, your code will only work on Windows. If you write pure .NET application, limit it to the use of standard CLR libraries (and a lot of non-standard ones, such as
System.Windows.Forms
) you will be able to run your application without recompilation on many platforms, such as Linux, Mac OS and more. CLR is standardized under EMCA and ISO standard and is implemented on different platforms. One most essential product lik this is Mono, see
http://en.wikipedia.org/wiki/Mono_%28software%29[
^],
http://www.mono-project.com/[
^]. You can use all of that without much effort from your side if you refrain from using Windows-specific P/Invoke. You decide.
—SA