public int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam) { //Marshall the data from the callback. if (nCode < 0) { return(CallNextHookEx(hkeyHook, nCode, wParam, lParam)); } else { if (isCapturing && currentServer != null && currentServer.Status == 1) { try { //Create a string variable that shows the current mouse coordinates. KBDLLHOOKSTRUCT kb; kb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); KeyboardStruct ks = new KeyboardStruct(); ks.kb = kb; ks.wparam = wParam; UdpClient uc = currentServer.getUdpClient(); Int32 ik = 1; byte[] keyboard = BitConverter.GetBytes(ik); byte[] bytestream = getBytesKey(ks); byte[] sending = new byte[bytestream.Length + sizeof(Int32)]; keyboard.CopyTo(sending, 0); bytestream.CopyTo(sending, sizeof(Int32)); uc.Send(sending, sending.Length); //You must get the active form because it is a static function. if (wParam == (IntPtr)WM_KEYDOWN)//key is down 0 { Console.WriteLine("down sono:" + ks.kb.vkCode); } else if (wParam == (IntPtr)WM_KEYUP) //key is up 1 { Console.WriteLine("up sono:" + ks.kb.vkCode); } else { Console.WriteLine("default" + ks.wparam); } return(1); } catch (Exception e) { isCapturing = false; currentServer.Status = 0; Win.connectionProblem(currentServer); } } } return(CallNextHookEx(hkeyHook, nCode, wParam, lParam)); }
public void event_Switch_Keyboard(byte[] b1) { KeyboardStruct mystruct = kfromBytes(b1); int key = mystruct.kb.vkCode; switch ((uint)mystruct.wparam) { case WM_KEYUP: Console.WriteLine("up" + key); KeyUp(key); break; case WM_KEYDOWN: Console.WriteLine("down" + key); KeyDown(key); break; case WM_SYSKEYDOWN: Console.WriteLine("sysdown" + key); KeyDown(key); break; case WM_SYSKEYUP: Console.WriteLine("sysup" + key); KeyUp(key); break; default: Console.WriteLine("Default case keyboard" + key); Console.WriteLine("Default case keyboard" + (uint)mystruct.wparam); break; } }
//raise function every keyboard hook private static int RCH_Keyboard_Hook_Proc(int nCode, int wParam, IntPtr lParam) { KeyboardStruct k_struct = (KeyboardStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardStruct)); _Keyboard.Invoke(k_struct, wParam); return(CallNextHookEx(RCH_Keyboard_Handler, nCode, wParam, lParam)); }
private static int LowLevelKeyboardHookCallback(int code, IntPtr wParam, IntPtr lParam) { if (code < 0) { return(CallNextHookEx(IntPtr.Zero, code, wParam, lParam)); } // The wParam in the context of a Low Level Keyboard Hook Procedure is a flag for the type of keyboard event // Differentiating between key presses (and holding the key down), key releases, and key presses and releases with // the alt key pressed down? KeyFlags keyboardMessageIdentifier = (KeyFlags)wParam.ToInt32(); switch (keyboardMessageIdentifier) { case KeyFlags.WM_KEYDOWN: Debug.WriteLine("WM_KEYDOWN"); break; case KeyFlags.WM_KEYUP: Debug.WriteLine("WM_KEYUP"); break; case KeyFlags.WM_SYSKEYDOWN: Debug.WriteLine("WM_SYSKEYDOWN"); break; case KeyFlags.WM_SYSKEYUP: Debug.WriteLine("WM_SYSKEYUP"); break; default: Debug.Fail("Unknown flag: " + keyboardMessageIdentifier); break; } // The lParam in the context of a Low Level Keyboard Hook Procedure is a pointer to a structure called KBDLLHOOKSTRUCT // The structure contains: // a virtual key code // a hardware scan code // flags for: // extended key // event-injection // context code // transition (key up or key down) KeyboardStruct kbs = Marshal.PtrToStructure <KeyboardStruct>(lParam); Keys key = (Keys)kbs.vkCode; Debug.WriteLine(key); Debug.WriteLine(kbs.scanCode); Debug.WriteLine(kbs.flags); Debug.WriteLine(kbs.time); Debug.WriteLine(kbs.dwExtraInfo.ToUInt64()); Debug.WriteLine(""); return(CallNextHookEx(IntPtr.Zero, code, wParam, lParam)); }
KeyboardStruct kfromBytes(byte[] arr) { KeyboardStruct mystruct = new KeyboardStruct(); int size = Marshal.SizeOf(mystruct); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(arr, 0, ptr, size); mystruct = (KeyboardStruct)Marshal.PtrToStructure(ptr, mystruct.GetType()); Marshal.FreeHGlobal(ptr); return(mystruct); }
byte[] getBytesKey(KeyboardStruct mystruct) { int size = Marshal.SizeOf(mystruct); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(mystruct, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return(arr); }
KeyboardStruct kfromBytes(byte[] arr) { KeyboardStruct mystruct = new KeyboardStruct(); int size = Marshal.SizeOf(mystruct); IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.Copy(arr, 0, ptr, size); mystruct = (KeyboardStruct)Marshal.PtrToStructure(ptr, mystruct.GetType()); Marshal.FreeHGlobal(ptr); return mystruct; }
byte[] getBytesKey(KeyboardStruct mystruct) { int size = Marshal.SizeOf(mystruct); byte[] arr = new byte[size]; IntPtr ptr = Marshal.AllocHGlobal(size); Marshal.StructureToPtr(mystruct, ptr, true); Marshal.Copy(ptr, arr, 0, size); Marshal.FreeHGlobal(ptr); return arr; }
public int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam) { //Marshall the data from the callback. if (nCode < 0) { return CallNextHookEx(hkeyHook, nCode, wParam, lParam); } else { if (isCapturing && currentServer != null && currentServer.Status == 1) { try { //Create a string variable that shows the current mouse coordinates. KBDLLHOOKSTRUCT kb; kb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); KeyboardStruct ks = new KeyboardStruct(); ks.kb = kb; ks.wparam = wParam; UdpClient uc = currentServer.getUdpClient(); Int32 ik = 1; byte[] keyboard = BitConverter.GetBytes(ik); byte[] bytestream = getBytesKey(ks); byte[] sending = new byte[bytestream.Length + sizeof(Int32)]; keyboard.CopyTo(sending, 0); bytestream.CopyTo(sending, sizeof(Int32)); uc.Send(sending, sending.Length); //You must get the active form because it is a static function. if (wParam == (IntPtr)WM_KEYDOWN)//key is down 0 { Console.WriteLine("down sono:" + ks.kb.vkCode); } else if (wParam == (IntPtr)WM_KEYUP) //key is up 1 { Console.WriteLine("up sono:" + ks.kb.vkCode); } else { Console.WriteLine("default" + ks.wparam); } return 1; } catch (Exception e) { isCapturing = false; currentServer.Status = 0; Win.connectionProblem(currentServer); } } } return CallNextHookEx(hkeyHook, nCode, wParam, lParam); }