diff --git a/ShareX.HelpersLib/Controls/HotkeySelectionButton.cs b/ShareX.HelpersLib/Controls/HotkeySelectionButton.cs new file mode 100644 index 000000000..3854ae836 --- /dev/null +++ b/ShareX.HelpersLib/Controls/HotkeySelectionButton.cs @@ -0,0 +1,181 @@ +#region License Information (GPL v3) + +/* + ShareX - A program that allows you to take screenshots and share any file type + Copyright (c) 2007-2016 ShareX Team + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + + Optionally you can also view the license at . +*/ + +#endregion License Information (GPL v3) + +using System; +using System.ComponentModel; +using System.Drawing; +using System.Windows.Forms; + +namespace ShareX.HelpersLib.Controls +{ + public class HotkeySelectionButton : Button + { + public event EventHandler HotkeyChanged; + + [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public HotkeyInfo HotkeyInfo { get; set; } = new HotkeyInfo(); + + [Browsable(false)] + public bool EditingHotkey { get; private set; } + + [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public override string Text { get; set; } + + public HotkeySelectionButton() + { + SetDefaultButtonText(); + } + + private void SetDefaultButtonText() + { + Text = "Select a hotkey..."; + } + + private void StartEditing() + { + EditingHotkey = true; + + BackColor = Color.FromArgb(225, 255, 225); + SetDefaultButtonText(); + + HotkeyInfo.Hotkey = Keys.None; + HotkeyInfo.Win = false; + + OnHotkeyChanged(); + } + + private void StopEditing() + { + EditingHotkey = false; + + if (HotkeyInfo.IsOnlyModifiers) + { + HotkeyInfo.Hotkey = Keys.None; + } + + BackColor = SystemColors.Control; + UseVisualStyleBackColor = true; + + OnHotkeyChanged(); + UpdateHotkeyText(); + } + + private void UpdateHotkeyText() + { + Text = HotkeyInfo.ToString(); + } + + protected override void OnMouseClick(MouseEventArgs e) + { + if (EditingHotkey) + { + StopEditing(); + } + else + { + StartEditing(); + } + + base.OnMouseClick(e); + } + + protected override void OnLeave(EventArgs e) + { + if (EditingHotkey) + { + StopEditing(); + } + + base.OnLeave(e); + } + + protected override void OnPreviewKeyDown(PreviewKeyDownEventArgs e) + { + if (EditingHotkey) + { + // For handle Tab key etc. + e.IsInputKey = true; + } + + base.OnPreviewKeyDown(e); + } + + protected override void OnKeyDown(KeyEventArgs kevent) + { + kevent.SuppressKeyPress = true; + + if (EditingHotkey) + { + if (kevent.KeyData == Keys.Escape) + { + HotkeyInfo.Hotkey = Keys.None; + StopEditing(); + } + else if (kevent.KeyCode == Keys.LWin || kevent.KeyCode == Keys.RWin) + { + HotkeyInfo.Win = !HotkeyInfo.Win; + UpdateHotkeyText(); + } + else if (new HotkeyInfo(kevent.KeyData).IsValidHotkey) + { + HotkeyInfo.Hotkey = kevent.KeyData; + StopEditing(); + } + else + { + HotkeyInfo.Hotkey = kevent.KeyData; + UpdateHotkeyText(); + } + } + + base.OnKeyDown(kevent); + } + + protected override void OnKeyUp(KeyEventArgs kevent) + { + kevent.SuppressKeyPress = true; + + if (EditingHotkey) + { + // PrintScreen not trigger KeyDown event + if (kevent.KeyCode == Keys.PrintScreen) + { + HotkeyInfo.Hotkey = kevent.KeyData; + StopEditing(); + } + } + + base.OnKeyUp(kevent); + } + + protected void OnHotkeyChanged() + { + if (HotkeyChanged != null) + { + HotkeyChanged(this, EventArgs.Empty); + } + } + } +} \ No newline at end of file diff --git a/ShareX.HelpersLib/Enums.cs b/ShareX.HelpersLib/Enums.cs index 0a76cdef3..b5cad8dd5 100644 --- a/ShareX.HelpersLib/Enums.cs +++ b/ShareX.HelpersLib/Enums.cs @@ -160,4 +160,11 @@ public enum ScreenTearingTestMode VerticalLines, HorizontalLines } + + public enum HotkeyStatus + { + Registered, + Failed, + NotConfigured + } } \ No newline at end of file diff --git a/ShareX/HotkeyInfo.cs b/ShareX.HelpersLib/Input/HotkeyInfo.cs similarity index 99% rename from ShareX/HotkeyInfo.cs rename to ShareX.HelpersLib/Input/HotkeyInfo.cs index 97f693d21..58d1aee43 100644 --- a/ShareX/HotkeyInfo.cs +++ b/ShareX.HelpersLib/Input/HotkeyInfo.cs @@ -24,11 +24,10 @@ #endregion License Information (GPL v3) using Newtonsoft.Json; -using ShareX.HelpersLib; using System.Text; using System.Windows.Forms; -namespace ShareX +namespace ShareX.HelpersLib { public class HotkeyInfo { diff --git a/ShareX.HelpersLib/ShareX.HelpersLib.csproj b/ShareX.HelpersLib/ShareX.HelpersLib.csproj index 3dad9c326..021c56f18 100644 --- a/ShareX.HelpersLib/ShareX.HelpersLib.csproj +++ b/ShareX.HelpersLib/ShareX.HelpersLib.csproj @@ -81,9 +81,13 @@ + + Component + Component + diff --git a/ShareX/Controls/HotkeySelectionControl.cs b/ShareX/Controls/HotkeySelectionControl.cs index 272d46123..12f2028e1 100644 --- a/ShareX/Controls/HotkeySelectionControl.cs +++ b/ShareX/Controls/HotkeySelectionControl.cs @@ -23,6 +23,7 @@ #endregion License Information (GPL v3) +using ShareX.HelpersLib; using ShareX.Properties; using System; using System.Drawing; diff --git a/ShareX/Enums.cs b/ShareX/Enums.cs index fe0d738e8..ccf2aea1b 100644 --- a/ShareX/Enums.cs +++ b/ShareX/Enums.cs @@ -202,13 +202,6 @@ public enum HotkeyType // Localized + Category OpenImageHistory } - public enum HotkeyStatus - { - Registered, - Failed, - NotConfigured - } - public enum PopUpNotificationType // Localized { None, diff --git a/ShareX/HotkeySettings.cs b/ShareX/HotkeySettings.cs index b298d107b..6377ba255 100644 --- a/ShareX/HotkeySettings.cs +++ b/ShareX/HotkeySettings.cs @@ -23,6 +23,7 @@ #endregion License Information (GPL v3) +using ShareX.HelpersLib; using System.Windows.Forms; namespace ShareX diff --git a/ShareX/ShareX.csproj b/ShareX/ShareX.csproj index 0ee6e1355..2f088041b 100644 --- a/ShareX/ShareX.csproj +++ b/ShareX/ShareX.csproj @@ -244,7 +244,6 @@ Form - UserControl