using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Drawing; using System.Linq; using System.Windows.Forms; using Gma.System.MouseKeyHook; using misu.Properties; namespace misu { public partial class BindKeyForm : Form { #region Public Events & Delegates public event EventHandler KeyBound; #endregion #region Private Delegates, Events, Enums, Properties, Indexers and Fields private IKeyboardMouseEvents GlobalHook { get; } private List BoundKeys { get; } = new List(); #endregion #region Constructors, Destructors and Finalizers public BindKeyForm() { InitializeComponent(); } public BindKeyForm(IKeyboardMouseEvents globalHook) : this() { GlobalHook = globalHook; } #endregion #region Event Handlers private void GlobalHook_KeyUp(object sender, KeyEventArgs e) { GlobalHook.KeyDown -= GlobalHook_KeyDown; GlobalHook.KeyUp -= GlobalHook_KeyUp; button1.BackColor = SystemColors.Control; e.Handled = true; e.SuppressKeyPress = true; Settings.Default.Keys = new StringCollection(); foreach (var key in BoundKeys.Distinct()) { Settings.Default.Keys.Add(key.ToString()); } this.Execute(() => { textBox1.Text = string.Join(" ", Settings.Default.Keys.OfType()); }); KeyBound?.Invoke(this, new KeyBoundEventArgs()); BoundKeys.Clear(); } private void GlobalHook_KeyDown(object sender, KeyEventArgs e) { e.SuppressKeyPress = true; BoundKeys.Add(e.KeyCode); } private void OnFormClosing(object sender, FormClosingEventArgs e) { GlobalHook.KeyDown -= GlobalHook_KeyDown; GlobalHook.KeyUp -= GlobalHook_KeyUp; } private void RecButtonOnClick(object sender, EventArgs e) { button1.BackColor = Color.Red; GlobalHook.KeyDown += GlobalHook_KeyDown; GlobalHook.KeyUp += GlobalHook_KeyUp; } private void OnFormLoad(object sender, EventArgs e) { if (Settings.Default.Keys != null) { textBox1.Text = string.Join(" ", Settings.Default.Keys.OfType()); } } #endregion } }