Ejemplo n.º 1
0
        private void pinTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            List <TextBox> tbs = new List <TextBox>();

            tbs.Add(p1TextBox);
            tbs.Add(p2TextBox);
            tbs.Add(p3TextBox);
            tbs.Add(p4TextBox);
            int pIndex = 0;

            for (int i = 0; i < tbs.Count; i++)
            {
                if (tbs[i] == sender)
                {
                    pIndex = i;
                    break;
                }
            }
            if (e.Key == Key.Back)
            {
                e.Handled = true;
                if (pIndex == 0)
                {
                    tbs[pIndex].Text = "";
                }
                else
                {
                    tbs[pIndex].Text = "";
                    tbs[pIndex - 1].Focus();
                    tbs[pIndex - 1].Select(0, 1);
                    //tbs[pIndex - 1].SelectionLength = 0;
                }
            }
            else
            {
                e.Handled = true;
                char ch = KeyEventUtility.GetCharFromKey(e.Key);
                if (Char.IsDigit(ch))
                {
                    if (pIndex == tbs.Count - 1)
                    {
                        tbs[pIndex].Text = ch.ToString();
                    }
                    else
                    {
                        tbs[pIndex].Text = ch.ToString();
                        tbs[pIndex + 1].Focus();
                        tbs[pIndex + 1].Select(0, 0);
                        //tbs[pIndex + 1].SelectionLength = 0;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void OnWindowPreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.IsRepeat ||
                e.Handled)
            {
                return;
            }

            if (this.ribbon.IsCollapsed ||
                this.ribbon.IsEnabled == false ||
                this.window.IsActive == false)
            {
                return;
            }

            // Keytips should be cancelled if Alt+Num0 is pressed #241.
            // This allows entering special keys via numpad.
            if (e.KeyboardDevice.Modifiers == ModifierKeys.Alt &&
                e.SystemKey >= Key.NumPad0 &&
                e.SystemKey <= Key.NumPad9)
            {
                this.Terminate();
                return;
            }

            if (IsShowOrHideKey(e))
            {
                if (this.activeAdornerChain == null ||
                    this.activeAdornerChain.IsAdornerChainAlive == false ||
                    this.activeAdornerChain.AreAnyKeyTipsVisible == false)
                {
                    this.ShowDelayed();
                }
                else
                {
                    this.Terminate();
                }
            }
            else if (e.Key == Key.Escape &&
                     this.activeAdornerChain != null)
            {
                this.activeAdornerChain.ActiveKeyTipAdorner.Back();
                this.ClearUserInput();
                e.Handled = true;
            }
            else
            {
                if ((e.Key != Key.System && this.activeAdornerChain == null) ||
                    e.SystemKey == Key.Escape ||
                    (e.KeyboardDevice.Modifiers != ModifierKeys.Alt && this.activeAdornerChain == null))
                {
                    return;
                }

                var actualKey = e.Key == Key.System ? e.SystemKey : e.Key;
                // we need to get the real string input for the key because of keys like ä,ö,ü #258
                var key            = KeyEventUtility.GetStringFromKey(actualKey);
                var isKeyRealInput = string.IsNullOrEmpty(key) == false &&
                                     key != "\t";

                // Don't do anything and let WPF handle the rest
                if (isKeyRealInput == false)
                {
                    // This block is a "temporary" fix for keyboard navigation not matching the office behavior.
                    // If someone finds a way to implement it properly, here is your starting point.
                    // In office: If you navigate by keyboard (in menus) and keytips are shown they are shown or hidden based on the menu you are in.
                    // Implementing navigation the way office does would require complex focus/state tracking etc. so i decided to just terminate keytips and not restore focus.
                    {
                        this.backUpFocusedControl = null;
                        this.Terminate();
                    }

                    return;
                }

                var shownImmediately = false;

                // Should we show the keytips and immediately react to key?
                if (this.activeAdornerChain == null ||
                    this.activeAdornerChain.IsAdornerChainAlive == false ||
                    this.activeAdornerChain.AreAnyKeyTipsVisible == false)
                {
                    this.ShowImmediatly();
                    shownImmediately = true;
                }

                if (this.activeAdornerChain == null)
                {
                    return;
                }

                var previousInput = this.currentUserInput;
                this.currentUserInput += key;

                if (this.activeAdornerChain.ActiveKeyTipAdorner.ContainsKeyTipStartingWith(this.currentUserInput) == false)
                {
                    // Handles access-keys #258
                    if (shownImmediately)
                    {
                        this.Terminate();
                        return;
                    }

                    // If no key tips match the current input, continue with the previously entered and still correct keys.
                    this.currentUserInput = previousInput;
                    System.Media.SystemSounds.Beep.Play();
                    e.Handled = true;
                    return;
                }

                if (this.activeAdornerChain.ActiveKeyTipAdorner.Forward(this.currentUserInput, true))
                {
                    this.ClearUserInput();
                    e.Handled = true;
                    return;
                }

                this.activeAdornerChain.ActiveKeyTipAdorner.FilterKeyTips(this.currentUserInput);
                e.Handled = true;
            }
        }