fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5.  
  6. public class KeySequence :IEquatable<KeySequence>
  7. {
  8. public KeySequence(Keys first, Keys second)
  9. {
  10. this.First = first; this.Second = second;
  11. }
  12. public Keys First { get; private set; }
  13. public Keys Second { get; private set; }
  14.  
  15. public bool Equals(KeySequence other)
  16. {
  17. return First == other.First && Second == other.Second;
  18. }
  19.  
  20. public override bool Equals(object obj)
  21. {
  22. return (obj as KeySequence)?.Equals(this) ?? false;
  23. }
  24. public static bool operator ==(KeySequence a, KeySequence b)
  25. {
  26. return a.Equals(b);
  27. }
  28.  
  29. public static bool operator !=(KeySequence a, KeySequence b)
  30. {
  31. return !(a == b);
  32. }
  33. }
  34.  
  35. public class KeyBinding
  36. {
  37. public KeyBinding(Keys first, Keys second, Action action)
  38. {
  39. KeySequence = new KeySequence(first, second);
  40. this.Action = action;
  41. }
  42. public KeySequence KeySequence { get; private set; }
  43. public Action Action { get; private set; }
  44. }
  45.  
  46. class KeyboardShortcutsDetector
  47. {
  48. private List<KeyBinding> mKeyBindings = new List<KeyBinding>();
  49.  
  50. public bool IsWaitingFor2ndKey { get; private set; }
  51. public Keys FirstKey { get; private set; }
  52.  
  53. public void AddKeyBinding(Keys first, Keys second, Action action)
  54. {
  55. mKeyBindings.Add(new KeyBinding(first, second, action));
  56. }
  57.  
  58. public void RemoveKeyBinding(Keys first, Keys second)
  59. {
  60. var ks = new KeySequence(first, second);
  61. var item = mKeyBindings.Find(x => x.KeySequence == ks);
  62. mKeyBindings.Remove(item);
  63. }
  64.  
  65. public bool ProcessKey(Keys keyData)
  66. {
  67. return IsWaitingFor2ndKey ? Process2ndKey(keyData) : Process1stsKey(keyData);
  68. }
  69.  
  70. private bool Process1stsKey(Keys keyData)
  71. {
  72. if(mKeyBindings.Any(x => x.KeySequence.First == keyData))
  73. {
  74. FirstKey = keyData;
  75. IsWaitingFor2ndKey = true;
  76. return true;
  77. }
  78. return false;
  79. }
  80.  
  81. private bool Process2ndKey(Keys keyData)
  82. {
  83. IsWaitingFor2ndKey = false;
  84. var ks = new KeySequence(FirstKey, keyData);
  85. var keyBinding = mKeyBindings.Find(x => x.KeySequence == ks);
  86. keyBinding?.Action();
  87. return keyBinding != null;
  88. }
  89. }
  90.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
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
stdout
Standard output is empty