【问题标题】:Handle alt key(s) in UWP application在 UWP 应用程序中处理 alt 键
【发布时间】:2020-08-01 13:15:17
【问题描述】:

我在Window.Current.CoreWindow 上使用 KeyDown 和 KeyUp 处理程序来捕获 UWP 的 VNC 应用程序中的击键。这很好用,但有一个例外:alt (VirtualKey.Menu/LeftMenu/RightMenu) 永远不会发送到我的应用程序。此外,alt+letter 不会发送到处理程序。

我认为这是因为某些加速器处理程序在这些事件到达 CoreWindow 之前就吃掉了它们。有没有办法解决这个问题?

【问题讨论】:

    标签: c# xaml uwp


    【解决方案1】:

    尝试使用 Dispatcher.AcceleratorKeyActivated 代替,它处理 Alt 键。

    此外,CoreWindow 似乎存在问题。更多详情MSDN

    【讨论】:

    • 这绝对是我所需要的。太感谢了!我会在 23 小时内奖励赏金。不敢相信我在所有的谷歌搜索中都没有找到这个。
    • 我很高兴它有帮助! :)
    • 你还需要在Dispatcher.AcceleratorKeyActivated实现的开头添加if (!args.KeyStatus.IsMenuKeyDown) return;,否则即使不按住ALT键也会对按键做出反应。
    【解决方案2】:

    这是我如何在 Xamarin Forms 中解决此问题的示例。

    // A model to store the values of the key event.
    public class KeyEventArgs : EventArgs
    {
        public bool IsAltKeyPressed { get; set; }
    
        public string Key { get; set; }
    }
    
    // UWP Custom render
    public class MyPageViewRenderer : PageRenderer
    {
        /// <summary>
        /// Monitor windows core ui keypress when MyPageView is showing
        /// </summary>
        public MyPageViewRenderer() : base()
        {
            // When ExplorePage appears then attach to the ui core keydown event
            Loaded += (object sender, RoutedEventArgs e) =>
            {
                CoreWindow.GetForCurrentThread().Dispatcher.AcceleratorKeyActivated += Dispatcher_AcceleratorKeyActivated;
            };
    
            // When ExplorePage disappears then detach from the ui core keydown event
            Unloaded += (object sender, RoutedEventArgs e) =>
            {
                CoreWindow.GetForCurrentThread().Dispatcher.AcceleratorKeyActivated -= Dispatcher_AcceleratorKeyActivated;
            };
        }
    
        private void Dispatcher_AcceleratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args)
        {
            if ((args.EventType == CoreAcceleratorKeyEventType.KeyDown || args.EventType == CoreAcceleratorKeyEventType.SystemKeyDown)
                && !args.KeyStatus.WasKeyDown)
            {
                bool isAltKeyPressed = args.KeyStatus.IsMenuKeyDown;
                (Element as MyPageView).MyPageView_KeyPressed(Element, new KeyEventArgs { IsAltKeyPressed = isAltKeyPressed, Key = args.VirtualKey.ToString() });
            }
        }
    }
    
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class MyPageView : ContentPage
    {
        public MyPageView()
        {
            InitializeComponent();
    
            this.Focus();
        }
    
        public void MyPageView_KeyPressed(object sender, KeyEventArgs e)
        {
    
            (this.BindingContext as MyPageViewModel).CommandOnKeyPress?.Execute(e);
            
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-30
      • 1970-01-01
      • 2015-08-08
      • 2016-10-10
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多