PreviewKeyDown for Windows Store App ListBox

后端 未结 1 411
走了就别回头了
走了就别回头了 2021-02-10 10:38

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

I have exactly the same problem as described her

相关标签:
1条回答
  • 2021-02-10 11:40

    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!

    0 讨论(0)
提交回复
热议问题