public bool OnMnemonic(ref MSG msg, ModifierKeys modifiers) { switch (msg.message) { case 260: case 262: case 263: string key = new string((char)(int)msg.wParam, 1); if (key.Length > 0) { IntPtr hwnd = new WindowInteropHelper(Window).Owner; if (hwnd == IntPtr.Zero) { var fromVisual = (HwndSource)PresentationSource.FromVisual(Application.Current.MainWindow); if (fromVisual != null) { hwnd = fromVisual.Handle; } } if (hwnd != IntPtr.Zero) { HwndSource hwndSource = HwndSource.FromHwnd(hwnd); if (hwndSource != null && AccessKeyManager.IsKeyRegistered(hwndSource, key)) { AccessKeyManager.ProcessKey(hwndSource, key, false); return(true); } } } break; } return(false); }
public void AccessKeyManagerTest() { SimpleButtonBase b = new SimpleButtonBase(); Assert.IsFalse(AccessKeyManager.IsKeyRegistered(null, "1"), "Before"); b.Content = "_1"; Assert.IsFalse(AccessKeyManager.IsKeyRegistered(null, "1"), "After"); }
/// <summary> /// This is overridden to pass hot keys on to the contained user control. /// </summary> /// <param name="m">The message to pre-process</param> /// <returns>True if the message was handled, false if not</returns> /// <remarks>When a WPF user control is hosted in a docked tool window, the hot keys no longer work. /// This works around the problem by manually seeing if the control makes use of the hot key, and if /// it does, processing it here.</remarks> protected override bool PreProcessMessage(ref System.Windows.Forms.Message m) { if (m.Msg == 0x0100 /* WM_KEYDOWN */) { System.Windows.Forms.Keys keyCode = (System.Windows.Forms.Keys)m.WParam & System.Windows.Forms.Keys.KeyCode; if (keyCode == System.Windows.Forms.Keys.F1) { ApplicationCommands.Help.Execute(null, (UserControl)base.Content); return(true); } } if (m.Msg == 0x0104 /* WM_SYSKEYDOWN */) { if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)) { // Cache a copy of the scope on first use if (scope == null && base.Content != null) { // Get the scope for handling hot keys. The key used here doesn't matter. We're just // getting the scope to use. AccessKeyPressedEventArgs e = new AccessKeyPressedEventArgs("X"); ((UserControl)base.Content).RaiseEvent(e); scope = e.Scope; } string key = ((char)m.WParam).ToString(); // See if the hot key is registered for the control. If so, handle it. Ignore anything // that isn't 'A' to 'Z' if (scope != null && key[0] >= 'A' && key[0] <= 'Z' && AccessKeyManager.IsKeyRegistered(scope, key)) { AccessKeyManager.ProcessKey(scope, key, false); return(true); } } } return(base.PreProcessMessage(ref m)); }
bool System.Windows.Interop.IKeyboardInputSink.OnMnemonic(ref MSG msg, ModifierKeys modifiers) { switch (msg.message) { case 262: case 263: { string str = new string((char)((int)msg.wParam), 1); if (str == null || str.Length <= 0) { break; } IntPtr owner = (new WindowInteropHelper(this.Window)).Owner; HwndSource hwndSource = HwndSource.FromHwnd(owner); if (hwndSource == null || !AccessKeyManager.IsKeyRegistered(hwndSource, str)) { break; } AccessKeyManager.ProcessKey(hwndSource, str, false); return(true); } } return(false); }