その1では、CoreWindowのKeyDownを使ってました。 DispatcherのAccelaratorKeyActivatedイベントも使える奴みたいですね。 こっちでやるとTextBoxがフォーカス持っててもEnterとかのイベントが拾えたのでこっちのほうがいいかも。
こんな感じのBehaviorになりました。
using Microsoft.Xaml.Interactivity; using Windows.System; using Windows.UI.Core; using Windows.UI.Xaml; namespace App65 { publicclass KeyEventTriggerBehavior : DependencyObject, IBehavior { public DependencyObject AssociatedObject { get; private set; } public ActionCollection Actions { get { if (GetValue(ActionsProperty) == null) { this.Actions = new ActionCollection(); } return (ActionCollection)GetValue(ActionsProperty); } set { SetValue(ActionsProperty, value); } } publicstaticreadonly DependencyProperty ActionsProperty = DependencyProperty.Register( nameof(Actions), typeof(ActionCollection), typeof(KeyEventTriggerBehavior), new PropertyMetadata(null)); publicbool ShiftKey { get { return (bool)GetValue(ShiftKeyProperty); } set { SetValue(ShiftKeyProperty, value); } } // Using a DependencyProperty as the backing store for ShiftKey. This enables animation, styling, binding, etc...publicstaticreadonly DependencyProperty ShiftKeyProperty = DependencyProperty.Register("ShiftKey", typeof(bool), typeof(KeyEventTriggerBehavior), new PropertyMetadata(false)); publicbool CtrlKey { get { return (bool)GetValue(CtrlKeyProperty); } set { SetValue(CtrlKeyProperty, value); } } // Using a DependencyProperty as the backing store for CtrlKey. This enables animation, styling, binding, etc...publicstaticreadonly DependencyProperty CtrlKeyProperty = DependencyProperty.Register("CtrlKey", typeof(bool), typeof(KeyEventTriggerBehavior), new PropertyMetadata(false)); public VirtualKey Key { get { return (VirtualKey)GetValue(KeyProperty); } set { SetValue(KeyProperty, value); } } // Using a DependencyProperty as the backing store for Key. This enables animation, styling, binding, etc...publicstaticreadonly DependencyProperty KeyProperty = DependencyProperty.Register("Key", typeof(VirtualKey), typeof(KeyEventTriggerBehavior), new PropertyMetadata(VirtualKey.None)); publicvoid Attach(DependencyObject associatedObject) { this.AssociatedObject = associatedObject; this.Register(); } publicvoid Detach() { this.Unregister(); this.AssociatedObject = null; } privatevoid Register() { var fe = this.AssociatedObject as FrameworkElement; if (fe == null) { return; } fe.Unloaded += this.Fe_Unloaded; this.Dispatcher.AcceleratorKeyActivated += this.Dispatcher_AccelaratorKeyActivated; } privatevoid Unregister() { var fe = this.AssociatedObject as FrameworkElement; if (fe == null) { return; } fe.Unloaded -= this.Fe_Unloaded; this.Dispatcher.AcceleratorKeyActivated -= this.Dispatcher_AccelaratorKeyActivated; } privatevoid Dispatcher_AccelaratorKeyActivated(CoreDispatcher sender, AcceleratorKeyEventArgs args) { if (args.KeyStatus.RepeatCount != 1) { return; } if (args.EventType != CoreAcceleratorKeyEventType.KeyDown) { return; } if (this.ShiftKey && (Window.Current.CoreWindow.GetKeyState(VirtualKey.Shift) & CoreVirtualKeyStates.Down) != CoreVirtualKeyStates.Down) { return; } if (this.CtrlKey && (Window.Current.CoreWindow.GetKeyState(VirtualKey.Control) & CoreVirtualKeyStates.Down) != CoreVirtualKeyStates.Down) { return; } if (this.Key == args.VirtualKey) { Interaction.ExecuteActions(this, this.Actions, args); args.Handled = true; } } privatevoid Fe_Unloaded(object sender, RoutedEventArgs e) { this.Unregister(); } } }
使い方は前回と同じ。