Capture Esc Key in UserControl

99封情书 提交于 2019-12-04 04:46:40

问题


I want to create a Custom Popup using UserControl, because this is UWP apps, I want to hide Popup when user press ESC on Keyboard.

I try to override OnKeyDown method of UserControl but this method never executed when I press ESC on Keyboard.

 protected override void OnKeyDown(KeyRoutedEventArgs e)
 {
    if (e.Key == VirtualKey.Escape)
    {
       this.Visibility = Visibility.Colapse;
    }
 }

回答1:


I want to hide Popup when user press ESC on Keyboard.

In UWP app, consider using CoreWindow.CharacterReceived event

In UserControl, add event handler in the Constructor method:

public CustomPopupControl()
{
            this.InitializeComponent();

            Window.Current.CoreWindow.CharacterReceived += CoreWindow_CharacterReceived;
}

private void CoreWindow_CharacterReceived(CoreWindow sender, CharacterReceivedEventArgs args)
{
            if(args.KeyCode==27) //ESC
            {
        //Do somthing
                this.Visibility = Visibility.Collapsed;
            }
}


来源:https://stackoverflow.com/questions/34371280/capture-esc-key-in-usercontrol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!