我希望能够检查鼠标是否向下或向上忽略虚拟点击。 我正在制作一个程序,当按住右键单击按钮时发出右键单击,如果我在任何其他按钮被按住时绑定垃圾邮件右键单击,我的程序将起作用。它无法正常工作,因为它将模拟的点击作为按钮向下然后向上注册。因此,它只会点击一次,直到它认为鼠标因虚拟点击而未被保持。 此外,如果你知道一个程序执行此操作,那么下载它会很不错。
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Click_Manager
{
class Program
{
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
private static POINT mousePos;
public static void Main()
{
Thread othread = new Thread(new ThreadStart(MouseHandler));
othread.Start();
_hookID = SetHook(_proc);
Application.Run();
UnhookWindowsHookEx(_hookID);
}
public static void MouseHandler()
{
while (true)
{
MouseButtons pressedButtons = Control.MouseButtons;
if (pressedButtons == MouseButtons.Right)
{
Console.WriteLine("click");
mouse_event((uint)MouseEvents.WM_RBUTTONDOWN | (uint)MouseEvents.WM_RBUTTONUP, (uint)mousePos.x, (uint)mousePos.y, 0, 0);
}
Thread.Sleep(50);
}
}
private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 &&
MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
mousePos = hookStruct.pt;
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
private const int WH_MOUSE_LL = 14;
private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_MOUSEMOVE = 0x0200,
WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205
}
private enum MouseEvents
{
WM_LBUTTONDOWN = 0x02,
WM_LBUTTONUP = 0x04,
WM_RBUTTONDOWN = 0x08,
WM_RBUTTONUP = 0x10
}
[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}
[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);
[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, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);
}
}
答案 0 :(得分:1)
通过使用鼠标处理程序事件,您无法区分"硬件"和"软件"点击。鼠标驱动程序生成完全相同的事件,您可以通过SendMessage
以编程方式创建这些事件。 "低水平"事件只不过是全局捕获这些软事件,然后您可以映射到控件或其他任何事件。
因此,如果您想实现这一目标,您应该编写自己的API,通过USB,PS2或RS-232物理接口询问物理信号。
但你是什么意思"硬件状态"在VM?还是在线申请?例如,谷歌选择了另一种方式to differentiate real and soft clicks:而不是尝试使用任何"低级别访问" (无论它意味着什么),它们检测鼠标移动和点击是否自然或过于规则,这可能表示软件调用了点击。