发送GetAsyncKeyState()忽略的虚拟鼠标单击

本文关键字:虚拟 鼠标 单击 GetAsyncKeyState 发送 | 更新日期: 2023-09-27 18:26:08

我一整天都在试验和搜索,但我一辈子都不知道如何做到这一点。

正如标题所示,我希望能够发送被GetAsyncKeyState()之类的东西忽略的鼠标点击。

基本上我在做什么:

//While physically holding left mouse button...
while (GetAsyncKeyState(0x01) != 0)
{
    //left mouse button virtually down (obviously already is down the first loop)
    mouse_event(2, 0, 0, 0, 0); 
    Thread.Sleep(100);
    //left mouse button virtually up
    mouse_event(4, 0, 0, 0, 0);
}

现在,while循环停止了,因为我实际上按下了按钮,所以我想要的是mouse_event/GetAsyncKeyState(或一些我不知道的参数)的替代方案,这样我就可以在不影响实际状态的情况下操纵键状态。

例如,我已经能够在AutoHotkey中使用发送{LButton up}GetKeyState("LButton","p")来执行此操作。

有什么想法吗?

发送GetAsyncKeyState()忽略的虚拟鼠标单击

MSLLHOOKSTRUCT包含LLMHF_INJECTEDLLMHF_LOWER_IL_INJECTED标志。也许你应该看看。

下面的代码只是一个带有计时器的空表单,配置为"On"并每隔几秒钟调用一次"TimerOnTick"。

当您点击时,输出为"497361,0",当计时器点击时,输出是"4973611,1"

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Syracuse
{
    public partial class Form1 : Form
    {
        private const int WH_MOUSE_LL = 14;
        private static LowLevelMouseProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
        public Form1()
        {
            InitializeComponent();
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _hookID = SetHook(_proc);
        }
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            UnhookWindowsHookEx(_hookID);
        }
        private void TimerOnTick(object sender, EventArgs e)
        {
            //left mouse button virtually down (obviously already is down the first loop)
            mouse_event(2, 0, 0, 0, UIntPtr.Zero);
            Thread.Sleep(100);
            //left mouse button virtually up
            mouse_event(4, 0, 0, 0, UIntPtr.Zero);
        }

        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages) wParam)
            {
                var hookStruct = (MSLLHOOKSTRUCT) Marshal.PtrToStructure(lParam, typeof (MSLLHOOKSTRUCT));
                Console.WriteLine(hookStruct.pt.x + ", " + hookStruct.pt.y + ", " + hookStruct.flags);
            }
            // Todo create OnMouseUpExEvent, OnMouseDownExEvent witch provide MSLLHOOKSTRUCT informations.
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
        private static IntPtr SetHook(LowLevelMouseProc proc)
        {
            using (var curProcess = Process.GetCurrentProcess())
            using (var curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
            }
        }
        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
        #region Enum & Struct
        private enum MouseMessages
        {
            WM_LBUTTONDOWN = 0x0201,
            WM_LBUTTONUP = 0x0202,
            WM_MOUSEMOVE = 0x0200,
            WM_MOUSEWHEEL = 0x020A,
            WM_RBUTTONDOWN = 0x0204,
            WM_RBUTTONUP = 0x0205
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct MSLLHOOKSTRUCT
        {
            public POINT pt;
            public uint mouseData;
            public uint flags;
            public uint time;
            public IntPtr dwExtraInfo;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public int x;
            public int y;
        }
        #endregion
        #region Extern
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, UIntPtr dwExtraInfo);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
        #endregion
    }
}