alansiqueira27
alansiqueira27

Reputation: 8526

Why does this Keyboard Input freeze the screen?

When I press any of the following keys, the object that I want to move, moves one square and then freeze. When I release the key, the screen unfreeze, and the object moved a lot. Why is this happenning? How can I solve this please? I am using WPF:


private void MainWindow_KeyDown(object sender, KeyEventArgs e)
{
            if (e.Key == Key.W)
            {
                playerService.move(Player.id, PlayerService.Direction.UP);
            }
            if (e.Key == Key.S)
            {
                playerService.move(Player.id, PlayerService.Direction.DOWN);
            }
            if (e.Key == Key.A)
            {
                playerService.move(Player.id, PlayerService.Direction.LEFT);
            }
            if (e.Key == Key.D)
            {
                playerService.move(Player.id, PlayerService.Direction.RIGHT);
            }
}

Edit: I am using the following event to draw. How can I control this consedering that I'm blocking the UI Thread?


CompositionTarget.Rendering += new EventHandler(Draw);

Upvotes: 1

Views: 318

Answers (1)

Andrew T Finnell
Andrew T Finnell

Reputation: 13638

It's because you are blocking in the UI thread. I know this is .NET but reference my other answer to a similar problem in Java.

Skipping over Java KeyEvents

Update

I would suggest looking into this project and determining how they are handing key events.

http://www.codeproject.com/KB/WPF/GrandPrix.aspx

It's a racing game written using WPF.

Upvotes: 2

Related Questions