protected override IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)) { KBDLLHOOKSTRUCT kb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); var vkCode = (int)kb.vkCode; var key = KeyMapConverter.KeyCodeToKey(vkCode); var isVirtualInput = kb.dwExtraInfo == MAGIC_NUMBER; OnKeyDownEvent(vkCode, key, isVirtualInput); return(KeyDownFunction(key, isVirtualInput, () => base.HookProcedure(nCode, wParam, lParam))); } else if (nCode >= 0 && (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)) { KBDLLHOOKSTRUCT kb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); var vkCode = (int)kb.vkCode; var key = KeyMapConverter.KeyCodeToKey(vkCode); var isVirtualInput = kb.dwExtraInfo == MAGIC_NUMBER; OnKeyUpEvent(vkCode, key, isVirtualInput); return(KeyUpFunction(key, isVirtualInput, () => base.HookProcedure(nCode, wParam, lParam))); } return(base.HookProcedure(nCode, wParam, lParam)); }
public List <int> WhichModifiersPressed(int flags) { List <int> pressedModifiers = new List <int>(); int[] modifierKeys = { KeyMapConverter.KeyToCode(OriginalKey.LeftWindows), KeyMapConverter.KeyToCode(OriginalKey.RightWindows), KeyMapConverter.KeyToCode(OriginalKey.LeftCtrl), KeyMapConverter.KeyToCode(OriginalKey.RightCtrl), KeyMapConverter.KeyToCode(OriginalKey.LeftShift), KeyMapConverter.KeyToCode(OriginalKey.RightShift), }; for (int i = 0; i < modifierKeys.Length; i++) { var mk = modifierKeys[i]; if (GetAsyncKeyState(mk) < 0) { pressedModifiers.Add(mk); } } //Logger.WriteLine($"InterceptKeyboard.WhichModifierPressed.flags = {flags}"); // ALT は左右の判定ができないので左ALTが押されたものとする // flags の値は、キーダウン時は 32(0x20)、キーアップ時は160(0xA0) if ((flags & 0x20) == 0x20) { pressedModifiers.Add(KeyMapConverter.KeyToCode(OriginalKey.LeftAlt)); //pressedModifiers.Add(KeyMapConverter.KeyToCode(OriginalKey.RightAlt)); } return(pressedModifiers); }
/// <summary> /// Input the Key. /// </summary> /// <param name="pushedKey">Actually pushed key</param> /// <param name="destKey">Converted key</param> /// <returns></returns> protected IntPtr InputKey(OriginalKey pushedKey, OriginalKey destKey) { var inputKey = KeyMapConverter.KeyToCode(destKey); var inkey = input.KeyDown(inputKey); if (!inkeys.ContainsKey(pushedKey)) { inkeys.Add(pushedKey, inkey); } return(new IntPtr(1)); }
/* * public override IntPtr HookProcedure(int nCode, IntPtr wParam, IntPtr lParam) * { * var eventKeyDown = (wParam == (IntPtr)WM_KEYDOWN) ? "KeyDown " : ""; * var eventSysKeyDown = (wParam == (IntPtr)WM_SYSKEYDOWN) ? "SysKeyDown " : ""; * var eventKeyUp = (wParam == (IntPtr)WM_KEYUP) ? "KeyUp " : ""; * var eventSysKeyUp = (wParam == (IntPtr)WM_SYSKEYUP) ? "SysKeyUp " : ""; * var wkb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); * var wvkCode = (int)wkb.vkCode; * var vk = KeyMapConverter.KeyCodeToKey(wvkCode); * * Logger.WriteLine($"{nCode} : {vk} : {eventKeyDown}{eventSysKeyDown}{eventKeyUp}{eventSysKeyUp}"); * * if (nCode >= 0 && (wParam == (IntPtr)WM_KEYDOWN || wParam == (IntPtr)WM_SYSKEYDOWN)) * { * var kb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); * var vkCode = (int)kb.vkCode; * if (this.isModifier(vkCode)) * { * if (! this.Modifiers.Contains(vkCode)) * { * this.Modifiers.Add(vkCode); * } * else * { * // KeyDown 時にすでに同じ Modifier が存在していた場合 * // ある Modifier を二重に押していることになる * // なにか問題が発生しているときなので、いったん削除する * this.Modifiers.Remove(vkCode); * } * } * else * { * this.OnKeyDownEvent(vkCode); * } * } * else if (nCode >= 0 && (wParam == (IntPtr)WM_KEYUP || wParam == (IntPtr)WM_SYSKEYUP)) * { * var kb = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); * var vkCode = (int)kb.vkCode; * if (this.isModifier(vkCode)) * { * if (this.Modifiers.Contains(vkCode)) * { * this.Modifiers.Remove(vkCode); * } * } * else * { * this.OnKeyUpEvent(vkCode); * } * } * * if (!this.callNextHook) * { * return new IntPtr(1); * } * * return base.HookProcedure(nCode, wParam, lParam); * } */ protected bool isModifier(int keyCode) { var key = KeyMapConverter.KeyCodeToKey(keyCode); return(key == OriginalKey.LeftWindows || key == OriginalKey.RightWindows || key == OriginalKey.LeftCtrl || key == OriginalKey.RightCtrl || key == OriginalKey.LeftShift || key == OriginalKey.RightShift || key == OriginalKey.LeftAlt || key == OriginalKey.RightAlt); }
private void InterceptKeyboard_KeyDownEvent(object sender, InterceptKeyboard.OriginalKeyEventArg e) { var key = KeyMapConverter.KeyCodeToKey(e.KeyCode); Debug.WriteLine("Keydown KeyCode {0}", key); if (e.equalsModifiers(new int[] { (int)OriginalKey.LeftWindows })) { Debug.WriteLine("With LeftWindows"); } /* * if ( key == OriginalKey.LeftWindows) * { * interceptKeyboard.callNextHook = false; * } else * { * interceptKeyboard.callNextHook = true; * } */ }
/** * <summary> * 現在のキーイベントを文字列として戻す * </summary> */ override public string ToString() { var key = KeyMapConverter.KeyCodeToKey(this.KeyCode); if (this.Modifiers.Length == 0) { return($"{key}"); } /* * var modifiers = ""; * for (int i = 0; i < this.Modifiers.Length; i++) * { * var mk = KeyMapConverter.KeyCodeToKey(this.Modifiers[i]); * modifiers += mk + " "; * } */ var modifiers = string.Join(" + ", this.Modifiers.Select(x => KeyMapConverter.KeyCodeToKey(x))); return($"{modifiers} + {key}"); }
private void InterceptKeyboard_KeyUpEvent(object sender, InterceptKeyboard.OriginalKeyEventArg e) { Debug.WriteLine("Keyup KeyCode {0}", KeyMapConverter.KeyCodeToKey(e.KeyCode)); }
/** * <summary> * 指定されたキーコード、修飾キーが押されたかを判別する * </summary> */ public bool Equals(OriginalKey key, int[] modifiers) { var originalKey = KeyMapConverter.KeyCodeToKey(this.KeyCode); return(key == originalKey && this.EqualsModifiers(modifiers)); }