protected override bool ProcessKeyPreview(ref Message m) { if (m_currentlyEditingShortcut == null) { return(base.ProcessKeyPreview(ref m)); } // Let's handle the message ourselves! // Keys key = (Keys) (((int)m.LParam >> 16) & 0xFFFF); // Interop.NativeModifierKeys modifiers = (Interop.NativeModifierKeys) ((int)m.LParam & 0xFFFF); Interop.NativeModifierKeys modifiers = Interop.NativeModifierKeys.None; Keys key = (Keys)m.WParam.ToInt32(); m_currentlyEditingShortcut.HandleKeyCombination(modifiers, key, (Shortcut _S, Interop.NativeModifierKeys _newModifiers, Keys _newKey, bool _canceled) => { if (!_canceled) { _S.Update(_newModifiers, _newKey); // Validate combination } RegisterHotKeys(); m_currentlyEditingShortcutLabel.Text = _S.ToString(); }); return(true); // Processed! }
public static void RegisterHotKey(Control _owner, int _ID, Interop.NativeModifierKeys modifier, Keys key) { if (!Interop.RegisterHotKey(_owner.Handle, 0, (uint)modifier, (uint)key)) { throw new InvalidOperationException("Couldn’t register the hot key."); } }
public void HandleKeyCombination(Interop.NativeModifierKeys _modifiers, Keys _key, KeyCombinationUpdateHandler _update) { if (_key != Keys.Y) { return; } bool canceled = _key == Keys.Escape; _update(this, _modifiers, _key, canceled); }
public Shortcut HandleHotKey(Interop.NativeModifierKeys _modifier, Keys _key) { foreach (Shortcut S in m_shortcuts) { if (_modifier == S.m_modifier && _key == S.m_key) { return(S); } } return(null); }
public void Update(PreviewKeyDownEventArgs e) { m_modifier = Interop.NativeModifierKeys.None; if ((e.Modifiers & Keys.LWin) != 0) { m_modifier |= Interop.NativeModifierKeys.Win; } if (e.Control) { m_modifier |= Interop.NativeModifierKeys.Control; } if (e.Alt) { m_modifier |= Interop.NativeModifierKeys.Alt; } if (e.Shift) { m_modifier |= Interop.NativeModifierKeys.Shift; } m_key = e.KeyCode; }
public void Update(Interop.NativeModifierKeys _modifiers, Keys _key) { m_modifier = _modifiers; m_key = _key; }
// From https://stackoverflow.com/questions/2450373/set-global-hotkeys-using-c-sharp /// <summary> /// Overridden to get the notifications. /// </summary> /// <param name="m"></param> protected override void WndProc(ref Message m) { switch (m.Msg) { case Interop.WM_HOTKEY: Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); Interop.NativeModifierKeys modifiers = (Interop.NativeModifierKeys)((int)m.LParam & 0xFFFF); PreferencesForm.Shortcut shortcut = m_preferenceForm.HandleHotKey(modifiers, key); if (shortcut == null) { break; } switch (shortcut.m_type) { case PreferencesForm.Shortcut.SHORTCUT.TOGGLE: // Toggle visibility if (Visible) { Hide(); } else { Show(); } break; case PreferencesForm.Shortcut.SHORTCUT.PASTE: // Create a quick fiche & ask for tags in a very light way try { IDataObject clipboardData = Clipboard.GetDataObject(); Fiche fiche = m_database.Sync_CreateFicheFromClipboard(clipboardData); SelectedFiche = fiche; if (!SelectedFicheEditor.EditorForm.Visible) { m_fastTaggerForm.Fiches = new Fiche[] { fiche }; // m_fastTaggerFormF.Location = this.Location + this.Size - F.Size; // Bottom-right of the screen m_fastTaggerForm.CenterOnPoint(Control.MousePosition); // Center on mouse if (!m_fastTaggerForm.Visible) { m_fastTaggerForm.Show(this); } } } catch (Exception _e) { // @TODO => Show an error dialog! LogError(_e); } break; case PreferencesForm.Shortcut.SHORTCUT.NEW: // Show and create a new empty editable fiche try { Fiche fiche = m_database.Sync_CreateFicheDescriptor(Fiche.TYPE.LOCAL_EDITABLE_WEBPAGE, "New Fiche", null, null, null); // Make it the last edited fiche m_ficheWebPageEditorForm.EditedFiche = fiche; // Show both the application & the fiche editor m_showEditorFormOnShowMain = true; Show(); } catch (Exception _e) { // @TODO => Show an error dialog! LogError(_e); } break; } break; case Interop.WM_KEYDOWN: // if ( m.HWnd == m_ficheEditorForm.Handle ) { // System.Diagnostics.Debug.WriteLine( "Bisou!" ); // } break; default: // Log every message! //System.Diagnostics.Debug.WriteLine( "Message: " + m ); break; } if (!m_fishing) { base.WndProc(ref m); // Process messages normally } else { // In "fishing mode", dispatch messages to background windows switch (m.Msg) { case 0x7f: case 0x84: // (WM_NCHITTEST) hwnd=0xaa1b64 wparam=0x0 lparam=0x34c040e result=0x0 case 0x20: // (WM_SETCURSOR) break; default: System.Diagnostics.Debug.WriteLine("Message: " + m); break; } // if ( m.Msg == Interop.WM_KEYUP || m.Msg == Interop.WM_KEYDOWN || m.Msg == Interop.WM_SYSKEYUP || m.Msg == Interop.WM_SYSKEYDOWN ) { if (m.Msg == Interop.WM_KEYUP || m.Msg == Interop.WM_SYSKEYUP) { if (((Keys)m.WParam.ToInt32()) == Keys.Menu) { System.Diagnostics.Debug.WriteLine("========================= EXIT FISHING! ========================="); ExitFishing(); } } DispatchMessageToBackgroundWindows(m); } }
/// <summary> /// Registers a hot key in the system. /// </summary> /// <param name="modifier">The modifiers that are associated with the hot key.</param> /// <param name="key">The key itself that is associated with the hot key.</param> public static void RegisterHotKey(Control _owner, Interop.NativeModifierKeys modifier, Keys key) { RegisterHotKey(_owner, 0, modifier, key); }