/// <summary> /// Called when the user presses a key /// </summary> // ReSharper disable once RedundantAssignment public static bool KeyDownHandler(KeyEventArgs e) { // if set to true, the keyinput is completly intercepted, otherwise npp sill does its stuff bool handled = false; MenuItem menuItem = null; try { // Since it's a keydown message, we can receive this a lot if the user let a button pressed var isSpamming = Utils.IsSpamming(e.KeyCode.ToString(), 100, true); // check if the user triggered a 3P function defined in the AppliMenu menuItem = TriggeredMenuItem(AppliMenu.Instance.ShortcutableItemList, isSpamming, e, ref handled); if (handled) { return(true); } // Autocompletion if (AutoCompletion.IsVisible) { handled = AutoCompletion.PerformKeyDown(e); } // next tooltip if (!handled && InfoToolTip.IsVisible && e.Control && (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)) { if (e.KeyCode == Keys.Up) { InfoToolTip.IndexToShow--; } else { InfoToolTip.IndexToShow++; } InfoToolTip.TryToShowIndex(); handled = true; } if (handled) { return(true); } // Ok so... when we open a form in notepad++, we can't use the overrides PreviewKeyDown / KeyDown // like we normally can, for some reasons, they don't react to certain keys (like enter!) // It only works "almost normally" if we ShowDialog() the form?! // So i gave up and handle things here! // Each control / form that should use a key not handled by Npp should implement a method // "PerformKeyDown" that will be triggered from here (see below) var curControl = Win32Api.GetFocusedControl(); if (curControl != null) { var invokeResponse = curControl.InvokeMethod("PerformKeyDown", new[] { (object)e }); if (invokeResponse != null && (bool)invokeResponse) { return(true); } } var curWindow = Control.FromHandle(WinApi.GetForegroundWindow()); if (curWindow != null) { var invokeResponse = curWindow.InvokeMethod("PerformKeyDown", new[] { (object)e }); if (invokeResponse != null && (bool)invokeResponse) { return(true); } } // Close interfacePopups if (e.KeyCode == Keys.PageDown || e.KeyCode == Keys.PageUp || e.KeyCode == Keys.Next || e.KeyCode == Keys.Prior) { ClosePopups(); } } catch (Exception ex) { ErrorHandler.ShowErrors(ex, "Occurred in : " + (menuItem == null ? new ShortcutKey(e.Control, e.Alt, e.Shift, e.KeyCode).ToString() : menuItem.ItemId)); } return(handled); }
/// <summary> /// Called when the user presses a key /// </summary> // ReSharper disable once RedundantAssignment private static bool KeyDownHandler(Keys key, KeyModifiers keyModifiers) { // if set to true, the keyinput is completly intercepted, otherwise npp sill does its stuff bool handled = false; MenuItem menuItem = null; try { // Since it's a keydown message, we can receive this a lot if the user let a button pressed var isSpamming = Utils.IsSpamming(key.ToString(), 100, true); //HACK: // Ok so... when we open a form in notepad++, we can't use the overrides PreviewKeyDown / KeyDown // like we normally can, for some reasons, they don't react to certain keys (like enter!) // It only works "almost normally" if we ShowDialog() the form?! Wtf right? // So i gave up and handle things here! if (Appli.IsFocused()) { handled = Appli.Form.HandleKeyPressed(key, keyModifiers); } else { // same shit for the YamuiMenu var curMenu = (Control.FromHandle(WinApi.GetForegroundWindow())); var menu = curMenu as YamuiMenu; if (menu != null) { menu.OnKeyDown(key); } } // check if the user triggered a 3P function defined in the AppliMenu menuItem = TriggeredMenuItem(AppliMenu.Instance.ShortcutableItemList, isSpamming, key, keyModifiers, ref handled); if (handled) { return(true); } // The following is specific to 3P so don't go further if we are not on a valid file if (!IsCurrentFileProgress) { return(false); } // Close interfacePopups if (key == Keys.PageDown || key == Keys.PageUp || key == Keys.Next || key == Keys.Prior) { ClosePopups(); } // Autocompletion if (AutoComplete.IsVisible) { if (key == Keys.Up || key == Keys.Down || key == Keys.Tab || key == Keys.Return || key == Keys.Escape) { handled = AutoComplete.OnKeyDown(key); } else { if ((key == Keys.Right || key == Keys.Left) && keyModifiers.IsAlt) { handled = AutoComplete.OnKeyDown(key); } } } else { // snippet ? if (key == Keys.Tab || key == Keys.Escape || key == Keys.Return) { if (!keyModifiers.IsCtrl && !keyModifiers.IsAlt && !keyModifiers.IsShift) { if (!Snippets.InsertionActive) { //no snippet insertion in progress if (key == Keys.Tab) { if (Snippets.TriggerCodeSnippetInsertion()) { handled = true; } } } else { //there is a snippet insertion in progress if (key == Keys.Tab) { if (Snippets.NavigateToNextParam()) { handled = true; } } else if (key == Keys.Escape || key == Keys.Return) { Snippets.FinalizeCurrent(); if (key == Keys.Return) { handled = true; } } } } } } // next tooltip if (keyModifiers.IsCtrl && InfoToolTip.IsVisible && (key == Keys.Up || key == Keys.Down)) { if (key == Keys.Up) { InfoToolTip.IndexToShow--; } else { InfoToolTip.IndexToShow++; } InfoToolTip.TryToShowIndex(); handled = true; } } catch (Exception e) { ErrorHandler.ShowErrors(e, "Occured in : " + (menuItem == null ? (new ShortcutKey(keyModifiers.IsCtrl, keyModifiers.IsAlt, keyModifiers.IsShift, key)).ToString() : menuItem.ItemId)); } return(handled); }