Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreviewKeyDown for Windows Store App ListBox

Is there an equivalent to the PreviewKeyDown for a Windows Store App? It isn't available.

I have exactly the same problem as described here:

I have a ListBox with a TextBox above it. I would like to use the arrow keys to navigate from the ListBox to the TextBox. The intention is that if the first item in the ListBox is selected, and the user keys up, the TextBox will get focus.

like image 652
Johann Avatar asked May 20 '13 14:05

Johann


1 Answers

Ah, tricky. Handling key events isn't super-obvious. Here's what you want:

public MainPage()
{
    this.InitializeComponent();
    Window.Current.CoreWindow.Dispatcher.AcceleratorKeyActivated += (s, args) =>
    {
        if ((args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown 
            || args.EventType == CoreAcceleratorKeyEventType.KeyDown)
            && (args.VirtualKey == VirtualKey.Up))
        {
            MoveUp();
        }
        else if ((args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown 
            || args.EventType == CoreAcceleratorKeyEventType.KeyDown)
            && (args.VirtualKey == VirtualKey.Down))
        {
            MoveDown();
        }
    };
}

private void MoveUp()
{
    // this part is up to you
    throw new NotImplementedException();
}

private void MoveDown()
{
    // this part is up to you
    throw new NotImplementedException();
}

Best of luck!

like image 144
Jerry Nixon Avatar answered Sep 20 '22 02:09

Jerry Nixon