protected virtual void SendKeyDown(short b, bool specialKey) { if (KeysHeld.Contains(b)) { throw new InputDeviceException(string.Format("Cannot press the key {0} as its already pressed", b)); } KeysHeld.Add(b); var keyUpDown = KeyboardWin32.GetSpecialKeyCode(specialKey, KeyboardInput.KeyUpDown.KEYEVENTF_KEYDOWN); KeyboardWin32.SendInput(KeyboardWin32.GetInputFor(b, keyUpDown)); }
protected virtual void SendKeyUp(short b, bool specialKey) { if (!KeysHeld.Contains(b)) { throw new InputDeviceException(string.Format("Cannot unpress the key {0}, it has not been pressed", b)); } KeysHeld.Remove(b); var keyUpDown = KeyboardWin32.GetSpecialKeyCode(specialKey, KeyboardInput.KeyUpDown.KEYEVENTF_KEYUP); KeyboardWin32.SendInput(KeyboardWin32.GetInputFor(b, keyUpDown)); }
/// <summary> /// Implements <see cref="IKeyboardWithActionListener.Send(string, IActionListener)"/> /// </summary> public virtual void Send(string keysToType, IActionListener actionListener) { if (HeldKeys.Count() > 0) { keysToType = keysToType.ToLower(); } CapsLockOn = false; foreach (var key in from c in keysToType let key = KeyboardWin32.VkKeyScan(c) where !c.Equals('\r') select key) { if (KeyboardWin32.ShiftKeyIsNeeded(key)) { SendKeyDown((short)KeyboardInput.SpecialKeys.SHIFT, false); } if (KeyboardWin32.CtrlKeyIsNeeded(key)) { SendKeyDown((short)KeyboardInput.SpecialKeys.CONTROL, false); } if (KeyboardWin32.AltKeyIsNeeded(key)) { SendKeyDown((short)KeyboardInput.SpecialKeys.ALT, false); } Press(key, false); if (KeyboardWin32.ShiftKeyIsNeeded(key)) { SendKeyUp((short)KeyboardInput.SpecialKeys.SHIFT, false); } if (KeyboardWin32.CtrlKeyIsNeeded(key)) { SendKeyUp((short)KeyboardInput.SpecialKeys.CONTROL, false); } if (KeyboardWin32.AltKeyIsNeeded(key)) { SendKeyUp((short)KeyboardInput.SpecialKeys.ALT, false); } } actionListener.ActionPerformed(Action.WindowMessage); }