Prevent focus changes when pressing the left / right keys

I would like my program to not change the focused element when the left / right keys are pressed, since I need something else when they are pressed. Right now, when the keys are pressed after clicking on a certain button to load everything, the focus is set on the list box next to that button, which does not allow the keys to do anything, but instead changes the selected index of the drop-down list.

I have this code for pressing a key in a window:

    private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (!PostTypeComboBox.IsFocused && !PredefinedSubsComboBox.IsFocused && !SortComboBox.IsFocused)
        {
            switch (e.Key)
            {
                case System.Windows.Input.Key.Left:
                    e.Handled = false;
                    ViewModel.GoToPreviousPost();
                    PreviousImageButton.Focus();
                    break;

                case System.Windows.Input.Key.Right:
                    e.Handled = false;
                    ViewModel.GoToNextPost();
                    NextImageButton.Focus();
                    break;

                default:
                    break;
            }
        }
    }

I tried with and without the following lines in switch statements:

    e.handled = False;
    PreviousImageButton.Focus();
    NextImageButton.Focus();

But I do nothing to prevent the focus from changing, and _ImageButton.Focus () does not change focus, it still returns to the callout.

+4
1

,

e.Handled = true;

0

Source: https://habr.com/ru/post/1534573/


All Articles