diff --git a/DisplayMagician/ShortcutItem.cs b/DisplayMagician/ShortcutItem.cs index eb6869e..4c04daf 100644 --- a/DisplayMagician/ShortcutItem.cs +++ b/DisplayMagician/ShortcutItem.cs @@ -15,7 +15,7 @@ using System.Text.RegularExpressions; using IWshRuntimeLibrary; using AudioSwitcher.AudioApi.CoreAudio; using AudioSwitcher.AudioApi; -using WK.Libraries.HotkeyListenerNS; +//using WK.Libraries.HotkeyListenerNS; namespace DisplayMagician { @@ -111,7 +111,7 @@ namespace DisplayMagician private ShortcutPermanence _displayPermanence = ShortcutPermanence.Temporary; private ShortcutPermanence _audioPermanence = ShortcutPermanence.Temporary; private ShortcutPermanence _capturePermanence = ShortcutPermanence.Temporary; - private Hotkey _hotkey = null; + private Keys _hotkey = Keys.None; private bool _autoName = true; private ShortcutValidity _isValid; private List _shortcutErrors = new List(); @@ -967,7 +967,7 @@ namespace DisplayMagician } #pragma warning disable CS3003 // Type is not CLS-compliant - public Hotkey Hotkey + public Keys Hotkey #pragma warning restore CS3003 // Type is not CLS-compliant { get @@ -976,8 +976,7 @@ namespace DisplayMagician } set { - if (value is Hotkey) - _hotkey = value; + _hotkey = value; } } diff --git a/DisplayMagician/UIForms/DisplayProfileForm.cs b/DisplayMagician/UIForms/DisplayProfileForm.cs index 73ab095..8caa9ff 100644 --- a/DisplayMagician/UIForms/DisplayProfileForm.cs +++ b/DisplayMagician/UIForms/DisplayProfileForm.cs @@ -7,7 +7,7 @@ using DisplayMagicianShared; using Manina.Windows.Forms; using System.Drawing; using System.Collections.Generic; -using WK.Libraries.HotkeyListenerNS; +//using WK.Libraries.HotkeyListenerNS; namespace DisplayMagician.UIForms { @@ -467,11 +467,11 @@ namespace DisplayMagician.UIForms private void btn_hotkey_Click(object sender, EventArgs e) { - Hotkey testHotkey = null; - if (_selectedProfile.Hotkey is Hotkey) + Keys testHotkey; + if (_selectedProfile.Hotkey != Keys.None) testHotkey = _selectedProfile.Hotkey; else - testHotkey = new Hotkey(); + testHotkey = Keys.None; string hotkeyHeading = $"Choose a '{_selectedProfile.Name}' Display Profile Hotkey"; string hotkeyDescription = $"Choose a Hotkey (a keyboard shortcut) so that you can apply to this" + Environment.NewLine + "screen using your keyboard. This must be a Hotkey that" + Environment.NewLine + @@ -496,12 +496,14 @@ namespace DisplayMagician.UIForms btn_hotkey.PerformClick(); } - private void UpdateHotkeyLabel (Hotkey myHotkey) + private void UpdateHotkeyLabel (Keys myHotkey) { // And if we get back and this is a Hotkey with a value, we need to show that in the UI - if (myHotkey is Hotkey && !(myHotkey.KeyCode == Keys.None && myHotkey.Modifiers == Keys.None)) + if (myHotkey != Keys.None) { - lbl_hotkey_assigned.Text = "Hotkey: " + HotkeyListener.Convert(myHotkey); + KeysConverter kc = new KeysConverter(); + + lbl_hotkey_assigned.Text = "Hotkey: " + kc.ConvertToString(myHotkey); lbl_hotkey_assigned.Visible = true; } else diff --git a/DisplayMagician/UIForms/HotkeyForm.cs b/DisplayMagician/UIForms/HotkeyForm.cs index 9b8878e..c971a37 100644 --- a/DisplayMagician/UIForms/HotkeyForm.cs +++ b/DisplayMagician/UIForms/HotkeyForm.cs @@ -7,29 +7,35 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; -using WK.Libraries.HotkeyListenerNS; +//using WK.Libraries.HotkeyListenerNS; +using NHotkey.WindowsForms; namespace DisplayMagician.UIForms { public partial class HotkeyForm : Form { //HotkeyListener myHotkeyListener = null; - HotkeySelector hks; - Hotkey myHotkey = null; + //HotkeySelector hks; + Keys myHotkey = Keys.None; + string emptyHotkeyText = ""; + string invalidHotkeyText = ""; + List _needNonShiftModifier = new List() { }; + List _needNonAltGrModifier = new List() { }; + #pragma warning disable CS3003 // Type is not CLS-compliant - public Hotkey Hotkey + public Keys Hotkey #pragma warning restore CS3003 // Type is not CLS-compliant { - get - { + get; + /*{ return myHotkey; - } - set - { - if (value is Hotkey) + }*/ + set; + /*{ + if (value is Keys) myHotkey = value; - } + }*/ } @@ -37,31 +43,21 @@ namespace DisplayMagician.UIForms public HotkeyForm() { InitializeComponent(); - - myHotkey = new Hotkey(); - - hks = new HotkeySelector(); - hks.EmptyHotkeyText = ""; - hks.Enable(txt_hotkey); + + //hks = new HotkeySelector(); + //hks.EmptyHotkeyText = ""; + //hks.Enable(txt_hotkey); this.ActiveControl = txt_hotkey; } #pragma warning disable CS3001 // Argument type is not CLS-compliant - public HotkeyForm(Hotkey hotkeyToEdit = null, string hotkeyHeading = "", string hotkeyDescription = "") + public HotkeyForm(Keys hotkeyToEdit = Keys.None, string hotkeyHeading = "", string hotkeyDescription = "") #pragma warning restore CS3001 // Argument type is not CLS-compliant { InitializeComponent(); - //myHotkeyListener = hotkeyListener; + myHotkey = hotkeyToEdit; - if (hotkeyToEdit == null) - myHotkey = new Hotkey(); - else - myHotkey = hotkeyToEdit; - - hks = new HotkeySelector(); - hks.EmptyHotkeyText = ""; - hks.Enable(txt_hotkey, hotkeyToEdit); this.ActiveControl = txt_hotkey; if (!String.IsNullOrEmpty(hotkeyHeading)) @@ -88,34 +84,23 @@ namespace DisplayMagician.UIForms private void btn_clear_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.None; - hks.Clear(txt_hotkey); - myHotkey.KeyCode = Keys.None; - myHotkey.Modifiers = Keys.None; + txt_hotkey.Text = ""; + myHotkey = Keys.None; this.ActiveControl = txt_hotkey; } private void btn_save_Click(object sender, EventArgs e) { - if (!String.IsNullOrWhiteSpace(txt_hotkey.Text)) +/* if (!String.IsNullOrWhiteSpace(txt_hotkey.Text)) { - Program.HotkeyListener.Update - ( - // Reference the current clipping hotkey for directly updating - // the hotkey without a need for restarting your application. - ref myHotkey, - - // Convert the selected hotkey's text representation - // to a Hotkey object and update it. - HotkeyListener.Convert(txt_hotkey.Text) - ); + } else { - myHotkey.KeyCode = Keys.None; - myHotkey.Modifiers = Keys.None; + myHotkey = Keys.None; } - +*/ this.Hotkey = myHotkey; this.DialogResult = DialogResult.OK; this.Close(); @@ -126,5 +111,185 @@ namespace DisplayMagician.UIForms this.DialogResult = DialogResult.Cancel; this.Close(); } + + + /// + /// Fires when a key is pressed down. Here, we'll want to update the Text + /// property to notify the user what key combination is currently pressed. + /// + private void OnKeyDown(object sender, KeyEventArgs e) + { + if (e.KeyData == Keys.Delete || e.KeyData == (Keys.Control | Keys.Delete)) + { + myHotkey = Keys.None; + Refresh(txt_hotkey, false); + } + + + if (e.KeyData == (Keys.Shift | Keys.Insert)) + { + myHotkey = Keys.Shift; + + e.Handled = true; + } + + // Clear the current hotkey. + if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) + { + myHotkey = Keys.None; + Refresh(txt_hotkey, false); + + return; + } + else + { + myHotkey = e.KeyCode; + + Refresh(txt_hotkey, false); + } + } + + /// + /// Fires when all keys are released. If the current hotkey isn't valid, reset it. + /// Otherwise, do nothing and keep the Text and hotkey as it was. + /// + private void OnKeyUp(object sender, KeyEventArgs e) + { + if (myHotkey == Keys.None && Control.ModifierKeys == Keys.None) + { + myHotkey = Keys.None; + Refresh(txt_hotkey, false); + + return; + } + } + + /// + /// Prevents anything entered in Input controls from being displayed. + /// Without this, a "A" key press would appear as "aControl, Alt + A". + /// + private void OnKeyPress(object sender, KeyPressEventArgs e) + { + e.Handled = true; + } + + private void Refresh(Control control, bool internalCall) + { + try + { + string parsedHotkey = string.Empty; + + // No hotkey set. + if (myHotkey == Keys.None) + { + txt_hotkey.Text = emptyHotkeyText; + + return; + } + + // LWin/RWin don't work as hotkeys... + // (neither do they work as modifier keys in .NET 2.0). + if (myHotkey == Keys.LWin || myHotkey == Keys.RWin) + { + txt_hotkey.Text = invalidHotkeyText; + + return; + } + + // Only validate input if it comes from the user. + if (internalCall == false) + { + // No modifier or shift only, and a hotkey that needs another modifier. + if ((myHotkey == Keys.Shift || myHotkey == Keys.None) && + this._needNonShiftModifier.Contains((int)myHotkey)) + { + if (this.myHotkey == Keys.None) + { + // Set Ctrl+Alt as the modifier unless Ctrl+Alt+ won't work. + if (_needNonAltGrModifier.Contains((int)myHotkey) == false) + { + this.myHotkey |= Keys.Alt | Keys.Control; + } + else + { + // ...In that case, use Shift+Alt instead. + this.myHotkey |= Keys.Alt | Keys.Shift; + } + } + else + { + // User pressed Shift and an invalid key (e.g. a letter or a number), + // that needs another set of modifier keys. + this.myHotkey = Keys.None; + + txt_hotkey.Text = this.myHotkey.ToString() + $" + {invalidHotkeyText}"; + + return; + } + } + } + + // Without this code, pressing only Ctrl + // will show up as "Control + ControlKey", etc. + if (this.myHotkey == Keys.Menu || /* Alt */ + this.myHotkey == Keys.ShiftKey || + this.myHotkey == Keys.ControlKey) + { + this.myHotkey = Keys.None; + } + + // A final compilation of the processed keys in string format. + parsedHotkey = this.myHotkey.ToString(); + + txt_hotkey.Text = parsedHotkey; + + return; + } + catch (Exception) { } + } + + private void GenerateInvalidModifiers() + { + // Fill the ArrayLists that contain + // all invalid hotkey combinations. + _needNonShiftModifier = new List() { }; + _needNonAltGrModifier = new List() { }; + + // Shift + 0 - 9, A - Z. + for (Keys k = Keys.D0; k <= Keys.Z; k++) + _needNonShiftModifier.Add((int)k); + + // Shift + Numpad keys. + for (Keys k = Keys.NumPad0; k <= Keys.NumPad9; k++) + _needNonShiftModifier.Add((int)k); + + // Shift + Misc (,;<./ etc). + for (Keys k = Keys.Oem1; k <= Keys.OemBackslash; k++) + _needNonShiftModifier.Add((int)k); + + // Shift + Space, PgUp, PgDn, End, Home. + for (Keys k = Keys.Space; k <= Keys.Home; k++) + _needNonShiftModifier.Add((int)k); + + // Misc keys that we can't loop through. + _needNonShiftModifier.Add((int)Keys.Insert); + _needNonShiftModifier.Add((int)Keys.Help); + _needNonShiftModifier.Add((int)Keys.Multiply); + _needNonShiftModifier.Add((int)Keys.Add); + _needNonShiftModifier.Add((int)Keys.Subtract); + _needNonShiftModifier.Add((int)Keys.Divide); + _needNonShiftModifier.Add((int)Keys.Decimal); + _needNonShiftModifier.Add((int)Keys.Return); + _needNonShiftModifier.Add((int)Keys.Escape); + _needNonShiftModifier.Add((int)Keys.NumLock); + _needNonShiftModifier.Add((int)Keys.Scroll); + _needNonShiftModifier.Add((int)Keys.Pause); + + // Ctrl+Alt + 0 - 9. + for (Keys k = Keys.D0; k <= Keys.D9; k++) + _needNonAltGrModifier.Add((int)k); + } + + } } diff --git a/DisplayMagician/UIForms/MainForm.cs b/DisplayMagician/UIForms/MainForm.cs index f4d4173..309ebf0 100644 --- a/DisplayMagician/UIForms/MainForm.cs +++ b/DisplayMagician/UIForms/MainForm.cs @@ -10,8 +10,10 @@ using Newtonsoft.Json; using System.Net; using Windows.Data.Xml.Dom; using Microsoft.Toolkit.Uwp.Notifications; -using WK.Libraries.HotkeyListenerNS; +//using WK.Libraries.HotkeyListenerNS; using System.Collections.Generic; +using NHotkey.WindowsForms; +using NHotkey; namespace DisplayMagician.UIForms { @@ -20,12 +22,12 @@ namespace DisplayMagician.UIForms private bool allowVisible; // ContextMenu's Show command used private bool allowClose; // ContextMenu's Exit command used - private HotkeyListener hotkeyListener = new HotkeyListener(); + /*private HotkeyListener hotkeyListener = new HotkeyListener(); private Hotkey hotkeyMainWindow; private Hotkey hotkeyShortcutLibraryWindow; - private Hotkey hotkeyDisplayProfileWindow; - private Dictionary hotkeyDisplayProfiles = new Dictionary() { }; - private Dictionary hotkeyShortcuts = new Dictionary() { }; + private Hotkey hotkeyDisplayProfileWindow;*/ + private List hotkeyDisplayProfiles = new List() { }; + private List hotkeyShortcuts = new List() { }; private static readonly NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger(); @@ -39,39 +41,34 @@ namespace DisplayMagician.UIForms notifyIcon.ContextMenuStrip = mainContextMenuStrip; RefreshNotifyIconMenus(); - - // Define a new hotkey using the Hotkey class. - // Parameters are: [modifiers], [keys]. - //hotkeyMainWindow = new Hotkey(Keys.Control | Keys.Shift, Keys.W); - //Program.HotkeyListener.Add(hotkeyMainWindow); - /*if (Program.AppProgramSettings.MainWindowHotkey) - hotkeyMainWindow = new Hotkey(Program.AppProgramSettings.MainWindowHotkey); + HotkeyManager.Current.AddOrReplace("MainWindowHotkey", Program.AppProgramSettings.MainWindowHotkey, OnWindowHotkeyPressed); if (Program.AppProgramSettings.ShortcutLibraryWindow) - hotkeyShortcutLibraryWindow = new Hotkey(Program.AppProgramSettings.ShortcutLibraryWindow); + HotkeyManager.Current.AddOrReplace("ShortcutLibraryWindow", Program.AppProgramSettings.ShortcutLibraryWindow, OnWindowHotkeyPressed); if (Program.AppProgramSettings.DisplayProfileWindow) - hotkeyDisplayProfileWindow = new Hotkey(Program.AppProgramSettings.DisplayProfileWindow);*/ + HotkeyManager.Current.AddOrReplace("DisplayProfileWindow", Program.AppProgramSettings.DisplayProfileWindow, OnWindowHotkeyPressed);*/ // Add all the Profile Hotkeys that are set foreach (ProfileItem myProfile in ProfileRepository.AllProfiles) { - if (myProfile.Hotkey is Hotkey && myProfile.Hotkey.Modifiers != Keys.None && myProfile.Hotkey.KeyCode != Keys.None) + if (myProfile.Hotkey != Keys.None) { - hotkeyDisplayProfiles.Add(myProfile.Hotkey, myProfile.UUID); + hotkeyDisplayProfiles.Add(myProfile.UUID); + HotkeyManager.Current.AddOrReplace(myProfile.UUID, myProfile.Hotkey, OnWindowHotkeyPressed); } } // Add all the Shortcut Hotkeys that are set foreach (ShortcutItem myShortcut in ShortcutRepository.AllShortcuts) { - if (myShortcut.Hotkey is Hotkey && myShortcut.Hotkey.Modifiers != Keys.None && myShortcut.Hotkey.KeyCode != Keys.None) + if (myShortcut.Hotkey != Keys.None) { - hotkeyShortcuts.Add(myShortcut.Hotkey, myShortcut.UUID); + hotkeyShortcuts.Add(myShortcut.UUID); + HotkeyManager.Current.AddOrReplace(myShortcut.UUID, myShortcut.Hotkey, OnWindowHotkeyPressed); } } // And now connect up our processing function - Program.HotkeyListener.HotkeyPressed += Hkl_WindowHotkeyPressed; - + if (Program.AppProgramSettings.MinimiseOnStart) @@ -536,28 +533,29 @@ namespace DisplayMagician.UIForms openApplicationWindow(); } - private void Hkl_WindowHotkeyPressed(object sender, HotkeyEventArgs e) + private void OnWindowHotkeyPressed(object sender, HotkeyEventArgs e) { - if (e.Hotkey == hotkeyMainWindow) + if (e.Name == "HotkeyMainWindow") openApplicationWindow(); - else if (e.Hotkey == hotkeyDisplayProfileWindow) + else if (e.Name == "HotkeyDisplayProfileWindow") btn_setup_display_profiles.PerformClick(); - else if (e.Hotkey == hotkeyDisplayProfileWindow) + else if (e.Name == "ShortcutLibraryWindow") btn_setup_game_shortcuts.PerformClick(); - else if (hotkeyDisplayProfiles.ContainsKey(e.Hotkey)) + else if (hotkeyDisplayProfiles.Contains(e.Name)) { - string displayProfileUUID = hotkeyDisplayProfiles[e.Hotkey]; + string displayProfileUUID = e.Name; ProfileItem chosenProfile = ProfileRepository.GetProfile(displayProfileUUID); if (chosenProfile is ProfileItem) Program.ApplyProfile(chosenProfile); } - else if (hotkeyShortcuts.ContainsKey(e.Hotkey)) + else if (hotkeyShortcuts.Contains(e.Name)) { - string shortcutUUID = hotkeyShortcuts[e.Hotkey]; + string shortcutUUID = e.Name; ShortcutItem chosenShortcut = ShortcutRepository.GetShortcut(shortcutUUID); if (chosenShortcut is ShortcutItem) ShortcutRepository.RunShortcut(chosenShortcut); } } + } } diff --git a/DisplayMagician/UIForms/ShortcutForm.cs b/DisplayMagician/UIForms/ShortcutForm.cs index 356bafa..f8b3e3b 100644 --- a/DisplayMagician/UIForms/ShortcutForm.cs +++ b/DisplayMagician/UIForms/ShortcutForm.cs @@ -15,7 +15,7 @@ using Manina.Windows.Forms; using System.Windows.Forms.VisualStyles; using AudioSwitcher.AudioApi.CoreAudio; using AudioSwitcher.AudioApi; -using WK.Libraries.HotkeyListenerNS; +//using WK.Libraries.HotkeyListenerNS; namespace DisplayMagician.UIForms { @@ -2230,11 +2230,11 @@ namespace DisplayMagician.UIForms private void btn_hotkey_Click(object sender, EventArgs e) { - Hotkey testHotkey = null; - if (_shortcutToEdit.Hotkey is Hotkey) + Keys testHotkey; + if (_shortcutToEdit.Hotkey != Keys.None) testHotkey = _shortcutToEdit.Hotkey; else - testHotkey = new Hotkey(); + testHotkey = Keys.None; string hotkeyHeading = $"Choose a '{_shortcutToEdit.Name}' Shortcut Hotkey"; string hotkeyDescription = $"Choose a Hotkey (a keyboard shortcut) so that you can start this" + Environment.NewLine + "game shortcut using your keyboard. This must be a Hotkey that" + Environment.NewLine + @@ -2257,12 +2257,14 @@ namespace DisplayMagician.UIForms btn_hotkey.PerformClick(); } - private void UpdateHotkeyLabel(Hotkey myHotkey) + private void UpdateHotkeyLabel(Keys myHotkey) { // And if we get back and this is a Hotkey with a value, we need to show that in the UI - if (myHotkey is Hotkey && !(myHotkey.KeyCode == Keys.None && myHotkey.Modifiers == Keys.None)) + if (myHotkey != Keys.None) { - lbl_hotkey_assigned.Text = "Hotkey: " + HotkeyListener.Convert(myHotkey); + KeysConverter kc = new KeysConverter(); + + lbl_hotkey_assigned.Text = "Hotkey: " + kc.ConvertToString(myHotkey); lbl_hotkey_assigned.Visible = true; } else diff --git a/DisplayMagicianShared/ProfileItem.cs b/DisplayMagicianShared/ProfileItem.cs index 8f2ee6f..c0872f2 100644 --- a/DisplayMagicianShared/ProfileItem.cs +++ b/DisplayMagicianShared/ProfileItem.cs @@ -9,7 +9,7 @@ using System.Drawing; using System.Drawing.Imaging; using System.Text.RegularExpressions; using IWshRuntimeLibrary; -using WK.Libraries.HotkeyListenerNS; +//using WK.Libraries.HotkeyListenerNS; namespace DisplayMagicianShared { @@ -25,7 +25,7 @@ namespace DisplayMagicianShared private string _uuid = ""; private bool _isPossible = false; - private Hotkey _hotkey = null; + private Keys _hotkey = Keys.None; #region JsonConverterBitmap @@ -152,15 +152,14 @@ namespace DisplayMagicianShared } } - public Hotkey Hotkey { + public Keys Hotkey { get { return _hotkey; } set { - if (value is Hotkey) - _hotkey = value; + _hotkey = value; } }