Click here to Skip to main content
15,886,578 members
Articles / Desktop Programming / Win32
Tip/Trick

C# / VB.NET / C++ CLI and WinAPI: How to Programmatically Press Mouse Button, and Check if Mouse Button or Keyboard Key Pressed

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
29 Sep 2014CPOL 28.4K   919   17   6
Using WinAPI functions - GetAsyncKeyState and mouse_event

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#

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;

namespace Mouse_and_keyboard_interaction_in_CSharp // DO NOT FORGET CHANGE NAMESPACE NAME TO YOUR
{
    class Win32
    {
        // Mouse

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

        // Check mouse buttons & keys if pressed
        [DllImport("user32.dll")]
        public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey); 
    }
}

VB.NET

VB.NET
Imports System.Runtime.InteropServices

Class Win32
    ' Mouse

    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

    ' Check mouse buttons & keys if pressed
    <DllImport("user32.dll")> _
    Public Shared Function GetAsyncKeyState(ByVal vKey As System.Windows.Forms.Keys) As Short
    End Function
End Class

'=======================================================
'Service provided by Telerik (www.telerik.com)
'Conversion powered by NRefactory.
'Twitter: @telerik
'Facebook: facebook.com/telerik
'=======================================================

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++.

MC++
#include <Windows.h>

#pragma comment(lib, "user32.lib")

Usage

C#

C#
// Press left mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Thread.Sleep(100); // using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

// Press right mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
Thread.Sleep(100); // using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);

// Press middle mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
Thread.Sleep(100); // using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);

// Press X mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_XDOWN, 0, 0, 0, 0);
Thread.Sleep(100); // using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_XUP, 0, 0, 0, 0);

// Check if left mouse button pressed

if (Win32.GetAsyncKeyState(Keys.LButton) != 0)
{

}

// Check if right mouse button pressed

if (Win32.GetAsyncKeyState(Keys.RButton) != 0)
{

}

// Check if middle mouse button pressed

if (Win32.GetAsyncKeyState(Keys.MButton) != 0)
{
    
}

// Check if first X mouse button pressed

if (Win32.GetAsyncKeyState(Keys.XButton1) != 0)
{

}

// Check if second X mouse button pressed

if (Win32.GetAsyncKeyState(Keys.XButton2) != 0)
{

}

// Check if ENTER key pressed

if (Win32.GetAsyncKeyState(Keys.Return) != 0)
{

}

VB.NET

VB.NET
' Press left mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Thread.Sleep(100) ' using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

' Press right mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0)
Thread.Sleep(100) ' using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0)

' Press middle mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0)
Thread.Sleep(100) ' using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0)

' Press X mouse button

Win32.mouse_event(Win32.MOUSEEVENTF_XDOWN, 0, 0, 0, 0)
Thread.Sleep(100) ' using System.Threading;
Win32.mouse_event(Win32.MOUSEEVENTF_XUP, 0, 0, 0, 0)

' Check if left mouse button pressed

If Win32.GetAsyncKeyState(Keys.LButton) <> 0 Then

End If

' Check if right mouse button pressed

If Win32.GetAsyncKeyState(Keys.RButton) <> 0 Then

End If

' Check if middle mouse button pressed

If Win32.GetAsyncKeyState(Keys.MButton) <> 0 Then
    
End If

' Check if first X mouse button pressed

If Win32.GetAsyncKeyState(Keys.XButton1) <> 0 Then

End If

' Check if second X mouse button pressed

If Win32.GetAsyncKeyState(Keys.XButton2) <> 0 Then

End If

' Check if ENTER key pressed

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++.

MC++
// Press left mouse button

mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

// Press right mouse button

mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);

// Press middle mouse button

mouse_event(MOUSEEVENTF_MIDDLEDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_MIDDLEUP, 0, 0, 0, 0);

// Press X mouse button

mouse_event(MOUSEEVENTF_XDOWN, 0, 0, 0, 0);
Sleep(100);
mouse_event(MOUSEEVENTF_XUP, 0, 0, 0, 0);

// Check if left mouse button pressed

if (GetAsyncKeyState(VK_LBUTTON) != 0)
{

}

// Check if right mouse button pressed

if (GetAsyncKeyState(VK_RBUTTON) != 0)
{

}

// Check if middle mouse button pressed

if (GetAsyncKeyState(VK_MBUTTON) != 0)
{
    
}

// Check if first X mouse button pressed

if (GetAsyncKeyState(VK_XBUTTON1) != 0)
{

}

// Check if second X mouse button pressed

if (GetAsyncKeyState(VK_XBUTTON2) != 0)
{

}

// Check if ENTER key pressed

if (GetAsyncKeyState(VK_RETURN) != 0)
{

}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionMissing C++ example download Pin
big_sticks4-Oct-14 10:26
big_sticks4-Oct-14 10:26 
AnswerRe: Missing C++ example download Pin
Emiliarge4-Oct-14 10:44
professionalEmiliarge4-Oct-14 10:44 
SuggestionIt would seem that the no_exe zip file is actually an exe_only zip file. Pin
big_sticks4-Oct-14 10:21
big_sticks4-Oct-14 10:21 
GeneralRe: It would seem that the no_exe zip file is actually an exe_only zip file. Pin
Emiliarge4-Oct-14 10:43
professionalEmiliarge4-Oct-14 10:43 
QuestionSeems incomplete Pin
Wendelius29-Sep-14 18:54
mentorWendelius29-Sep-14 18:54 
AnswerRe: Seems incomplete Pin
Emiliarge30-Sep-14 1:01
professionalEmiliarge30-Sep-14 1:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.