PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Classes/KeyManager.cs

http://github.com/Concliff/Maze
C# | 136 lines | 77 code | 16 blank | 43 comment | 1 complexity | 84689866832bc3cb2724105615d08a9b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Collections;
  7. namespace Maze.Classes
  8. {
  9. public class KeyManager
  10. {
  11. /// <summary>
  12. /// Arguments of the last KeyDown event.
  13. /// </summary>
  14. private KeyEventArgs lastKeyEventArgs;
  15. /// <summary>
  16. /// Collection of keys that are holding down at the moment.
  17. /// </summary>
  18. private List<Keys> keysDownList;
  19. /// <summary>
  20. /// Initializes a new instance of the KeyManager class.
  21. /// </summary>
  22. public KeyManager()
  23. {
  24. KeyPressed = Keys.None;
  25. keysDownList = new List<Keys>();
  26. lastKeyEventArgs = new KeyEventArgs(Keys.None);
  27. }
  28. /// <summary>
  29. /// Gets the count of keys that are holding down at this moment.
  30. /// </summary>
  31. public int KeysDownCount
  32. {
  33. get
  34. {
  35. return keysDownList.Count;
  36. }
  37. }
  38. /// <summary>
  39. /// Gets or sets the last pressed key.
  40. /// </summary>
  41. public Keys KeyPressed { get; private set; }
  42. /// <summary>
  43. /// Gets a value indicating whether the CTRL key was pressed.
  44. /// </summary>
  45. public bool Control
  46. {
  47. get
  48. {
  49. return lastKeyEventArgs.Control;
  50. }
  51. }
  52. /// <summary>
  53. /// Gets a value indicating whether the ALT key was pressed.
  54. /// </summary>
  55. public bool Alt
  56. {
  57. get
  58. {
  59. return lastKeyEventArgs.Alt;
  60. }
  61. }
  62. /// <summary>
  63. /// Gets a value indicating whether the SHIFT key was pressed.
  64. /// </summary>
  65. public bool Shift
  66. {
  67. get
  68. {
  69. return lastKeyEventArgs.Shift;
  70. }
  71. }
  72. /// <summary>
  73. /// Occurs when a key is pressed
  74. /// </summary>
  75. public void EventKeyDown(object sender, KeyEventArgs e)
  76. {
  77. KeyPressed = e.KeyCode;
  78. lastKeyEventArgs = e;
  79. // Do not double add the same key
  80. if (!keysDownList.Contains(e.KeyCode))
  81. keysDownList.Add(e.KeyCode);
  82. }
  83. /// <summary>
  84. /// Occurs when a key is released
  85. /// </summary>
  86. public void EventKeyUp(object sender, KeyEventArgs e)
  87. {
  88. keysDownList.Remove(e.KeyCode);
  89. }
  90. /// <summary>
  91. /// Occurs when a key is pressed
  92. /// </summary>
  93. public void EventKeyPress(object sender, KeyPressEventArgs e)
  94. {
  95. // TODO: remove or something else
  96. }
  97. public Keys KeyDown(int Number) { return (Keys)keysDownList[Number]; }
  98. /// <summary>
  99. /// Gets the last pressed key and clear the record about that.
  100. /// </summary>
  101. public Keys ExtractKeyPressed()
  102. {
  103. Keys KeyToReturn;
  104. KeyToReturn = KeyPressed;
  105. KeyPressed = Keys.None;
  106. return KeyToReturn;
  107. }
  108. /// <summary>
  109. /// Clear All Keys
  110. /// </summary>
  111. /// <param name="sender"></param>
  112. /// <param name="e"></param>
  113. public void EventFormLostFocus(object sender, EventArgs e)
  114. {
  115. KeyPressed = Keys.None;
  116. keysDownList.Clear();
  117. lastKeyEventArgs = new KeyEventArgs(Keys.None);
  118. }
  119. }
  120. }