A
A
Alikhan2022-03-02 11:32:30
WPF
Alikhan, 2022-03-02 11:32:30

How to track pressing multiple WPF keys at once?

I'm making a game in WPF and ran into a problem.
We know that you can add a KeyDown event handler to an element.
I have a motion keypress handling function like this:

private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right)
            {
                this.player.x += this.player.speed;
                this.player.character.Margin = new Thickness(this.player.x, this.player.y, 0, 0);
            }

            if (e.Key == Key.Left)
            {
                this.player.x -= this.player.speed;
                this.player.character.Margin = new Thickness(this.player.x, this.player.y, 0, 0);
            }
        }


But here it processes only one key, and not several at once (let's say down and to the left).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2022-03-02
Mulaev @AlikhanPython

private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if(Keyboard.IsKeyDown(Key.Left) && Keyboard.IsKeyDown(Key.Down))
            {
                MessageBox.Show("Нажаты клавиши вниз и влево");
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question