简体   繁体   English

在UWP中检测没有事件按下的按键

[英]Detecting keys pressed without events in UWP

I am writing a simple 2D game engine for UWP. 我正在为UWP写一个简单的2D游戏引擎。 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. 我有一个摘自XNA的代码段,检查是否按下了某个键(请参见下文),并且我正在尝试执行类似的操作。

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. 确实,在我的游戏引擎中,我宁愿不使用专用的事件处理程序(KeyDown等),而只是在游戏循环中检查是否有按下的键-如果可能的话。 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). 原因之一是,我可以控制例如连续按下键(使用计时器和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. 它还将使按键彼此独立,例如,向上和向右箭头键为玩家1提供对角线移动,而另一个玩家(使用同一键盘)则使用键A和W在另一个方向上移动他/她的角色,而且他们俩偶尔都会发射激光枪。 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. 我认为对于KeyPress事件尤其如此。

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). 但是,键盘类的单词对我来说不是Intellisense的(尽管Windows.UI.Xaml.Input;默认情况下是using)。

What I'm thinking of is basically a series of if statements (the code is wrong - it's just to show what I want): 我在想的基本上是一系列if语句(代码错误-只是为了显示我想要的):

    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? 因此,是否可以在UWP中编写类似的内容?

I've done some searching on Bing and Google for this, but there is still very little info on UWP. 我已经在Bing和Google上进行了一些搜索,但是关于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. 我在CodeProject( https://www.codeproject.com/Questions/1107441/How-do-I-register-events-from-two-players-in-UWP )中发现了类似的问题,但是答案太困难了对于我而言,无论如何,如上所述,我宁愿将此代码包含在游戏循环中。

(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 皮特

In UWP, you should check the key state from CoreWindow using these methods: 在UWP中,您应该使用以下方法从CoreWindow检查键状态:

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: 您不仅应该像这样比较从GetKeyState方法返回的值:

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

since it could return CoreVirtualKeyStates.Down | CoreVirtualKeyStates.Locked 因为它可以返回CoreVirtualKeyStates.Down | CoreVirtualKeyStates.Locked CoreVirtualKeyStates.Down | CoreVirtualKeyStates.Locked when the key is pressed and the condition will then be false. 当按键被按下时, CoreVirtualKeyStates.Down | CoreVirtualKeyStates.LockedCoreVirtualKeyStates.Down | CoreVirtualKeyStates.Locked ,条件将为假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM