Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1452 | chris | 1 | #region --- License --- |
| 2 | /* Copyright (c) 2007 Stefanos Apostolopoulos |
||
| 3 | * See license.txt for license info |
||
| 4 | */ |
||
| 5 | #endregion |
||
| 6 | |||
| 7 | #region --- Using directives --- |
||
| 8 | |||
| 9 | using System; |
||
| 10 | |||
| 11 | using OpenTK.Input; |
||
| 12 | using System.Diagnostics; |
||
| 13 | |||
| 14 | #endregion |
||
| 15 | |||
| 16 | namespace OpenTK.Input |
||
| 17 | { |
||
| 18 | /// <summary> |
||
| 19 | /// Represents a keyboard device and provides methods to query its status. |
||
| 20 | /// </summary> |
||
| 21 | public sealed class KeyboardDevice : IInputDevice |
||
| 22 | { |
||
| 23 | //private IKeyboard keyboard; |
||
| 24 | private bool[] keys = new bool[Enum.GetValues(typeof(Key)).Length]; |
||
| 25 | private string description; |
||
| 26 | private int numKeys, numFKeys, numLeds; |
||
| 27 | private IntPtr devID; |
||
| 28 | private bool repeat; |
||
| 29 | private KeyboardKeyEventArgs args = new KeyboardKeyEventArgs(); |
||
| 30 | |||
| 31 | #region --- Constructors --- |
||
| 32 | |||
| 33 | internal KeyboardDevice() { } |
||
| 34 | |||
| 35 | #endregion |
||
| 36 | |||
| 37 | #region --- IKeyboard members --- |
||
| 38 | |||
| 39 | /// <summary> |
||
| 40 | /// Gets a value indicating the status of the specified Key. |
||
| 41 | /// </summary> |
||
| 42 | /// <param name="key">The Key to check.</param> |
||
| 43 | /// <returns>True if the Key is pressed, false otherwise.</returns> |
||
| 44 | public bool this[Key key] |
||
| 45 | { |
||
| 46 | get { return keys[(int)key]; } |
||
| 47 | internal set |
||
| 48 | { |
||
| 49 | if (keys[(int)key] != value || KeyRepeat) |
||
| 50 | { |
||
| 51 | keys[(int)key] = value; |
||
| 52 | |||
| 53 | if (value && KeyDown != null) |
||
| 54 | { |
||
| 55 | args.Key = key; |
||
| 56 | KeyDown(this, args); |
||
| 57 | } |
||
| 58 | else if (!value && KeyUp != null) |
||
| 59 | { |
||
| 60 | args.Key = key; |
||
| 61 | KeyUp(this, args); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /// <summary> |
||
| 68 | /// Gets an integer representing the number of keys on this KeyboardDevice. |
||
| 69 | /// </summary> |
||
| 70 | public int NumberOfKeys |
||
| 71 | { |
||
| 72 | get { return numKeys; } |
||
| 73 | internal set { numKeys = value; } |
||
| 74 | } |
||
| 75 | |||
| 76 | /// <summary> |
||
| 77 | /// Gets an integer representing the number of function keys (F-keys) on this KeyboardDevice. |
||
| 78 | /// </summary> |
||
| 79 | public int NumberOfFunctionKeys |
||
| 80 | { |
||
| 81 | get { return numFKeys; } |
||
| 82 | internal set { numFKeys = value; } |
||
| 83 | } |
||
| 84 | |||
| 85 | /// <summary> |
||
| 86 | /// Gets a value indicating the number of led indicators on this KeyboardDevice. |
||
| 87 | /// </summary> |
||
| 88 | public int NumberOfLeds |
||
| 89 | { |
||
| 90 | get { return numLeds; } |
||
| 91 | internal set { numLeds = value; } |
||
| 92 | } |
||
| 93 | |||
| 94 | /// <summary> |
||
| 95 | /// Gets an IntPtr representing a device dependent ID. |
||
| 96 | /// </summary> |
||
| 97 | public IntPtr DeviceID |
||
| 98 | { |
||
| 99 | get { return devID; } |
||
| 100 | internal set { devID = value; } |
||
| 101 | } |
||
| 102 | |||
| 103 | #region public bool KeyRepeat |
||
| 104 | |||
| 105 | /// <summary> |
||
| 106 | /// Gets or sets a System.Boolean indicating key repeat status. |
||
| 107 | /// </summary> |
||
| 108 | /// <remarks> |
||
| 109 | /// If KeyRepeat is true, multiple KeyDown events will be generated while a key is being held. |
||
| 110 | /// Otherwise only one KeyDown event will be reported. |
||
| 111 | /// <para> |
||
| 112 | /// The rate of the generated KeyDown events is controlled by the Operating System. Usually, |
||
| 113 | /// one KeyDown event will be reported, followed by a small (250-1000ms) pause and several |
||
| 114 | /// more KeyDown events (6-30 events per second). |
||
| 115 | /// </para> |
||
| 116 | /// <para> |
||
| 117 | /// Set to true to handle text input (where keyboard repeat is desirable), but set to false |
||
| 118 | /// for game input. |
||
| 119 | /// </para> |
||
| 120 | /// </remarks> |
||
| 121 | public bool KeyRepeat |
||
| 122 | { |
||
| 123 | get { return repeat; } |
||
| 124 | set { repeat = value; } |
||
| 125 | } |
||
| 126 | |||
| 127 | #endregion |
||
| 128 | |||
| 129 | #region KeyDown |
||
| 130 | |||
| 131 | /// <summary> |
||
| 132 | /// Occurs when a key is pressed. |
||
| 133 | /// </summary> |
||
| 134 | public event EventHandler<KeyboardKeyEventArgs> KeyDown; |
||
| 135 | |||
| 136 | #endregion |
||
| 137 | |||
| 138 | #region KeyUp |
||
| 139 | |||
| 140 | /// <summary> |
||
| 141 | /// Occurs when a key is released. |
||
| 142 | /// </summary> |
||
| 143 | public event EventHandler<KeyboardKeyEventArgs> KeyUp; |
||
| 144 | |||
| 145 | #endregion |
||
| 146 | |||
| 147 | #endregion |
||
| 148 | |||
| 149 | #region --- IInputDevice Members --- |
||
| 150 | |||
| 151 | /// <summary> |
||
| 152 | /// Gets a <see cref="System.String"/> which describes this instance. |
||
| 153 | /// </summary> |
||
| 154 | public string Description |
||
| 155 | { |
||
| 156 | get { return description; } |
||
| 157 | internal set { description = value; } |
||
| 158 | } |
||
| 159 | |||
| 160 | /// <summary> |
||
| 161 | /// Gets the <see cref="InputDeviceType"/> for this instance. |
||
| 162 | /// </summary> |
||
| 163 | public InputDeviceType DeviceType |
||
| 164 | { |
||
| 165 | get { return InputDeviceType.Keyboard; } |
||
| 166 | } |
||
| 167 | |||
| 168 | #endregion |
||
| 169 | |||
| 170 | #region --- Public Methods --- |
||
| 171 | |||
| 172 | /// <summary>Returns the hash code for this KeyboardDevice.</summary> |
||
| 173 | /// <returns>A 32-bit signed integer hash code.</returns> |
||
| 174 | public override int GetHashCode() |
||
| 175 | { |
||
| 176 | //return base.GetHashCode(); |
||
| 177 | return (int)(numKeys ^ numFKeys ^ numLeds ^ devID.GetHashCode() ^ description.GetHashCode()); |
||
| 178 | } |
||
| 179 | |||
| 180 | /// <summary> |
||
| 181 | /// Returns a System.String representing this KeyboardDevice. |
||
| 182 | /// </summary> |
||
| 183 | /// <returns>A System.String representing this KeyboardDevice.</returns> |
||
| 184 | public override string ToString() |
||
| 185 | { |
||
| 186 | //return base.ToString(); |
||
| 187 | return String.Format("ID: {0} ({1}). Keys: {2}, Function keys: {3}, Leds: {4}", |
||
| 188 | DeviceID, Description, NumberOfKeys, NumberOfFunctionKeys, NumberOfLeds); |
||
| 189 | } |
||
| 190 | |||
| 191 | #endregion |
||
| 192 | |||
| 193 | #region --- Internal Methods --- |
||
| 194 | |||
| 195 | #region internal void ClearKeys() |
||
| 196 | |||
| 197 | internal void ClearKeys() |
||
| 198 | { |
||
| 199 | for (int i = 0; i < keys.Length; i++) |
||
| 200 | if (this[(Key)i]) // Make sure KeyUp events are *not* raised for keys that are up, even if key repeat is on. |
||
| 201 | this[(Key)i] = false; |
||
| 202 | } |
||
| 203 | |||
| 204 | #endregion |
||
| 205 | |||
| 206 | #endregion |
||
| 207 | } |
||
| 208 | } |