using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; public class KeySequence :IEquatable<KeySequence> { public KeySequence(Keys first, Keys second) { this.First = first; this.Second = second; } public Keys First { get; private set; } public Keys Second { get; private set; } public bool Equals(KeySequence other) { return First == other.First && Second == other.Second; } public override bool Equals(object obj) { return (obj as KeySequence)?.Equals(this) ?? false; } public static bool operator ==(KeySequence a, KeySequence b) { return a.Equals(b); } public static bool operator !=(KeySequence a, KeySequence b) { return !(a == b); } } public class KeyBinding { public KeyBinding(Keys first, Keys second, Action action) { KeySequence = new KeySequence(first, second); this.Action = action; } public KeySequence KeySequence { get; private set; } public Action Action { get; private set; } } class KeyboardShortcutsDetector { private List<KeyBinding> mKeyBindings = new List<KeyBinding>(); public bool IsWaitingFor2ndKey { get; private set; } public Keys FirstKey { get; private set; } public void AddKeyBinding(Keys first, Keys second, Action action) { mKeyBindings.Add(new KeyBinding(first, second, action)); } public void RemoveKeyBinding(Keys first, Keys second) { var ks = new KeySequence(first, second); var item = mKeyBindings.Find(x => x.KeySequence == ks); mKeyBindings.Remove(item); } public bool ProcessKey(Keys keyData) { return IsWaitingFor2ndKey ? Process2ndKey(keyData) : Process1stsKey(keyData); } private bool Process1stsKey(Keys keyData) { if(mKeyBindings.Any(x => x.KeySequence.First == keyData)) { FirstKey = keyData; IsWaitingFor2ndKey = true; return true; } return false; } private bool Process2ndKey(Keys keyData) { IsWaitingFor2ndKey = false; var ks = new KeySequence(FirstKey, keyData); var keyBinding = mKeyBindings.Find(x => x.KeySequence == ks); keyBinding?.Action(); return keyBinding != null; } }
Standard input is empty
prog.cs(4,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference? prog.cs(8,24): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(8,36): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(12,12): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(13,12): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(6,14): warning CS0661: `KeySequence' defines operator == or operator != but does not override Object.GetHashCode() prog.cs(6,14): warning CS0659: `KeySequence' overrides Object.Equals(object) but does not override Object.GetHashCode() prog.cs(37,23): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(37,35): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(51,12): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(53,31): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(53,43): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(58,34): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(58,46): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(65,28): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(70,33): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? prog.cs(81,32): error CS0246: The type or namespace name `Keys' could not be found. Are you missing an assembly reference? Compilation failed: 15 error(s), 2 warnings
Standard output is empty