// [In] Indique que les données doivent être marshalées de l'appelant vers l'appelé, mais pas à nouveau vers l'appelant. InAttribute class dans System.Runtime.InteropServices (http://msdn.microsoft.com/fr-fr/library/system.runtime.interopservices.inattribute.aspx) private int _HookProc(int nCode, User.WM wm, [In] User.Struct.KBDLLHOOKSTRUCT kbParam) { if (nCode >= 0 && (HookEvent != null || gbTrace)) { User.VK vk = Messages.GetVirtualKey((int)kbParam.vkCode); KeystrokeMessageFlags kf = Messages.GetKeystrokeMessageFlags(kbParam.Flags); User.Struct.GUITHREADINFO GUIThreadInfo = Win32.Windows.GetGUIThreadInfo(); uint ProcessId; uint ThreadId = User.GetWindowThreadProcessId(GUIThreadInfo.hwndFocus, out ProcessId); if (gbTrace) { string sCode; if (nCode == User.Const.HC_ACTION) { sCode = "HC_ACTION"; } else { sCode = nCode.ToString(); } string sKey = vk.ToString(); pb.Trace.WriteLine("KeyboardLowLevelHook : {0,-20} {1,-20} {2:x,4} {3,-11} process {4:x} thread {5:x}", wm, sKey, kbParam.vkCode, sCode, ProcessId, ThreadId); } if (HookEvent != null) { KeyboardLowLevelHookMessage msg = new KeyboardLowLevelHookMessage() { nCode = nCode, kbParam = kbParam, wm = wm, vk = vk, KeyFlags = kf, GUIThreadInfo = GUIThreadInfo, ProcessId = ProcessId, ThreadId = ThreadId }; HookEvent(msg); if (msg.RemoveMessage) { return(1); } } } return(User.CallNextHookEx(gHookHandle, nCode, wm, kbParam)); }
private int _HookProc(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && (HookEvent != null || gbTrace)) { User.VK vk = Messages.GetVirtualKey((int)wParam); KeyboardState ks = Messages.GetKeyboardState(); // attention (uint)lParam peut générer une exception si la valeur dépasse la capacité d'un int, utiliser (uint)p.ToInt64() //KeystrokeMessageFlags kf = Messages.GetKeystrokeMessageFlags((uint)lParam); KeystrokeMessageFlags kf = Messages.GetKeystrokeMessageFlags((uint)lParam.ToInt64()); User.Struct.GUITHREADINFO GUIThreadInfo = Win32.Windows.GetGUIThreadInfo(); if (gbTrace) { string sCode; if (nCode == User.Const.HC_ACTION) { sCode = "HC_ACTION"; } else if (nCode == User.Const.HC_NOREMOVE) { sCode = "HC_NOREMOVE"; } else { sCode = nCode.ToString(); } string sKey = vk.ToString(); if (ks.Alt) { sKey = "ALT-" + sKey; } if (ks.Ctrl) { sKey = "CTRL-" + sKey; } if (ks.Shift) { sKey = "SHIFT-" + sKey; } string sTransition; if (kf.TransitionState == 0) { sTransition = "WM_KEYDOWN"; } else { sTransition = "WM_KEYUP"; } string sPreviousKeyState; if (kf.PreviousKeyState == 0) { sPreviousKeyState = "KEYUP"; } else { sPreviousKeyState = "KEYDOWN"; } pb.Trace.WriteLine("KeyboardHook : {0,-20} {1,-20} {2:x,4} {3,-11} prev key state {4,-7} lParam {5:x}", sTransition, sKey, wParam, sCode, sPreviousKeyState, lParam); } if (HookEvent != null) { KeyboardHookMessage msg = new KeyboardHookMessage() { nCode = nCode, wParam = wParam, lParam = lParam, vk = vk, KeyState = ks, KeyFlags = kf, GUIThreadInfo = GUIThreadInfo }; HookEvent(msg); if (msg.RemoveMessage) { return(1); } } } return(User.CallNextHookEx(gHookHandle, nCode, wParam, lParam)); }