Introduction
In C# and VB.NET, you must import GetAsyncKeyState
and mouse_event
functions from user32.dll WinAPI library by DllImport
and set special constants for using with them. Given below is the class with functions and constants for easily integrating them.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Mouse_and_keyboard_interaction_in_CSharp
{
class Win32
{
public const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
public const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
public const uint MOUSEEVENTF_LEFTUP = 0x0004;
public const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
public const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
public const uint MOUSEEVENTF_MOVE = 0x0001;
public const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
public const uint MOUSEEVENTF_RIGHTUP = 0x0010;
public const uint MOUSEEVENTF_XDOWN = 0x0080;
public const uint MOUSEEVENTF_XUP = 0x0100;
public const uint MOUSEEVENTF_WHEEL = 0x0800;
public const uint MOUSEEVENTF_HWHEEL = 0x01000;
[DllImport("user32.dll")]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, int dwExtraInfo);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
}
}
VB.NET
Imports System.Runtime.InteropServices
Class Win32
Public Const MOUSEEVENTF_ABSOLUTE As UInteger = &H8000
Public Const MOUSEEVENTF_LEFTDOWN As UInteger = &H2
Public Const MOUSEEVENTF_LEFTUP As UInteger = &H4
Public Const MOUSEEVENTF_MIDDLEDOWN As UInteger = &H20
Public Const MOUSEEVENTF_MIDDLEUP As UInteger = &H40
Public Const MOUSEEVENTF_MOVE As UInteger = &H1
Public Const MOUSEEVENTF_RIGHTDOWN As UInteger = &H8
Public Const MOUSEEVENTF_RIGHTUP As UInteger = &H10
Public Const MOUSEEVENTF_XDOWN As UInteger = &H80
Public Const MOUSEEVENTF_XUP As UInteger = &H100
Public Const MOUSEEVENTF_WHEEL As UInteger = &H800
Public Const MOUSEEVENTF_HWHEEL As UInteger = &H1000
<DllImport("user32.dll")> _
Public Shared Sub mouse_event(ByVal dwFlags As UInteger, ByVal dx As UInteger, _
ByVal dy As UInteger, ByVal dwData As UInteger, ByVal dwExtraInfo As Integer)
End Sub
<DllImport("user32.dll")> _
Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
End Function
End Class
C++/CLI
In C++/CLI, you may just include Windows.h Pure C header and link with user32.lib. Windows.h initially contains required functions and constants imported from user32.dll. You can call these functions as well as in the Pure C or C++.
#include <Windows.h>
#pragma comment(lib, "user32.lib")
Usage
C#
Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Thread.Sleep(100);
Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
Thread.Sleep(100);
Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
Thread.Sleep(100);
Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
Win32.mouse_event(Win32.MOUSEEVENTF_XDOWN, 0, 0, 0, 0);
Thread.Sleep(100);
Win32.mouse_event(Win32.MOUSEEVENTF_XUP, 0, 0, 0, 0);
if (Win32.GetAsyncKeyState(Keys.LButton) != 0)
{
}
if (Win32.GetAsyncKeyState(Keys.RButton) != 0)
{
}
if (Win32.GetAsyncKeyState(Keys.MButton) != 0)
{
}
if (Win32.GetAsyncKeyState(Keys.XButton1) != 0)
{
}
if (Win32.GetAsyncKeyState(Keys.XButton2) != 0)
{
}
if (Win32.GetAsyncKeyState(Keys.Return) != 0)
{
}
VB.NET
Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Thread.Sleep(100)
Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
Thread.Sleep(100)
Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)
Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
Thread.Sleep(100)
Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)
Win32.mouse_event(Win32.MOUSEEVENTF_XDOWN, 0, 0, 0, 0)
Thread.Sleep(100)
Win32.mouse_event(Win32.MOUSEEVENTF_XUP, 0, 0, 0, 0)
If Win32.GetAsyncKeyState(Keys.LButton) <> 0 Then
End If
If Win32.GetAsyncKeyState(Keys.RButton) <> 0 Then
End If
If Win32.GetAsyncKeyState(Keys.MButton) <> 0 Then
End If
If Win32.GetAsyncKeyState(Keys.XButton1) <> 0 Then
End If
If Win32.GetAsyncKeyState(Keys.XButton2) <> 0 Then
End If
If Win32.GetAsyncKeyState(Keys.[Return]) <> 0 Then
End If
C++/CLI
Note: I can use System::Threading::Thread::Sleep()
as well as in C# and VB.NET codes, but I use Sleep()
from Windows.h for full compatibility with Pure C/C++.
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_XDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_XUP, 0, 0, 0, 0);
if (GetAsyncKeyState(VK_LBUTTON) != 0)
{
}
if (GetAsyncKeyState(VK_RBUTTON) != 0)
{
}
if (GetAsyncKeyState(VK_MBUTTON) != 0)
{
}
if (GetAsyncKeyState(VK_XBUTTON1) != 0)
{
}
if (GetAsyncKeyState(VK_XBUTTON2) != 0)
{
}
if (GetAsyncKeyState(VK_RETURN) != 0)
{
}