private IntPtr KeyboardHookDelegate(Int32 Code, IntPtr wParam, IntPtr lParam) { hookStruct param = (hookStruct)Marshal.PtrToStructure(lParam, typeof(hookStruct)); if (Code < 0 || (param.flags & LLKHF_INJECTED) != 0) { return(WindowsHookAPI.CallNextHookEx(keyBoardHandle, Code, wParam, lParam)); } int scanCode = param.scanCode; if ((param.flags & LLKHF_EXTENDED) != 0) { // NumberPadEnter, PageUp, PageDown, End, Home, LeftArrow, UpArrow, rightArrow, DownArrow, Insert, Delete, NumberPadstar if (param.vkCode == 13 || (param.vkCode >= 33 && param.vkCode <= 40) || param.vkCode == 45 || param.vkCode == 46 || param.vkCode == 111) { scanCode = param.scanCode + 128; } } if ((int)wParam == WM_KEYDOWN) { aic.writeBit(scanCode, true); // set bit key if (Buffered && DataClear) { var chr = (char)WindowsHookAPI.MapVirtualKey((uint)param.vkCode, 2); if (authorizedkeys.Contains(chr.ToString())) { accessor.Write(position: aic.DATA >> 3, value: (byte)chr); } } #if DEBUG Console.WriteLine($"Vk: {param.vkCode} Scancode: {param.scanCode} flags: {param.flags} wparam: {(int)wParam} otherscancode: {scanCode}"); Console.WriteLine($"Aa: {WindowsHookAPI.MapVirtualKey((uint)param.vkCode, 2)},{(char)WindowsHookAPI.MapVirtualKey((uint)param.vkCode, 2)}"); Console.WriteLine($"Vk: {param.vkCode} Scancode: {param.scanCode} flags: {param.flags} wparam: {(int)wParam}, scancal: {WindowsHookAPI.MapVirtualKey((uint)param.vkCode, 0)}"); #endif } else { aic.writeBit(scanCode, false); }; // raz bit key if (aic.readBit(scanCode + 256) || aic.readBit(aic.BUFFERED)) // Key hooked? { return((IntPtr)1); } return(WindowsHookAPI.CallNextHookEx(keyBoardHandle, Code, wParam, lParam)); }
private IntPtr MouseHookDelegate(Int32 Code, IntPtr wParam, IntPtr lParam) { //VK_LBUTTON 0x01 Left mouse button //VK_RBUTTON 0x02 Right mouse button //VK_MBUTTON 0x04 Middle mouse button //VK_XBUTTON1 0x05 X1 mouse button //VK_XBUTTON2 0x06 X2 mouse button // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms644970(v=vs.85).aspx //mouseData: //If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta.The low-order word is reserved. // A positive value indicates that the wheel was rotated forward, away from the user; // a negative value indicates that the wheel was rotated backward, toward the user. // One wheel click is defined as WHEEL_DELTA, which is 120.(0x78 or 0xFF88) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, or WM_NCXBUTTONDBLCLK, // the high - order word specifies which X button was pressed or released, // and the low - order word is reserved.This value can be one or more of the following values.Otherwise, mouseData is not used. //XBUTTON1 = 0x0001 The first X button was pressed or released. //XBUTTON2 = 0x0002 The second X button was pressed or released. MSLLHOOKSTRUCT lparam = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); int command = (int)wParam; if (Code < 0 || command == WM_LBUTTONDBLCLK || command == WM_RBUTTONDBLCLK) { return(WindowsHookAPI.CallNextHookEx(mouseHandle, Code, wParam, lParam)); } //if ((int)wParam != 512) // Console.WriteLine($"x: {lparam.pt.x} y: {lparam.pt.y} f: {lparam.flags} moudata: {(Int16)(lparam.mouseData >> 16)} wp: {(int)wParam}"); if (command == WM_MOUSEMOVE) { int xy = lparam.pt.x + (lparam.pt.y << 16); //Console.WriteLine($"xy:{ xy} x:{lparam.pt.x} y:{lparam.pt.y} y<<16:{(lparam.pt.y)<<16}"); accessor.Write((long)Mouse.XY, xy); } else if (command == WM_XBUTTONDOWN || command == WM_XBUTTONUP) { int numbutton = ((int)lparam.mouseData >> 16) - 1; aic.writeBit((int)Mouse.X1 + numbutton, command == WM_XBUTTONDOWN); //Console.WriteLine($"action={aic.readBit((int)Mouse.X1 + numbutton)}"); if (IsHooked((int)Mouse.X1 + numbutton)) { return((IntPtr)1); } } else if (command == WM_LBUTTONDOWN || command == WM_LBUTTONUP) { aic.writeBit((int)Mouse.Left, command == WM_LBUTTONDOWN); if (IsHooked((int)Mouse.Left)) { return((IntPtr)1); } } else if (command == WM_RBUTTONDOWN || command == WM_RBUTTONUP) { aic.writeBit((int)Mouse.Right, command == WM_RBUTTONDOWN); if (IsHooked((int)Mouse.Right)) { return((IntPtr)1); } } else if (command == WM_MBUTTONDOWN || command == WM_MBUTTONUP) { aic.writeBit((int)Mouse.Middle, command == WM_MBUTTONDOWN); Console.WriteLine($"action={aic.readBit((int)Mouse.Middle)}"); if (IsHooked((int)Mouse.Middle)) { return((IntPtr)1); } } else if (command == WM_MOUSEWHEEL) { int wheelvalue = (Int16)(lparam.mouseData >> 16) < 0 ? 1 : 0; // Forward = 0, Backward = 1 if (!aic.readBit((int)Mouse.WheelFwd + wheelvalue)) { aic.writeBit((int)Mouse.WheelFwd + wheelvalue, true); } if (IsHooked((int)Mouse.WheelFwd + wheelvalue)) { return((IntPtr)1); } } return(WindowsHookAPI.CallNextHookEx(mouseHandle, Code, wParam, lParam)); }
private IntPtr MouseHookDelegate(Int32 Code, IntPtr wParam, IntPtr lParam) { //VK_LBUTTON 0x01 Left mouse button //VK_RBUTTON 0x02 Right mouse button //VK_MBUTTON 0x04 Middle mouse button //VK_XBUTTON1 0x05 X1 mouse button //VK_XBUTTON2 0x06 X2 mouse button // see https://msdn.microsoft.com/en-us/library/windows/desktop/ms644970(v=vs.85).aspx //mouseData: //If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta.The low-order word is reserved. // A positive value indicates that the wheel was rotated forward, away from the user; // a negative value indicates that the wheel was rotated backward, toward the user. // One wheel click is defined as WHEEL_DELTA, which is 120.(0x78 or 0xFF88) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, or WM_NCXBUTTONDBLCLK, // the high - order word specifies which X button was pressed or released, // and the low - order word is reserved.This value can be one or more of the following values.Otherwise, mouseData is not used. //XBUTTON1 = 0x0001 The first X button was pressed or released. //XBUTTON2 = 0x0002 The second X button was pressed or released. MSLLHOOKSTRUCT lparam = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); int command = (int)wParam; if (Code < 0 || command == WM_LBUTTONDBLCLK || command == WM_RBUTTONDBLCLK) { return(WindowsHookAPI.CallNextHookEx(mouseHandle, Code, wParam, lParam)); } if (command == WM_MOUSEMOVE) { Console.WriteLine($"x:{lparam.pt.x} y:{lparam.pt.y}"); } else if (command == WM_LBUTTONDOWN || command == WM_LBUTTONUP) { if (command == WM_LBUTTONDOWN) { //do something } else { //do something } } else if (command == WM_RBUTTONDOWN || command == WM_RBUTTONUP) { if (command == WM_RBUTTONDOWN) { //do something } else { //do something } } else if (command == WM_MBUTTONDOWN || command == WM_MBUTTONUP) { if (command == WM_MBUTTONDOWN) { //do something } else { //do something } } else if (command == WM_MOUSEWHEEL) { int wheelvalue = (Int16)(lparam.mouseData >> 16) < 0 ? -120 : 120; // Forward = 120, Backward = -120 } return(WindowsHookAPI.CallNextHookEx(mouseHandle, Code, wParam, lParam)); }
private IntPtr MouseHookDelegate(Int32 Code, IntPtr wParam, IntPtr lParam) { //mouseData: //If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta.The low-order word is reserved. // A positive value indicates that the wheel was rotated forward, away from the user; // a negative value indicates that the wheel was rotated backward, toward the user. // One wheel click is defined as WHEEL_DELTA, which is 120.(0x78 or 0xFF88) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, or WM_NCXBUTTONDBLCLK, // the high - order word specifies which X button was pressed or released, // and the low - order word is reserved.This value can be one or more of the following values.Otherwise, mouseData is not used. //XBUTTON1 = 0x0001 The first X button was pressed or released. //XBUTTON2 = 0x0002 The second X button was pressed or released. MSLLHOOKSTRUCT lparam = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); int command = (int)wParam; if (Code < 0 || command == WM_LBUTTONDBLCLK || command == WM_RBUTTONDBLCLK) { return(WindowsHookAPI.CallNextHookEx(mouseHandle, Code, wParam, lParam)); } else if (command == WM_XBUTTONDOWN || command == WM_XBUTTONUP) { int numbutton = ((int)lparam.mouseData >> 16) - 1; //return (IntPtr)1; } else if (command == WM_LBUTTONDOWN || command == WM_LBUTTONUP) { if (command == WM_LBUTTONUP) { vc.VolDown(); Console.WriteLine("L down"); } } else if (command == WM_RBUTTONDOWN || command == WM_RBUTTONUP) { if (command == WM_RBUTTONUP) { vc.VolUp(); Console.WriteLine("L Up"); } } else if (command == WM_MBUTTONDOWN || command == WM_MBUTTONUP) { if (hooked) { setHook(false); Console.WriteLine("hook deactivated"); } else { setHook(true); Console.WriteLine("hook activated"); } } else if (command == WM_MOUSEWHEEL) { } return(WindowsHookAPI.CallNextHookEx(mouseHandle, Code, wParam, lParam)); }