我在Window.Current.CoreWindow
上使用KeyDown和KeyUp处理程序来捕获UWP的VNC应用程序中的击键。这有一个例外:alt(VirtualKey.Menu/LeftMenu/RightMenu
)永远不会发送到我的应用程序。此外,alt +字母也不会发送给处理程序。
我认为这是因为某些加速器处理程序在到达CoreWindow之前正在吃这些事件。有没有办法解决这个问题?
答案 0 :(得分:11)
尝试使用Dispatcher.AcceleratorKeyActivated,它会处理Alt键。
此外,似乎CoreWindow存在问题。有关MSDN
的更多详情答案 1 :(得分:0)
下面是我如何在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);
}
}