using System; using System.Collections.Generic; using System.Windows.Forms; namespace NumpadMonitor { internal partial class KeyConfig : Form { private readonly List keyHandlers = new List(); private KeyHandler askedKey; private readonly bool AlternativeKeySet; public KeyConfig(bool alternativeKeyset) { InitializeComponent(); AlternativeKeySet = alternativeKeyset; KeyInfo[] keyset = alternativeKeyset ? KeySet.Alternative : KeySet.Numpad; foreach (KeyInfo key in keyset) AddKeyHandler(key); AskNextKey(); } private void AddKeyHandler(KeyInfo key) { KeyHandler keyHandler = new KeyHandler(key, this); keyHandler.Register(); keyHandlers.Add(keyHandler); } private void AskNextKey() { KeyHandler keyHandler = keyHandlers.Find(x => x.KeyInfo.KeyCode == IntPtr.Zero); if (keyHandler == null) { KeyCodeReaderWriter.Write(keyHandlers); DisposeElements(); Close(); return; } AskKey(keyHandler); } private void AskKey(KeyHandler keyHandler) { askedKey = keyHandler; label_key.Text = AlternativeKeySet ? KeySet.AlternativeToNumpad(keyHandler.KeyInfo).ToString() : keyHandler.KeyInfo.ToString(); } private void HandleHotkey(IntPtr LParam) { label_keyCode.Text = LParam.ToInt64().ToString(); askedKey.SetKeyCode(LParam); AskNextKey(); } protected override void WndProc(ref Message m) { if (m.Msg == Constants.WM_HOTKEY_MSG_ID) HandleHotkey(m.LParam); base.WndProc(ref m); } public void DisposeElements() { keyHandlers.ForEach(x => x.Unregiser()); } } }