在 UserControl 中捕获 Esc 键 [英] Capture Esc Key in UserControl

查看:33
本文介绍了在 UserControl 中捕获 Esc 键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 UserControl 创建一个 Custom Popup,因为这是 UWP 应用程序,我想隐藏 Popupcode> 当用户在键盘上按下 ESC 时.

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.

我尝试覆盖 UserControlOnKeyDown 方法,但是当我在键盘上按下 ESC 时,此方法从未执行.

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

推荐答案

我想在用户按键盘上的 ESC 时隐藏弹出窗口.

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

在 UWP 应用中,考虑使用 CoreWindow.CharacterReceived 事件

In UWP app, consider using CoreWindow.CharacterReceived event

在 UserControl 中,在 Constructor 方法中添加事件处理程序:

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

这篇关于在 UserControl 中捕获 Esc 键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆