misu – Rev 1

Subversion Repositories:
Rev:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Configuration;
using Gma.System.MouseKeyHook;

namespace misu
{
    public partial class BindKeyForm : Form
    {
        #region Public Events & Delegates

        public event EventHandler<KeyBoundEventArgs> KeyBound;

        #endregion

        #region Public Enums, Properties and Fields

        public Settings Settings { get; set; }

        #endregion

        #region Private Delegates, Events, Enums, Properties, Indexers and Fields

        private IKeyboardMouseEvents GlobalHook { get; }

        private List<Keys> BoundKeys { get; } = new List<Keys>();

        #endregion

        #region Constructors, Destructors and Finalizers

        public BindKeyForm()
        {
            InitializeComponent();
        }

        public BindKeyForm(IKeyboardMouseEvents globalHook) : this()
        {
            GlobalHook = globalHook;
        }

        public BindKeyForm(IKeyboardMouseEvents globalHook, Settings settings) : this(globalHook)
        {
            Settings = settings;
        }

        #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;

            var distinct = new List<Keys>(BoundKeys.Distinct());

            Settings.Binding.Keys = distinct;

            this.Execute(() => { textBox1.Text = Settings.Binding.ToString(); });

            KeyBound?.Invoke(this, new KeyBoundEventArgs(Settings.Binding));

            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)
        {
            textBox1.Text = Settings.Binding.ToString();
        }

        #endregion
    }
}

Generated by GNU Enscript 1.6.5.90.