private void KeyModifierCheck(object sender, EventArgs e) #endif { if (CtrlPressed) { if (!KeyBoard.IsKeyDown(Key.LeftCtrl) && !KeyBoard.IsKeyDown(Key.RightCtrl)) { CtrlPressed = false; } } if (ShiftPressed) { if (!KeyBoard.IsKeyDown(Key.LeftShift) && !KeyBoard.IsKeyDown(Key.RightShift)) { ShiftPressed = false; } } if (AltPressed) { if (!KeyBoard.IsKeyDown(Key.LeftAlt) && !KeyBoard.IsKeyDown(Key.RightAlt) && !KeyBoard.IsKeyDown(Key.System)) { AltPressed = false; } } if (!CtrlPressed && !ShiftPressed && !AltPressed) { keyCheck.Stop(); } }
private void ChangeEditKind(EditKind editKind) { if (editKind == _editKind) { return; } _editKind = editKind; switch (editKind) { case EditKind.None: // Make sure that the editor has focus if (ParentVisualElement != null) { ParentVisualElement.Focus(); } _margin.IsEditReadOnly = true; break; case EditKind.Command: case EditKind.SearchForward: case EditKind.SearchBackward: WpfKeyboard.Focus(_margin.CommandLineTextBox); _margin.IsEditReadOnly = false; break; default: Contract.FailEnumValue(editKind); break; } }
/// <summary> /// This method handles the list view scrolling keys for the specified dependency object. /// </summary> /// <param name="element">Dependency object</param> /// <param name="e">Key event arguments</param> /// <remarks> /// Handled keys: Up, Down, (Ctrl+)PageUp, (Ctrl+)PageDown, Home, End /// </remarks> public static void HandleListViewKeyEvents(this DependencyObject element, KeyEventArgs e) { var stack = element.GetInnerStackPanel(); if (stack == null) { return; } var sw = element.GetScrollViewer(); if (sw == null) { return; } var ctrlPressed = WindowsKeyboard.IsKeyDown(Key.LeftCtrl) || WindowsKeyboard.IsKeyDown(Key.RightCtrl); var multiplier = ctrlPressed ? 10 : 1; switch (e.Key) { case Key.Up: sw.ScrollToVerticalOffset(stack.VerticalOffset - 1.0); e.Handled = true; break; case Key.Down: sw.ScrollToVerticalOffset(stack.VerticalOffset + 1.0); e.Handled = true; break; case Key.PageUp: sw.ScrollToVerticalOffset(stack.VerticalOffset - stack.ViewportHeight * multiplier); e.Handled = true; break; case Key.PageDown: sw.ScrollToVerticalOffset(stack.VerticalOffset + stack.ViewportHeight * multiplier); e.Handled = true; break; case Key.Home: if (ctrlPressed) { sw.ScrollToTop(); e.Handled = true; } break; case Key.End: if (ctrlPressed) { sw.ScrollToBottom(); e.Handled = true; } break; } }
private void LastOptionBtn_Click(object sender, RoutedEventArgs e) { if (FocusTb == null) { FocusTb = tbList[0]; } else if (tbList.IndexOf(FocusTb) - 1 >= 0) { FocusTb = tbList[tbList.IndexOf(FocusTb) - 1]; } Keyboard.Focus(FocusTb); }
private void NextOptionBtn_Click(object sender, RoutedEventArgs e) { if (FocusTb == null) { FocusTb = tbList[0]; } else if (tbList.IndexOf(FocusTb) + 1 < tbList.Count) { FocusTb = tbList[tbList.IndexOf(FocusTb) + 1]; } Keyboard.Focus(FocusTb); }
void Editor_PreviewKeyUp(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Back: case Key.Delete: DeleteMissingVisualLabels(); break; case Key.C: case Key.V: case Key.X: if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { DeleteMissingVisualLabels(); } break; } }
protected KeyModifiers GetKeyboardModifiers() { KeyModifiers mod = KeyModifiers.None; if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl)) { mod |= KeyModifiers.Ctrl; } if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift)) { mod |= KeyModifiers.Shift; } if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)) { mod |= KeyModifiers.Alt; } if (Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin)) { mod |= KeyModifiers.Win; } return(mod); }
async private void GlobalHookKeyDown(object sender, KeyEventArgs e) { await Task.Run(() => ResetEvent.WaitOne()); try { Debug.WriteLine("KeyDown: \t\t{0}", e.KeyData); if (((int)e.KeyCode).isBetween(48, 90)) { string key = kc.ConvertToString(e.KeyCode); if (e.Shift ^ !Keyboard.IsKeyToggled(Key.CapsLock)) { key = key.ToLower(); } //addItem("KeyInput", key); keybuf += key; } else { CommitBuf(); //if(!string.IsNullOrEmpty(keybuf)) //{ // addItem("KeyInput", keybuf ); // keybuf = string.Empty; //} if (e.KeyCode.In(Keys.LShiftKey, Keys.RShiftKey)) { if (PressShift) { return; } else { PressShift = true; } } if (e.KeyCode.In(Keys.RControlKey, Keys.LControlKey)) { if (PressCtrl) { return; } else { PressCtrl = true; } } if (e.KeyCode.In(Keys.LMenu, Keys.RMenu)) { if (PressAlt) { return; } else { PressAlt = true; } } else if (e.KeyCode == Keys.LWin) { if (PressWin) { return; } else { PressWin = true; } } else if (e.KeyCode == Keys.RWin) {//! if (PressWin) { return; } else { PressWin = true; } } addItem("SpecialKeysDown", new { KeyCode = e.KeyData, Name = e.KeyData.ToString() }); } Console.WriteLine("KeyDown: \t\t{0}", e.KeyCode); } finally { //mainWindow.ScrollBottomGrid(); ResetEvent.Set(); } }
public void KeyToChar(Key key, ref IoCmd_t KeyDecode) { bool iscap; //bool caplock; bool shift; KeyDecode.key = key; KeyDecode.alt = Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt); KeyDecode.ctrl = Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl); KeyDecode.shift = Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift); if (KeyDecode.alt || KeyDecode.ctrl) { KeyDecode.printable = false; KeyDecode.type = 1; } else { KeyDecode.printable = true; KeyDecode.type = 0; } shift = KeyDecode.shift; //caplock = Console.CapsLock; //Keyboard.IsKeyToggled(Key.CapsLock); //iscap = (caplock && !shift) || (!caplock && shift); iscap = false; switch (key) { case Key.Enter: KeyDecode.character = '\n'; return; case Key.A: KeyDecode.character = (iscap ? 'A' : 'a'); return; case Key.B: KeyDecode.character = (iscap ? 'B' : 'b'); return; case Key.C: KeyDecode.character = (iscap ? 'C' : 'c'); return; case Key.D: KeyDecode.character = (iscap ? 'D' : 'd'); return; case Key.E: KeyDecode.character = (iscap ? 'E' : 'e'); return; case Key.F: KeyDecode.character = (iscap ? 'F' : 'f'); return; case Key.G: KeyDecode.character = (iscap ? 'G' : 'g'); return; case Key.H: KeyDecode.character = (iscap ? 'H' : 'h'); return; case Key.I: KeyDecode.character = (iscap ? 'I' : 'i'); return; case Key.J: KeyDecode.character = (iscap ? 'J' : 'j'); return; case Key.K: KeyDecode.character = (iscap ? 'K' : 'k'); return; case Key.L: KeyDecode.character = (iscap ? 'L' : 'l'); return; case Key.M: KeyDecode.character = (iscap ? 'M' : 'm'); return; case Key.N: KeyDecode.character = (iscap ? 'N' : 'n'); return; case Key.O: KeyDecode.character = (iscap ? 'O' : 'o'); return; case Key.P: KeyDecode.character = (iscap ? 'P' : 'p'); return; case Key.Q: KeyDecode.character = (iscap ? 'Q' : 'q'); return; case Key.R: KeyDecode.character = (iscap ? 'R' : 'r'); return; case Key.S: KeyDecode.character = (iscap ? 'S' : 's'); return; case Key.T: KeyDecode.character = (iscap ? 'T' : 't'); return; case Key.U: KeyDecode.character = (iscap ? 'U' : 'u'); return; case Key.V: KeyDecode.character = (iscap ? 'V' : 'v'); return; case Key.W: KeyDecode.character = (iscap ? 'W' : 'w'); return; case Key.X: KeyDecode.character = (iscap ? 'X' : 'x'); return; case Key.Y: KeyDecode.character = (iscap ? 'Y' : 'y'); return; case Key.Z: KeyDecode.character = (iscap ? 'Z' : 'z'); return; case Key.D0: KeyDecode.character = (shift ? ')' : '0'); return; case Key.D1: KeyDecode.character = (shift ? '!' : '1'); return; case Key.D2: KeyDecode.character = (shift ? '@' : '2'); return; case Key.D3: KeyDecode.character = (shift ? '#' : '3'); return; case Key.D4: KeyDecode.character = (shift ? '$' : '4'); return; case Key.D5: KeyDecode.character = (shift ? '%' : '5'); return; case Key.D6: KeyDecode.character = (shift ? '^' : '6'); return; case Key.D7: KeyDecode.character = (shift ? '&' : '7'); return; case Key.D8: KeyDecode.character = (shift ? '*' : '8'); return; case Key.D9: KeyDecode.character = (shift ? '(' : '9'); return; case Key.OemPlus: KeyDecode.character = (shift ? '+' : '='); return; case Key.OemMinus: KeyDecode.character = (shift ? '_' : '-'); return; case Key.OemQuestion: KeyDecode.character = (shift ? '?' : '/'); return; case Key.OemComma: KeyDecode.character = (shift ? '<' : ','); return; case Key.OemPeriod: KeyDecode.character = (shift ? '>' : '.'); return; case Key.OemOpenBrackets: KeyDecode.character = (shift ? '{' : '['); return; case Key.OemQuotes: KeyDecode.character = (shift ? '"' : '\''); return; case Key.Oem1: KeyDecode.character = (shift ? ':' : ';'); return; case Key.Oem3: KeyDecode.character = (shift ? '~' : '`'); return; case Key.Oem5: KeyDecode.character = (shift ? '|' : '\\'); return; case Key.Oem6: KeyDecode.character = (shift ? '}' : ']'); return; case Key.Tab: KeyDecode.character = '\t'; return; case Key.Space: KeyDecode.character = ' '; return; // Number Pad case Key.NumPad0: KeyDecode.character = '0'; return; case Key.NumPad1: KeyDecode.character = '1'; return; case Key.NumPad2: KeyDecode.character = '2'; return; case Key.NumPad3: KeyDecode.character = '3'; return; case Key.NumPad4: KeyDecode.character = '4'; return; case Key.NumPad5: KeyDecode.character = '5'; return; case Key.NumPad6: KeyDecode.character = '6'; return; case Key.NumPad7: KeyDecode.character = '7'; return; case Key.NumPad8: KeyDecode.character = '8'; return; case Key.NumPad9: KeyDecode.character = '9'; return; case Key.Subtract: KeyDecode.character = '-'; return; case Key.Add: KeyDecode.character = '+'; return; case Key.Decimal: KeyDecode.character = '.'; return; case Key.Divide: KeyDecode.character = '/'; return; case Key.Multiply: KeyDecode.character = '*'; return; default: KeyDecode.type = 1; KeyDecode.printable = false; KeyDecode.character = '\x00'; return; } }
/// <summary> /// Determines if we can remain focused on the element we think has focus /// </summary> /// <remarks> /// Invoked asynchronously by ReevaluateFocusAsync. /// Confirms that the element we think has focus is: /// - still enabled /// - still visible /// - still in the tree /// </remarks> private object ReevaluateFocusCallback(object arg) { _reevaluateFocusOperation = null; if (_focus == null) { return(null); } // // Reevaluate the eligability of the focused element to actually // have focus. If that element is no longer focusable, then search // for an ancestor that is. // DependencyObject element = _focus; while (element != null) { if (Keyboard.IsFocusable(element)) { break; } // Walk the current tree structure. element = DeferredElementTreeState.GetCoreParent(element, null); } // Get the PresentationSource that contains the element to be focused. PresentationSource presentationSource = null; DependencyObject visualContainer = InputElement.GetContainingVisual(element); if (visualContainer != null) { presentationSource = PresentationSource.CriticalFromVisual(visualContainer); } // The default action is to reset focus to the root element // of the active presentation source. bool moveFocus = true; DependencyObject moveFocusTo = null; if (presentationSource != null) { IKeyboardInputProvider keyboardProvider = presentationSource.GetInputProvider(typeof(KeyboardDevice)) as IKeyboardInputProvider; if (keyboardProvider != null) { // Confirm with the keyboard provider for this // presentation source that it has acquired focus. if (keyboardProvider.AcquireFocus(true)) { if (element == _focus) { // The focus element is still good. moveFocus = false; } else { // The focus element is no longer focusable, but we found // an ancestor that is, so move focus there. moveFocus = true; moveFocusTo = element; } } } } if (moveFocus) { if (moveFocusTo == null && _activeSource != null) { moveFocusTo = _activeSource.Value.RootVisual as DependencyObject; } Focus(moveFocusTo, /*askOld=*/ false, /*askNew=*/ true, /*forceToNullIfFailed=*/ true); } else { // Refresh FocusWithinProperty so that ReverseInherited Flags are updated. // // We only need to do this if there is any information about the old // tree structure. if (_focusTreeState != null && !_focusTreeState.IsEmpty) { UIElement.FocusWithinProperty.OnOriginValueChanged(_focus, _focus, ref _focusTreeState); } } return(null); }
private void TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, bool askOld, bool askNew, bool forceToNullIfFailed) { bool changeFocus = true; int timeStamp = Environment.TickCount; DependencyObject oldFocus = _focus; // This is required, used below to see if focus has been delegated if (newFocus != _focus) { // If requested, and there is currently something with focus // Send the PreviewLostKeyboardFocus event to see if the object losing focus want to cancel it. // - no need to check "changeFocus" here as it was just previously unconditionally set to true if (askOld && _focus != null) { KeyboardFocusChangedEventArgs previewLostFocus = new KeyboardFocusChangedEventArgs(this, timeStamp, (IInputElement)_focus, (IInputElement)newFocus); previewLostFocus.RoutedEvent = Keyboard.PreviewLostKeyboardFocusEvent; previewLostFocus.Source = _focus; if (_inputManager != null) { _inputManager.Value.ProcessInput(previewLostFocus); } // is handled the right indication of canceled? if (previewLostFocus.Handled) { changeFocus = false; } } // If requested, and there is an object to specified to take focus // Send the PreviewGotKeyboardFocus event to see if the object gaining focus want to cancel it. // - must also check "changeFocus", no point in checking if the "previewLostFocus" event // above already cancelled it if (askNew && changeFocus && newFocus != null) { KeyboardFocusChangedEventArgs previewGotFocus = new KeyboardFocusChangedEventArgs(this, timeStamp, (IInputElement)_focus, (IInputElement)newFocus); previewGotFocus.RoutedEvent = Keyboard.PreviewGotKeyboardFocusEvent; previewGotFocus.Source = newFocus; if (_inputManager != null) { _inputManager.Value.ProcessInput(previewGotFocus); } // is handled the right indication of canceled? if (previewGotFocus.Handled) { changeFocus = false; } } // If we are setting the focus to an element, see if the InputProvider // can take focus for us. if (changeFocus && newFocus != null) { if (keyboardInputProvider != null && Keyboard.IsFocusable(newFocus)) { // Tell the element we are about to acquire focus through // the input provider. The element losing focus and the // element receiving focus have all agreed to the // transaction. This is used by menus to configure the // behavior of focus changes. KeyboardInputProviderAcquireFocusEventArgs acquireFocus = new KeyboardInputProviderAcquireFocusEventArgs(this, timeStamp, changeFocus); acquireFocus.RoutedEvent = Keyboard.PreviewKeyboardInputProviderAcquireFocusEvent; acquireFocus.Source = newFocus; if (_inputManager != null) { _inputManager.Value.ProcessInput(acquireFocus); } // Acquire focus through the input provider. changeFocus = keyboardInputProvider.AcquireFocus(false); // Tell the element whether or not we were able to // acquire focus through the input provider. acquireFocus = new KeyboardInputProviderAcquireFocusEventArgs(this, timeStamp, changeFocus); acquireFocus.RoutedEvent = Keyboard.KeyboardInputProviderAcquireFocusEvent; acquireFocus.Source = newFocus; if (_inputManager != null) { _inputManager.Value.ProcessInput(acquireFocus); } } else { changeFocus = false; } } // If the ChangeFocus operation was cancelled or the AcquireFocus operation failed // and the "ForceToNullIfFailed" flag was set, we set focus to null if (!changeFocus && forceToNullIfFailed && oldFocus == _focus /* Focus is not delegated */) { // focus might be delegated (e.g. during PreviewGotKeyboardFocus) // without actually changing, if it was already on the delegated // element. We can't test for this directly, // but if focus is within the desired element we'll assume this // is what happened. IInputElement newFocusElement = newFocus as IInputElement; if (newFocusElement == null || !newFocusElement.IsKeyboardFocusWithin) { newFocus = null; changeFocus = true; } } // If both the old and new focus elements allowed it, and the // InputProvider has acquired it, go ahead and change our internal // sense of focus to the desired element. if (changeFocus) { ChangeFocus(newFocus, timeStamp); } } }