Advertisement
Guest User

Untitled

a guest
Jan 16th, 2013
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 25.81 KB | None | 0 0
  1. Index: Source/OpenTK/Platform/Windows/WinGLNative.cs
  2. ===================================================================
  3. --- Source/OpenTK/Platform/Windows/WinGLNative.cs   (revision 3125)
  4. +++ Source/OpenTK/Platform/Windows/WinGLNative.cs   (working copy)
  5. @@ -89,7 +89,13 @@
  6.          IList<KeyboardDevice> keyboards = new List<KeyboardDevice>(1);
  7.          IList<MouseDevice> mice = new List<MouseDevice>(1);
  8.          const long ExtendedBit = 1 << 24;           // Used to distinguish left and right control, alt and enter keys.
  9. -        static readonly uint ShiftRightScanCode = Functions.MapVirtualKey(VirtualKeys.RSHIFT, 0);         // Used to distinguish left and right shift keys.
  10. +        
  11. +        public static readonly uint ShiftLeftScanCode = Functions.MapVirtualKey(VirtualKeys.LSHIFT, 0);
  12. +        public static readonly uint ShiftRightScanCode = Functions.MapVirtualKey(VirtualKeys.RSHIFT, 0);
  13. +        public static readonly uint ControlLeftScanCode = Functions.MapVirtualKey(VirtualKeys.LCONTROL, 0);
  14. +        public static readonly uint ControlRightScanCode = Functions.MapVirtualKey(VirtualKeys.RCONTROL, 0);
  15. +        public static readonly uint AltLeftScanCode = Functions.MapVirtualKey(VirtualKeys.LMENU, 0);
  16. +        public static readonly uint AltRightScanCode = Functions.MapVirtualKey(VirtualKeys.RMENU, 0);
  17.  
  18.          KeyPressEventArgs key_press = new KeyPressEventArgs((char)0);
  19.  
  20. @@ -369,6 +375,8 @@
  21.                      // In this case, both keys will be reported as pressed.
  22.  
  23.                      bool extended = (lParam.ToInt64() & ExtendedBit) != 0;
  24. +                    uint scancode = (uint)((lParam.ToInt64() >> 16) & 0xFF);
  25. +                    Key key = Key.Unknown;
  26.                      switch ((VirtualKeys)wParam)
  27.                      {
  28.                          case VirtualKeys.SHIFT:
  29. @@ -382,56 +390,51 @@
  30.                              // Otherwise, the state of one key might be stuck to pressed.
  31.                              if (ShiftRightScanCode != 0 && pressed)
  32.                              {
  33. -                                unchecked
  34. -                                {
  35. -                                    if (((lParam.ToInt64() >> 16) & 0xFF) == ShiftRightScanCode)
  36. -                                        keyboard[Input.Key.ShiftRight] = pressed;
  37. -                                    else
  38. -                                        keyboard[Input.Key.ShiftLeft] = pressed;
  39. -                                }
  40. +                                if (scancode == ShiftRightScanCode)
  41. +                                    key = Input.Key.ShiftRight;
  42. +                                else
  43. +                                    key = Input.Key.ShiftLeft;
  44.                              }
  45.                              else
  46.                              {
  47.                                  // Windows 9x and NT4.0 or key release event.
  48. -                                keyboard[Input.Key.ShiftLeft] = keyboard[Input.Key.ShiftRight] = pressed;
  49. +                                keyboard.SetKey(Input.Key.ShiftLeft, ShiftLeftScanCode, pressed);
  50. +                                keyboard.SetKey(Input.Key.ShiftRight, ShiftRightScanCode, pressed);
  51.                              }
  52. -                            return IntPtr.Zero;
  53. +                            break;
  54.  
  55.                          case VirtualKeys.CONTROL:
  56.                              if (extended)
  57. -                                keyboard[Input.Key.ControlRight] = pressed;
  58. +                                key = Input.Key.ControlRight;
  59.                              else
  60. -                                keyboard[Input.Key.ControlLeft] = pressed;
  61. -                            return IntPtr.Zero;
  62. +                                key = Input.Key.ControlLeft;
  63. +                            break;
  64.  
  65.                          case VirtualKeys.MENU:
  66.                              if (extended)
  67. -                                keyboard[Input.Key.AltRight] = pressed;
  68. +                                key = Input.Key.AltRight;
  69.                              else
  70. -                                keyboard[Input.Key.AltLeft] = pressed;
  71. -                            return IntPtr.Zero;
  72. +                                key = Input.Key.AltLeft;
  73. +                            break;
  74.  
  75.                          case VirtualKeys.RETURN:
  76.                              if (extended)
  77. -                                keyboard[Key.KeypadEnter] = pressed;
  78. +                                key = Key.KeypadEnter;
  79.                              else
  80. -                                keyboard[Key.Enter] = pressed;
  81. -                            return IntPtr.Zero;
  82. +                                key = Key.Enter;
  83. +                            break;
  84.  
  85.                          default:
  86.                              if (!KeyMap.ContainsKey((VirtualKeys)wParam))
  87. -                            {
  88.                                  Debug.Print("Virtual key {0} ({1}) not mapped.", (VirtualKeys)wParam, (long)lParam);
  89. -                                break;
  90. -                            }
  91.                              else
  92. -                            {
  93. -                                keyboard[KeyMap[(VirtualKeys)wParam]] = pressed;
  94. -                            }
  95. -                            return IntPtr.Zero;
  96. +                                key = KeyMap[(VirtualKeys)wParam];
  97. +                            break;
  98.                      }
  99. -                    break;
  100.  
  101. +                    keyboard.SetKey(key, scancode, pressed);
  102. +                    return IntPtr.Zero;
  103. +
  104.                  case WindowMessage.SYSCHAR:
  105.                      return IntPtr.Zero;
  106.  
  107. Index: Source/OpenTK/Platform/Windows/WMInput.cs
  108. ===================================================================
  109. --- Source/OpenTK/Platform/Windows/WMInput.cs   (revision 3125)
  110. +++ Source/OpenTK/Platform/Windows/WMInput.cs   (working copy)
  111. @@ -70,14 +70,12 @@
  112.  
  113.          void UpdateKeyboard()
  114.          {
  115. -            for (int i = 0; i < 256; i++)
  116. +            for (byte i = 0; i < byte.MaxValue; i++)
  117.              {
  118. -                VirtualKeys key = (VirtualKeys)i;
  119. -                bool pressed = (Functions.GetAsyncKeyState(key) >> 8) != 0;
  120. -                if (KeyMap.ContainsKey(key))
  121. -                {
  122. -                        keyboard[KeyMap[key]] = pressed;
  123. -                }
  124. +                bool pressed = (Functions.GetAsyncKeyState((VirtualKeys)i) >> 8) != 0;
  125. +                Key key;
  126. +                KeyMap.TryGetValue((VirtualKeys)i,out key);
  127. +                keyboard.SetKeyState(key, i, pressed);
  128.              }
  129.          }
  130.  
  131. Index: Source/OpenTK/Platform/Windows/WinRawKeyboard.cs
  132. ===================================================================
  133. --- Source/OpenTK/Platform/Windows/WinRawKeyboard.cs    (revision 3125)
  134. +++ Source/OpenTK/Platform/Windows/WinRawKeyboard.cs    (working copy)
  135. @@ -171,31 +171,30 @@
  136.              switch (rin.Data.Keyboard.VKey)
  137.              {
  138.                  case VirtualKeys.SHIFT:
  139. -                    keyboard[Input.Key.ShiftLeft] = keyboard[Input.Key.ShiftRight] = pressed;
  140. +                    keyboard.SetKeyState(Key.ShiftLeft, (byte)WinGLNative.ShiftLeftScanCode, pressed);
  141. +                    keyboard.SetKeyState(Key.ShiftRight, (byte)WinGLNative.ShiftRightScanCode, pressed);
  142.                      processed = true;
  143.                      break;
  144.  
  145.                  case VirtualKeys.CONTROL:
  146. -                    keyboard[Input.Key.ControlLeft] = keyboard[Input.Key.ControlRight] = pressed;
  147. +                    keyboard.SetKeyState(Key.ControlLeft, (byte)WinGLNative.ControlLeftScanCode, pressed);
  148. +                    keyboard.SetKeyState(Key.ControlRight, (byte)WinGLNative.ControlRightScanCode, pressed);
  149.                      processed = true;
  150.                      break;
  151.  
  152.                  case VirtualKeys.MENU:
  153. -                    keyboard[Input.Key.AltLeft] = keyboard[Input.Key.AltRight] = pressed;
  154. +                    keyboard.SetKeyState(Key.AltLeft, (byte)WinGLNative.AltLeftScanCode, pressed);
  155. +                    keyboard.SetKeyState(Key.AltRight, (byte)WinGLNative.AltRightScanCode, pressed);
  156.                      processed = true;
  157.                      break;
  158.  
  159.                  default:
  160. -                    if (!KeyMap.ContainsKey(rin.Data.Keyboard.VKey))
  161. -                    {
  162. -                        Debug.Print("Virtual key {0} ({1}) not mapped.",
  163. -                                    rin.Data.Keyboard.VKey, (int)rin.Data.Keyboard.VKey);
  164. -                    }
  165. -                    else
  166. -                    {
  167. -                        keyboard[KeyMap[rin.Data.Keyboard.VKey]] = pressed;
  168. -                        processed = true;
  169. -                    }
  170. +                    Key key;
  171. +                    KeyMap.TryGetValue(rin.Data.Keyboard.VKey, out key);
  172. +                    if (key == Key.Unknown)
  173. +                        Debug.Print("Virtual key {0} ({1}) not mapped.", rin.Data.Keyboard.VKey, (int)rin.Data.Keyboard.VKey);
  174. +                    keyboard.SetKeyState(key, BitConverter.GetBytes(rin.Data.Keyboard.MakeCode)[0], pressed);
  175. +                    processed = true;
  176.                      break;
  177.              }
  178.  
  179. Index: Source/OpenTK/Platform/MacOS/HIDInput.cs
  180. ===================================================================
  181. --- Source/OpenTK/Platform/MacOS/HIDInput.cs    (revision 3125)
  182. +++ Source/OpenTK/Platform/MacOS/HIDInput.cs    (working copy)
  183. @@ -250,18 +250,13 @@
  184.              HIDPage page = NativeMethods.IOHIDElementGetUsagePage(elem);
  185.              int usage = NativeMethods.IOHIDElementGetUsage(elem);
  186.  
  187. -             switch (page)
  188. +            switch (page)
  189.              {
  190.                  case HIDPage.GenericDesktop:
  191.                  case HIDPage.KeyboardOrKeypad:
  192. -                    int raw = (int)usage;
  193. -                    if (raw >= RawKeyMap.Length || raw < 0)
  194. -                    {
  195. -                        Debug.Print("[Warning] Key {0} not mapped.", raw);
  196. -                        return state;
  197. -                    }
  198. -                    Key key = RawKeyMap[raw];
  199. -                    state[key] = v_int != 0;
  200. +                    if (usage >= RawKeyMap.Length || usage < 0)
  201. +                        Debug.Print("[Warning] Key {0} not mapped.", usage);
  202. +                    state.SetKeyState(RawKeyMap[usage], (byte)usage, v_int != 0);
  203.                      break;
  204.              }
  205.  
  206. Index: Source/OpenTK/Platform/MacOS/CarbonGLNative.cs
  207. ===================================================================
  208. --- Source/OpenTK/Platform/MacOS/CarbonGLNative.cs  (revision 3125)
  209. +++ Source/OpenTK/Platform/MacOS/CarbonGLNative.cs  (working copy)
  210. @@ -368,6 +368,7 @@
  211.                      break;
  212.              }
  213.  
  214. +            OpenTK.Input.Key key;
  215.              switch (evt.KeyboardEventKind)
  216.              {
  217.                  case KeyboardEventKind.RawKeyRepeat:
  218. @@ -376,25 +377,15 @@
  219.                      break;
  220.  
  221.                  case KeyboardEventKind.RawKeyDown:
  222. -                {
  223. -                    OpenTK.Input.Key key;
  224. -                    if (Keymap.TryGetValue(code, out key))
  225. -                    {
  226. -                        InputDriver.Keyboard[0][key] = true;
  227. -                        OnKeyPress(mKeyPressArgs);
  228. -                    }
  229. +                    Keymap.TryGetValue(code, out key);
  230. +                    InputDriver.Keyboard[0].SetKey(key, (uint)code, true);
  231. +                    OnKeyPress(mKeyPressArgs);
  232.                      return OSStatus.NoError;
  233. -                }
  234.  
  235.                  case KeyboardEventKind.RawKeyUp:
  236. -                {
  237. -                    OpenTK.Input.Key key;
  238. -                    if (Keymap.TryGetValue(code, out key))
  239. -                    {
  240. -                        InputDriver.Keyboard[0][key] = false;
  241. -                    }
  242. +                    Keymap.TryGetValue(code, out key);
  243. +                    InputDriver.Keyboard[0].SetKey(key, (uint)code, false);
  244.                      return OSStatus.NoError;
  245. -                }
  246.  
  247.                  case KeyboardEventKind.RawKeyModifiersChanged:
  248.                      ProcessModifierKey(inEvent);
  249. @@ -614,21 +605,21 @@
  250.              Debug.Print("Modifiers Changed: {0}", modifiers);
  251.              
  252.              Input.KeyboardDevice keyboard = InputDriver.Keyboard[0];
  253. -            
  254. +
  255.              if (keyboard[OpenTK.Input.Key.AltLeft] ^ option)
  256. -                keyboard[OpenTK.Input.Key.AltLeft] = option;
  257. +                keyboard.SetKey(OpenTK.Input.Key.AltLeft, (uint)MacOSKeyModifiers.Option, option);
  258.              
  259.              if (keyboard[OpenTK.Input.Key.ShiftLeft] ^ shift)
  260. -                keyboard[OpenTK.Input.Key.ShiftLeft] = shift;
  261. +                keyboard.SetKey(OpenTK.Input.Key.ShiftLeft, (uint)MacOSKeyModifiers.Shift, shift);
  262.              
  263.              if (keyboard[OpenTK.Input.Key.WinLeft] ^ command)
  264. -                keyboard[OpenTK.Input.Key.WinLeft] = command;
  265. +                keyboard.SetKey(OpenTK.Input.Key.WinLeft, (uint)MacOSKeyModifiers.Command, command);
  266.              
  267.              if (keyboard[OpenTK.Input.Key.ControlLeft] ^ control)
  268. -                keyboard[OpenTK.Input.Key.ControlLeft] = control;
  269. +                keyboard.SetKey(OpenTK.Input.Key.ControlLeft, (uint)MacOSKeyModifiers.Control, control);
  270.              
  271.              if (keyboard[OpenTK.Input.Key.CapsLock] ^ caps)
  272. -                keyboard[OpenTK.Input.Key.CapsLock] = caps;
  273. +                keyboard.SetKey(OpenTK.Input.Key.CapsLock, (uint)MacOSKeyModifiers.CapsLock, caps);
  274.              
  275.          }
  276.  
  277. Index: Source/OpenTK/Platform/X11/X11Input.cs
  278. ===================================================================
  279. --- Source/OpenTK/Platform/X11/X11Input.cs  (revision 3125)
  280. +++ Source/OpenTK/Platform/X11/X11Input.cs  (working copy)
  281. @@ -157,20 +157,22 @@
  282.                  case XEventName.KeyPress:
  283.                  case XEventName.KeyRelease:
  284.                      bool pressed = e.type == XEventName.KeyPress;
  285. +                    XKey keysym = (XKey)API.LookupKeysym(ref e.KeyEvent, 0);
  286. +                    XKey keysym2 = (XKey)API.LookupKeysym(ref e.KeyEvent, 1);
  287. +                    Key key = Key.Unknown;
  288.  
  289. -                    IntPtr keysym = API.LookupKeysym(ref e.KeyEvent, 0);
  290. -                    IntPtr keysym2 = API.LookupKeysym(ref e.KeyEvent, 1);
  291. -
  292. -                    if (keymap.ContainsKey((XKey)keysym))
  293. -                        keyboard[keymap[(XKey)keysym]] = pressed;
  294. -                    else if (keymap.ContainsKey((XKey)keysym2))
  295. -                        keyboard[keymap[(XKey)keysym2]] = pressed;
  296. +                    if (keymap.ContainsKey(keysym))
  297. +                        key = keymap[keysym];
  298. +                    else if (keymap.ContainsKey(keysym2))
  299. +                        key = keymap[keysym2];
  300.                      else
  301.                          Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.KeyEvent.keycode, (XKey)keysym, (XKey)keysym2);
  302. +
  303. +                    keyboard.SetKey(key, (uint)e.KeyEvent.keycode, pressed);
  304.                      break;
  305.  
  306.                  case XEventName.ButtonPress:
  307. -                    if      (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = true;
  308. +                    if (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = true;
  309.                      else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = true;
  310.                      else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = true;
  311.                      else if (e.ButtonEvent.button == 4) mouse.Wheel++;
  312. @@ -190,7 +192,7 @@
  313.                      break;
  314.  
  315.                  case XEventName.ButtonRelease:
  316. -                    if      (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = false;
  317. +                    if (e.ButtonEvent.button == 1) mouse[OpenTK.Input.MouseButton.Left] = false;
  318.                      else if (e.ButtonEvent.button == 2) mouse[OpenTK.Input.MouseButton.Middle] = false;
  319.                      else if (e.ButtonEvent.button == 3) mouse[OpenTK.Input.MouseButton.Right] = false;
  320.                      else if (e.ButtonEvent.button == 6) mouse[OpenTK.Input.MouseButton.Button1] = false;
  321. Index: Source/OpenTK/Input/KeyboardDevice.cs
  322. ===================================================================
  323. --- Source/OpenTK/Input/KeyboardDevice.cs   (revision 3125)
  324. +++ Source/OpenTK/Input/KeyboardDevice.cs   (working copy)
  325. @@ -22,6 +22,7 @@
  326.      {
  327.          //private IKeyboard keyboard;
  328.          private bool[] keys = new bool[Enum.GetValues(typeof(Key)).Length];
  329. +        private bool[] scancodes = new bool[256];
  330.          private string description;
  331.          private int numKeys, numFKeys, numLeds;
  332.          private IntPtr devID;
  333. @@ -44,24 +45,16 @@
  334.          public bool this[Key key]
  335.          {
  336.              get { return keys[(int)key]; }
  337. -            internal set
  338. -            {
  339. -                if (keys[(int)key] != value || KeyRepeat)
  340. -                {
  341. -                    keys[(int)key] = value;
  342. +        }
  343.  
  344. -                    if (value && KeyDown != null)
  345. -                    {
  346. -                        args.Key = key;
  347. -                        KeyDown(this, args);
  348. -                    }
  349. -                    else if (!value && KeyUp != null)
  350. -                    {
  351. -                        args.Key = key;
  352. -                        KeyUp(this, args);
  353. -                    }
  354. -                }
  355. -            }
  356. +        /// <summary>
  357. +        /// Gets a value indicating the status of the specified Key.
  358. +        /// </summary>
  359. +        /// <param name="scancode">The scancode to check.</param>
  360. +        /// <returns>True if the scancode is pressed, false otherwise.</returns>
  361. +        public bool this[uint scancode]
  362. +        {
  363. +            get { return scancodes[scancode]; }
  364.          }
  365.  
  366.          /// <summary>
  367. @@ -197,12 +190,34 @@
  368.          internal void ClearKeys()
  369.          {
  370.              for (int i = 0; i < keys.Length; i++)
  371. -                if (this[(Key)i])       // Make sure KeyUp events are *not* raised for keys that are up, even if key repeat is on.
  372. -                    this[(Key)i] = false;
  373. +                keys[i] = false;
  374. +            for (uint i = 0; i < scancodes.Length; i++)
  375. +                scancodes[i] = false;
  376.          }
  377.  
  378.          #endregion
  379.  
  380. +        internal void SetKey(Key key, uint scancode, bool state)
  381. +        {
  382. +            if (keys[(int)key] != state || KeyRepeat)
  383. +            {
  384. +                keys[(int)key] = scancodes[scancode] = state;
  385. +
  386. +                if (state && KeyDown != null)
  387. +                {
  388. +                    args.Key = key;
  389. +                    args.ScanCode = scancode;
  390. +                    KeyDown(this, args);
  391. +                }
  392. +                else if (!state && KeyUp != null)
  393. +                {
  394. +                    args.Key = key;
  395. +                    args.ScanCode = scancode;
  396. +                    KeyUp(this, args);
  397. +                }
  398. +            }
  399. +        }
  400. +
  401.          #endregion
  402.      }
  403.  }
  404. \ No newline at end of file
  405. Index: Source/OpenTK/Input/KeyboardKeyEventArgs.cs
  406. ===================================================================
  407. --- Source/OpenTK/Input/KeyboardKeyEventArgs.cs (revision 3125)
  408. +++ Source/OpenTK/Input/KeyboardKeyEventArgs.cs (working copy)
  409. @@ -46,6 +46,7 @@
  410.          #region Fields
  411.  
  412.          Key key;
  413. +        uint scancode;
  414.  
  415.          #endregion
  416.  
  417. @@ -63,6 +64,7 @@
  418.          public KeyboardKeyEventArgs(KeyboardKeyEventArgs args)
  419.          {
  420.              Key = args.Key;
  421. +            ScanCode = args.ScanCode;
  422.          }
  423.  
  424.          #endregion
  425. @@ -77,6 +79,14 @@
  426.              get { return key; }
  427.              internal set { key = value; }
  428.          }
  429. +        /// <summary>
  430. +        /// Gets the scancode which generated this event.
  431. +        /// </summary>
  432. +        public uint ScanCode
  433. +        {
  434. +            get { return scancode; }
  435. +            internal set { scancode = value; }
  436. +        }
  437.  
  438.          #endregion
  439.      }
  440. Index: Source/OpenTK/Input/KeyboardState.cs
  441. ===================================================================
  442. --- Source/OpenTK/Input/KeyboardState.cs    (revision 3125)
  443. +++ Source/OpenTK/Input/KeyboardState.cs    (working copy)
  444. @@ -43,6 +43,7 @@
  445.          const int NumInts = ((int)Key.LastKey + IntSize - 1) / IntSize;
  446.          // The following line triggers bogus CS0214 in gmcs 2.0.1, sigh...
  447.          unsafe fixed int Keys[NumInts];
  448. +        unsafe fixed int Codes[256];
  449.          bool is_connected;
  450.  
  451.          #endregion
  452. @@ -58,16 +59,20 @@
  453.          public bool this[Key key]
  454.          {
  455.              get { return IsKeyDown(key); }
  456. -            internal set
  457. -            {
  458. -                if (value)
  459. -                    EnableBit((int)key);
  460. -                else
  461. -                    DisableBit((int)key);
  462. -            }
  463.          }
  464.  
  465.          /// <summary>
  466. +        /// Gets a <see cref="System.Boolean"/> indicating whether the specified
  467. +        /// <see cref="OpenTK.Input.Key"/> is pressed.
  468. +        /// </summary>
  469. +        /// <param name="key">The <see cref="OpenTK.Input.Key"/> to check.</param>
  470. +        /// <returns>True if key is pressed; false otherwise.</returns>
  471. +        public bool this[short code]
  472. +        {
  473. +            get { return IsKeyDown(code); }
  474. +        }
  475. +
  476. +        /// <summary>
  477.          /// Gets a <see cref="System.Boolean"/> indicating whether this key is down.
  478.          /// </summary>
  479.          /// <param name="key">The <see cref="OpenTK.Input.Key"/> to check.</param>
  480. @@ -77,6 +82,15 @@
  481.          }
  482.  
  483.          /// <summary>
  484. +        /// Gets a <see cref="System.Boolean"/> indicating whether this scan code is down.
  485. +        /// </summary>
  486. +        /// <param name="code">The scan code to check.</param>
  487. +        public bool IsKeyDown(short code)
  488. +        {
  489. +            return ReadBit(code,true);
  490. +        }
  491. +
  492. +        /// <summary>
  493.          /// Gets a <see cref="System.Boolean"/> indicating whether this key is up.
  494.          /// </summary>
  495.          /// <param name="key">The <see cref="OpenTK.Input.Key"/> to check.</param>
  496. @@ -86,6 +100,15 @@
  497.          }
  498.  
  499.          /// <summary>
  500. +        /// Gets a <see cref="System.Boolean"/> indicating whether this scan code is down.
  501. +        /// </summary>
  502. +        /// <param name="code">The scan code to check.</param>
  503. +        public bool IsKeyUp(short code)
  504. +        {
  505. +            return !ReadBit(code,true);
  506. +        }
  507. +
  508. +        /// <summary>
  509.          /// Gets a <see cref="System.Boolean"/> indicating whether this keyboard
  510.          /// is connected.
  511.          /// </summary>
  512. @@ -187,48 +210,62 @@
  513.  
  514.          #region Internal Members
  515.  
  516. -        internal bool ReadBit(int offset)
  517. +        internal void SetKeyState(Key key, byte code, bool down)
  518.          {
  519. -            ValidateOffset(offset);
  520. +            if (down)
  521. +            {
  522. +                EnableBit((int)key);
  523. +                EnableBit(code,true);
  524. +            }
  525. +            else
  526. +            {
  527. +                DisableBit((int)key);
  528. +                DisableBit(code, true);
  529. +            }
  530. +        }
  531.  
  532. +        internal bool ReadBit(int offset, bool ScanCode = false)
  533. +        {
  534. +            ValidateOffset(offset, ScanCode);
  535. +
  536.              int int_offset = offset / 32;
  537.              int bit_offset = offset % 32;
  538.              unsafe
  539.              {
  540. -                fixed (int* k = Keys)
  541. -                {
  542. -                    return (*(k + int_offset) & (1 << bit_offset)) != 0u;
  543. -                }
  544. +                if (ScanCode)
  545. +                    fixed (int* c = Codes) { return (*(c + int_offset) & (1 << bit_offset)) != 0u; }
  546. +                else
  547. +                    fixed (int* k = Keys) { return (*(k + int_offset) & (1 << bit_offset)) != 0u; }
  548.              }
  549.          }
  550.  
  551. -        internal void EnableBit(int offset)
  552. +        internal void EnableBit(int offset, bool ScanCode = false)
  553.          {
  554. -            ValidateOffset(offset);
  555. +            ValidateOffset(offset, ScanCode);
  556.  
  557.              int int_offset = offset / 32;
  558.              int bit_offset = offset % 32;
  559.              unsafe
  560.              {
  561. -                fixed (int* k = Keys)
  562. -                {
  563. -                    *(k + int_offset) |= 1 << bit_offset;
  564. -                }
  565. +                if (ScanCode)
  566. +                    fixed (int* c = Codes) { *(c + int_offset) |= 1 << bit_offset; }
  567. +                else
  568. +                    fixed (int* k = Keys) { *(k + int_offset) |= 1 << bit_offset; }
  569.              }
  570.          }
  571.  
  572. -        internal void DisableBit(int offset)
  573. +        internal void DisableBit(int offset, bool ScanCode = false)
  574.          {
  575. -            ValidateOffset(offset);
  576. +            ValidateOffset(offset, ScanCode);
  577.  
  578.              int int_offset = offset / 32;
  579.              int bit_offset = offset % 32;
  580.              unsafe
  581.              {
  582. -                fixed (int* k = Keys)
  583. -                {
  584. -                    *(k + int_offset) &= ~(1 << bit_offset);
  585. -                }
  586. +                if (ScanCode)
  587. +                    fixed (int* c = Codes) { *(c + int_offset) &= ~(1 << bit_offset); }
  588. +                else
  589. +                    fixed (int* k = Keys) { *(k + int_offset) &= ~(1 << bit_offset); }
  590.              }
  591.          }
  592.  
  593. @@ -242,6 +279,12 @@
  594.                      for (int i = 0; i < NumInts; i++)
  595.                          *(k1 + i) |= *(k2 + i);
  596.                  }
  597. +                int* c2 = other.Codes;
  598. +                fixed (int* c1 = Codes)
  599. +                {
  600. +                    for (int i = 0; i < short.MaxValue; i++)
  601. +                        *(c1 + i) |= *(c2 + i);
  602. +                }
  603.              }
  604.              IsConnected |= other.IsConnected;
  605.          }
  606. @@ -250,9 +293,9 @@
  607.  
  608.          #region Private Members
  609.  
  610. -        static void ValidateOffset(int offset)
  611. +        static void ValidateOffset(int offset, bool ScanCode)
  612.          {
  613. -            if (offset < 0 || offset >= NumInts * IntSize)
  614. +            if (offset < 0 || offset >= (ScanCode ? 256 : NumInts * IntSize))
  615.                  throw new ArgumentOutOfRangeException("offset");
  616.          }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement