0

I'm trying out sharpdx tookit with universal app in windows 10 and can't get the input to work. I know I have to create different input for different devices but for now I just want to try keyboard.

So I follow this link first to set up the project: Is there a SharpDx Template, for a Windows Universal App?

And right now I'm trying input like I usually do it:

In the constuctor:

_keyboardManager = new KeyboardManager(this);

In the update method:

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

But he never register that the key B is down (or any other key I have tried). I have also tried GetDownKeys() but the list is always empty.

So does any one have any idea what to do here?

Community
  • 1
  • 1
MilleB
  • 1,470
  • 2
  • 19
  • 32

2 Answers2

0

HEUREKA!

I had the same problem, but with windows 8.1. It took me 2 days to find a solution. It's the foolish SwapChainPanel, which can't be in focus because the class doesn't inherit from Control class, so it won't handle keyboard events.

The solution is HERE that is to say, you have to put an XAML element that's inherited from the Control class, like Button to handle the event. My XAML file is like this:

<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>

In the XAML.cs I handled the events this way:

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();
    }

In the KeyboardEvent() method I put the if things... and you have to make the button in focus manually, with code. "_swapChainButton.Focus(FocusState.Programmatic);"

But last, it's not so good for me, it's so slow. It has delay. :/

Totati
  • 1,489
  • 12
  • 23
0

And there's an another, better, easier version:

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);
        }
    }
}

You have to subscribe to the CoreWindow's event, that's it :)

Totati
  • 1,489
  • 12
  • 23