65.9K
CodeProject 正在变化。 阅读更多。
Home

C# / VB.NET / C++ CLI 和 WinAPI:如何以编程方式按下鼠标按钮,以及如何检查鼠标按钮或键盘按键是否被按下

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2014 年 9 月 30 日

CPOL
viewsIcon

29980

downloadIcon

1027

使用 WinAPI 函数 - GetAsyncKeyState 和 mouse_event

引言

在 C# 和 VB.NET 中,您必须通过 DllImport 导入 GetAsyncKeyStatemouse_event 函数,并设置用于配合使用的特殊常量。以下是包含函数和常量的类,以便轻松集成它们。

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

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

在 C++/CLI 中,您可以直接包含 Windows.h 纯 C 头文件并链接到 user32.libWindows.h 最初包含从 user32.dll 导入的所需函数和常量。您可以像在纯 C 或 C++ 中一样调用这些函数。

#include <Windows.h>

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

用法

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

' 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

注意:我可以使用 System::Threading::Thread::Sleep() 以及在 C# 和 VB.NET 代码中使用,但我使用 Windows.h 中的 Sleep() 以实现与纯 C/C++ 的完全兼容性。

// 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)
{

}
© . All rights reserved.