发送由GetAsyncKeyState()忽略的虚拟鼠标点击?

我一直在试验和寻找整天,不能为我的生活弄清楚如何做到这一点。

当标题读取时,我希望能够发送由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循环停止,因为我几乎抬起button,所以我要求的是一个替代mouse_event / GetAsyncKeyState(或一些参数,我不知道),所以我可以操纵键状态,而不会影响实际状态。

例如,我已经能够使用Send {LButton up}GetKeyState(“LButton”,“P”)在AutoHotkey中执行此操作。

有任何想法吗?

Solutions Collecting From Web of "发送由GetAsyncKeyState()忽略的虚拟鼠标点击?"

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

下面的代码只是一个带有定时器的空表单,配置为“开”,每秒调用“TimerOnTick”。

点击时,定时器点击输出为“497,361,1”时输出为“497,361,0”

 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 curmodulee = curProcess.Mainmodulee) { return SetWindowsHookEx(WH_MOUSE_LL, proc, GetmoduleeHandle(curmodulee.moduleeName), 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 GetmoduleeHandle(string lpmoduleeName); [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 } }