Detecting keys pressed without events in UWP

你。 提交于 2019-12-12 01:46:41

问题


I am writing a simple 2D game engine for UWP. I have a code snippet taken from XNA, checking for if a certain key is pressed (see below), and I'm trying to do something similar.

Indeed, in my game engine I'd rather not use the dedicated event handlers (KeyDown etc), but instead simply check for pressed keys in my game loop - if this is possible to do. One reason for this is that I can then control for example how often a bullet fires if the key is continuously pressed (using a timer and mod). It would also let the keys be independent of each other, such as up and right arrow key gives diagonal movement for player 1, while another player (using the same keyboard) uses keys A and W to move his/her character in another direction, and they both occasionally fire their laser guns. Finally, I have always considered using the event handlers working separately from my the rest of my game code, resulting in lack of control. This is especially true for the KeyPress event, I think.

The code I have found looks like this:

 public void DrawHelp()
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Tab))
            {

..and this is what I'm trying to use. However, the Keyboard class words don't come up in Intellisense for me (though the Windows.UI.Xaml.Input; using is attached by default).

What I'm thinking of is basically a series of if statements (the code is wrong - it's just to show what I want):

    int counter = 0;

    // Raised every tick while the DispatcherTimer is active.
    private void EachTick(object sender, object e)
    {
        TrackKeyboard();
        CalculateMoves();
        Physics.CollisionDetection();
        DrawGame();
    }

    private void TrackKeyboard()
    {
        if (Keyboard.GetState().IsKeyDown(Keys.P)) PauseGame();

        if (counter % 10 == 0) //control movements
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Down)) AddSpeed("Player1", 0, -1);
            if (Keyboard.GetState().IsKeyDown(Keys.Left)) AddSpeed("Player1", -1, 0);
            if (Keyboard.GetState().IsKeyDown(Keys.Up)) AddSpeed("Player1", -1, 0);
            if (Keyboard.GetState().IsKeyDown(Keys.Right)) AddSpeed("Player1", 0, 1);
            if (Keyboard.GetState().IsKeyDown(Keys.W)) AddSpeed("Player2", -1, 0);
            if (Keyboard.GetState().IsKeyDown(Keys.D)) AddSpeed("Player2", 0, 1);
            if (Keyboard.GetState().IsKeyDown(Keys.S)) AddSpeed("Player2", -1, 0);
            if (Keyboard.GetState().IsKeyDown(Keys.A)) AddSpeed("Player2", 0, 1);
        }

        if (counter % 5 == 0) //fire
        {
            if (Keyboard.GetState().IsKeyDown(Keys.Space)) Fire("Player1");
            if (Keyboard.GetState().IsKeyDown(Keys.X)) Fire("Player2");
        }
    }

So, is it possible to write something like this in UWP, and if so: How?

I've done some searching on Bing and Google for this, but there is still very little info on UWP.

I have found a similar question on CodeProject (https://www.codeproject.com/Questions/1107441/How-do-I-register-events-from-two-players-in-UWP), but the answer is too difficult for me to follow, and in any case, I'd much rather include this code in the game loop, as described above.

(Also please let me know if the route I'm trying to take is stupid for some technical reason that I may not be aware of.)

Thanks so much and Happy New Year!

Petter


回答1:


In UWP, you should check the key state from CoreWindow using these methods:

Window.Current.CoreWindow.GetKeyState(VirtualKey).HasFlag(CoreVirtualKeyStates);

You have to specify the key and state you want to get info about for example like this:

if(Window.Current.CoreWindow.GetKeyState(VirtualKey.Escape).HasFlag(CoreVirtualKeyStates.Down))
{
    // Escape key pressed
}

You should not only compare the value returned from the GetKeyState method like this:

Window.Current.CoreWindow.GetKeyState(VirtualKey) == CoreVirtualKeyStates.Down;

since it could return CoreVirtualKeyStates.Down | CoreVirtualKeyStates.Locked when the key is pressed and the condition will then be false.



来源:https://stackoverflow.com/questions/41406898/detecting-keys-pressed-without-events-in-uwp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!