0

我正在使用 windows 10 中的通用应用程序尝试使用 sharpdx takeit,但无法让输入正常工作。我知道我必须为不同的设备创建不同的输入,但现在我只想尝试键盘。

所以我首先点击这个链接来设置项目:Is there a SharpDx Template, for a Windows Universal App?

现在我正在尝试像往常一样输入:

在构造函数中:

_keyboardManager = new KeyboardManager(this);

在更新方法中:

var state = _keyboardManager.GetState();
if (state.IsKeyDown(Keys.B))
{
  //do stuff
}

但他从未注册过钥匙 B 已关闭(或我尝试过的任何其他钥匙)。我也试过 GetDownKeys() 但列表总是空的。

那么有人知道在这里做什么吗?

4

2 回答 2

0

赫雷卡!

我遇到了同样的问题,但使用的是 Windows 8.1。我花了 2 天时间才找到解决方案。这是愚蠢的 SwapChainPanel,它不能成为焦点,因为该类没有从 Control 类继承,所以它不会处理键盘事件。

解决方案在这里,也就是说,您必须放置一个从 Control 类继承的 XAML 元素,例如 Button 来处理事件。我的 XAML 文件是这样的:

<SwapChainPanel  x:Name="_swapChainPanel"
                 Loaded="_swapChainPanel_Loaded"
                 KeyDown="_swapChainPanel_KeyDown">
    <Button x:Name="_swapChainButton"
            Content="Button"
            HorizontalAlignment="Left"
            Height="0"
            VerticalAlignment="Top"
            Width="0" 
            KeyDown="_swapChainButton_KeyDown">
    </Button>
</SwapChainPanel>

在 XAML.cs 中,我以这种方式处理事件:

private void _swapChainButton_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        e.Handled = false; //This will pass the event to its parent, which is the _swapChainPanel
    }

private void _swapChainPanel_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
    {
        game.KeyboardEvent();
    }

在 KeyboardEvent() 方法中,我放置了 if things... 你必须使用代码手动使按钮成为焦点。"_swapChainButton.Focus(FocusState.Programmatic);"

但最后,它对我来说不是那么好,它太慢了。它有延迟。:/

于 2015-04-19T23:02:21.937 回答
0

还有另一个更好、更简单的版本:

using Windows.UI.Core;
using SharpDX.Toolkit;
using Windows.System;
namespace Example
{
    class MyGame : Game
    {
        public MyGame()
        {
            CoreWindow.GetForCurrentThread().KeyDown += MyGame_KeyDown;
        }
        void MyGame_KeyDown(CoreWindow sender, KeyEventArgs args)
        {
            System.Diagnostics.Debug.WriteLine(args.VirtualKey);
        }
    }
}

您必须订阅 CoreWindow 的事件,就是这样 :)

于 2015-04-22T16:10:56.243 回答