2007-07-27 03:37:12 +02:00
|
|
|
|
#region --- License ---
|
|
|
|
|
/* Copyright (c) 2007 Stefanos Apostolopoulos
|
|
|
|
|
* See license.txt for license info
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region --- Using directives ---
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
|
2007-08-04 14:09:58 +02:00
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
2007-07-27 03:37:12 +02:00
|
|
|
|
#endregion
|
2007-07-27 00:56:55 +02:00
|
|
|
|
|
|
|
|
|
namespace OpenTK.Input
|
|
|
|
|
{
|
2008-01-06 11:38:44 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a keyboard device and provides methods to query its status.
|
|
|
|
|
/// </summary>
|
2007-09-26 13:30:18 +02:00
|
|
|
|
public sealed class KeyboardDevice : IInputDevice
|
2007-07-27 00:56:55 +02:00
|
|
|
|
{
|
2007-08-04 14:09:58 +02:00
|
|
|
|
//private IKeyboard keyboard;
|
2007-08-05 18:04:39 +02:00
|
|
|
|
private bool[] keys = new bool[(int)Key.MaxKeys];
|
2007-08-04 14:09:58 +02:00
|
|
|
|
private string description;
|
|
|
|
|
private int numKeys, numFKeys, numLeds;
|
2007-09-21 22:03:53 +02:00
|
|
|
|
private IntPtr devID;
|
|
|
|
|
private bool repeat;
|
2007-07-27 00:56:55 +02:00
|
|
|
|
|
2007-08-03 02:14:31 +02:00
|
|
|
|
#region --- Constructors ---
|
|
|
|
|
|
2008-03-10 09:49:48 +01:00
|
|
|
|
internal KeyboardDevice() { }
|
2007-07-27 00:56:55 +02:00
|
|
|
|
|
2007-08-03 02:14:31 +02:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-07-27 03:20:55 +02:00
|
|
|
|
#region --- IKeyboard members ---
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating the status of the specified Key.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key">The Key to check.</param>
|
|
|
|
|
/// <returns>True if the Key is pressed, false otherwise.</returns>
|
|
|
|
|
public bool this[Key key]
|
2007-07-27 00:56:55 +02:00
|
|
|
|
{
|
2007-09-26 13:30:18 +02:00
|
|
|
|
get { return keys[(int)key]; }
|
2007-08-04 14:09:58 +02:00
|
|
|
|
internal set
|
|
|
|
|
{
|
2007-09-26 13:30:18 +02:00
|
|
|
|
if (keys[(int)key] != value || KeyRepeat)
|
2007-08-20 15:45:04 +02:00
|
|
|
|
{
|
2007-09-26 13:30:18 +02:00
|
|
|
|
keys[(int)key] = value;
|
2007-09-21 22:03:53 +02:00
|
|
|
|
|
|
|
|
|
if (value && KeyDown != null)
|
|
|
|
|
{
|
2007-09-26 13:30:18 +02:00
|
|
|
|
KeyDown(this, key);
|
2007-09-21 22:03:53 +02:00
|
|
|
|
}
|
|
|
|
|
else if (!value && KeyUp != null)
|
|
|
|
|
{
|
2007-09-26 13:30:18 +02:00
|
|
|
|
KeyUp(this, key);
|
2007-09-21 22:03:53 +02:00
|
|
|
|
}
|
2007-08-20 15:45:04 +02:00
|
|
|
|
}
|
2007-08-04 14:09:58 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the number of keys on this KeyboardDevice.
|
|
|
|
|
/// </summary>
|
2007-08-04 14:09:58 +02:00
|
|
|
|
public int NumberOfKeys
|
|
|
|
|
{
|
|
|
|
|
get { return numKeys; }
|
|
|
|
|
internal set { numKeys = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets an integer representing the number of function keys (F-keys) on this KeyboardDevice.
|
|
|
|
|
/// </summary>
|
2007-08-04 14:09:58 +02:00
|
|
|
|
public int NumberOfFunctionKeys
|
|
|
|
|
{
|
|
|
|
|
get { return numFKeys; }
|
|
|
|
|
internal set { numFKeys = value; }
|
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets a value indicating the number of led indicators on this KeyboardDevice.
|
|
|
|
|
/// </summary>
|
2007-08-04 14:09:58 +02:00
|
|
|
|
public int NumberOfLeds
|
|
|
|
|
{
|
|
|
|
|
get { return numLeds; }
|
|
|
|
|
internal set { numLeds = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-09-26 13:30:18 +02:00
|
|
|
|
/// Gets an IntPtr representing a device dependent ID.
|
2007-08-04 14:09:58 +02:00
|
|
|
|
/// </summary>
|
2007-09-21 22:03:53 +02:00
|
|
|
|
public IntPtr DeviceID
|
2007-08-04 14:09:58 +02:00
|
|
|
|
{
|
|
|
|
|
get { return devID; }
|
|
|
|
|
internal set { devID = value; }
|
2007-07-27 00:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-21 22:03:53 +02:00
|
|
|
|
#region public bool KeyRepeat
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2007-12-09 20:54:08 +01:00
|
|
|
|
/// Gets or sets a System.Boolean indicating key repeat status.
|
2007-09-21 22:03:53 +02:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
2007-12-09 20:54:08 +01:00
|
|
|
|
/// If KeyRepeat is true, multiple KeyDown events will be generated while a key is being held.
|
|
|
|
|
/// Otherwise only one KeyDown event will be reported.
|
|
|
|
|
/// <para>
|
|
|
|
|
/// The rate of the generated KeyDown events is controlled by the Operating System. Usually,
|
|
|
|
|
/// one KeyDown event will be reported, followed by a small (250-1000ms) pause and several
|
|
|
|
|
/// more KeyDown events (6-30 events per second).
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// Set to true to handle text input (where keyboard repeat is desirable), but set to false
|
|
|
|
|
/// for game input.
|
|
|
|
|
/// </para>
|
2007-09-21 22:03:53 +02:00
|
|
|
|
/// </remarks>
|
|
|
|
|
public bool KeyRepeat
|
|
|
|
|
{
|
|
|
|
|
get { return repeat; }
|
|
|
|
|
set { repeat = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2008-03-10 09:49:48 +01:00
|
|
|
|
#region public event KeyDownEvent KeyDown;
|
|
|
|
|
|
2007-08-20 15:45:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when a key is pressed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event KeyDownEvent KeyDown;
|
|
|
|
|
|
2008-03-10 09:49:48 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region public event KeyUpEvent KeyUp;
|
|
|
|
|
|
2007-08-20 15:45:04 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Occurs when a key is released.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event KeyUpEvent KeyUp;
|
|
|
|
|
|
2007-07-27 03:20:55 +02:00
|
|
|
|
#endregion
|
2007-08-03 02:14:31 +02:00
|
|
|
|
|
2008-03-10 09:49:48 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
2007-08-03 02:14:31 +02:00
|
|
|
|
#region --- IInputDevice Members ---
|
|
|
|
|
|
|
|
|
|
public string Description
|
|
|
|
|
{
|
2007-08-04 14:09:58 +02:00
|
|
|
|
get { return description; }
|
|
|
|
|
internal set { description = value; }
|
2007-08-03 02:14:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public InputDeviceType DeviceType
|
|
|
|
|
{
|
2007-08-04 14:09:58 +02:00
|
|
|
|
get { return InputDeviceType.Keyboard; }
|
2007-08-03 02:14:31 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-08-04 14:09:58 +02:00
|
|
|
|
|
2007-08-20 15:45:04 +02:00
|
|
|
|
#region --- Public Methods ---
|
|
|
|
|
|
2008-03-10 09:49:48 +01:00
|
|
|
|
/// <summary>Returns the hash code for this KeyboardDevice.</summary>
|
|
|
|
|
/// <returns>A 32-bit signed integer hash code.</returns>
|
2007-08-04 14:09:58 +02:00
|
|
|
|
public override int GetHashCode()
|
|
|
|
|
{
|
|
|
|
|
//return base.GetHashCode();
|
|
|
|
|
return (int)(numKeys ^ numFKeys ^ numLeds ^ devID.GetHashCode() ^ description.GetHashCode());
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-10 09:49:48 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a System.String representing this KeyboardDevice.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A System.String representing this KeyboardDevice.</returns>
|
2007-08-04 14:09:58 +02:00
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
//return base.ToString();
|
2007-09-26 13:30:18 +02:00
|
|
|
|
return String.Format("ID: {0} ({1}). Keys: {2}, Function keys: {3}, Leds: {4}",
|
|
|
|
|
DeviceID, Description, NumberOfKeys, NumberOfFunctionKeys, NumberOfLeds);
|
2007-08-04 14:09:58 +02:00
|
|
|
|
}
|
2007-08-20 15:45:04 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
2008-03-10 09:49:48 +01:00
|
|
|
|
|
|
|
|
|
#region --- Internal Methods ---
|
|
|
|
|
|
|
|
|
|
#region internal void ClearKeys()
|
|
|
|
|
|
|
|
|
|
internal void ClearKeys()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < (int)Key.MaxKeys; i++)
|
|
|
|
|
this[(Key)i] = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#endregion
|
2007-07-27 00:56:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2007-09-26 13:30:18 +02:00
|
|
|
|
public delegate void KeyDownEvent(KeyboardDevice sender, Key key);
|
|
|
|
|
public delegate void KeyUpEvent(KeyboardDevice sender, Key key);
|
|
|
|
|
|
2007-08-05 18:04:39 +02:00
|
|
|
|
#region public enum Key : int
|
2007-08-03 02:14:31 +02:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The available keyboard keys.
|
|
|
|
|
/// </summary>
|
2007-08-05 18:04:39 +02:00
|
|
|
|
public enum Key : int
|
2007-07-27 00:56:55 +02:00
|
|
|
|
{
|
|
|
|
|
// Modifiers
|
2007-08-05 18:04:39 +02:00
|
|
|
|
ShiftLeft = 0,
|
|
|
|
|
ShiftRight,
|
|
|
|
|
ControlLeft,
|
|
|
|
|
ControlRight,
|
|
|
|
|
AltLeft,
|
|
|
|
|
AltRight,
|
|
|
|
|
WinLeft,
|
|
|
|
|
WinRight,
|
2007-08-03 02:14:31 +02:00
|
|
|
|
Menu,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
|
|
|
|
|
// Function keys (hopefully enough for most keyboards - mine has 26)
|
2007-08-05 18:04:39 +02:00
|
|
|
|
// <keysymdef.h> on X11 reports up to 35 function keys.
|
2007-07-27 00:56:55 +02:00
|
|
|
|
F1, F2, F3, F4,
|
|
|
|
|
F5, F6, F7, F8,
|
|
|
|
|
F9, F10, F11, F12,
|
|
|
|
|
F13, F14, F15, F16,
|
|
|
|
|
F17, F18, F19, F20,
|
|
|
|
|
F21, F22, F23, F24,
|
|
|
|
|
F25, F26, F27, F28,
|
|
|
|
|
F29, F30, F31, F32,
|
2007-08-05 18:04:39 +02:00
|
|
|
|
F33, F34, F35,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
|
|
|
|
|
// Direction arrows
|
|
|
|
|
Up,
|
|
|
|
|
Down,
|
|
|
|
|
Left,
|
|
|
|
|
Right,
|
|
|
|
|
|
|
|
|
|
Enter,
|
|
|
|
|
Escape,
|
|
|
|
|
Space,
|
|
|
|
|
Tab,
|
2007-08-06 11:22:04 +02:00
|
|
|
|
BackSpace,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
Insert,
|
|
|
|
|
Delete,
|
|
|
|
|
PageUp,
|
|
|
|
|
PageDown,
|
|
|
|
|
Home,
|
|
|
|
|
End,
|
|
|
|
|
CapsLock,
|
2007-09-21 22:03:53 +02:00
|
|
|
|
ScrollLock,
|
2007-08-03 02:14:31 +02:00
|
|
|
|
PrintScreen,
|
|
|
|
|
Pause,
|
|
|
|
|
NumLock,
|
|
|
|
|
|
|
|
|
|
// Special keys
|
2007-09-21 22:03:53 +02:00
|
|
|
|
Clear, // Keypad5 with NumLock off.
|
2007-08-03 02:14:31 +02:00
|
|
|
|
Sleep,
|
|
|
|
|
/*LogOff,
|
|
|
|
|
Help,
|
|
|
|
|
Undo,
|
|
|
|
|
Redo,
|
|
|
|
|
New,
|
|
|
|
|
Open,
|
|
|
|
|
Close,
|
|
|
|
|
Reply,
|
|
|
|
|
Forward,
|
|
|
|
|
Send,
|
|
|
|
|
Spell,
|
|
|
|
|
Save,
|
|
|
|
|
Calculator,
|
|
|
|
|
|
|
|
|
|
// Folders and applications
|
|
|
|
|
Documents,
|
|
|
|
|
Pictures,
|
|
|
|
|
Music,
|
|
|
|
|
MediaPlayer,
|
|
|
|
|
Mail,
|
|
|
|
|
Browser,
|
|
|
|
|
Messenger,
|
|
|
|
|
|
|
|
|
|
// Multimedia keys
|
|
|
|
|
Mute,
|
|
|
|
|
PlayPause,
|
|
|
|
|
Stop,
|
|
|
|
|
VolumeUp,
|
2007-09-21 22:03:53 +02:00
|
|
|
|
VolumeDown,
|
|
|
|
|
TrackPrevious,
|
|
|
|
|
TrackNext,*/
|
2007-07-27 00:56:55 +02:00
|
|
|
|
|
|
|
|
|
// Keypad keys
|
|
|
|
|
Keypad0,
|
|
|
|
|
Keypad1,
|
|
|
|
|
Keypad2,
|
|
|
|
|
Keypad3,
|
|
|
|
|
Keypad4,
|
|
|
|
|
Keypad5,
|
|
|
|
|
Keypad6,
|
|
|
|
|
Keypad7,
|
|
|
|
|
Keypad8,
|
|
|
|
|
Keypad9,
|
|
|
|
|
KeypadDivide,
|
|
|
|
|
KeypadMultiply,
|
|
|
|
|
KeypadSubtract,
|
|
|
|
|
KeypadAdd,
|
|
|
|
|
KeypadDecimal,
|
2008-03-10 09:49:48 +01:00
|
|
|
|
KeypadEnter,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
|
|
|
|
|
// Letters
|
|
|
|
|
A, B, C, D, E, F, G,
|
|
|
|
|
H, I, J, K, L, M, N,
|
|
|
|
|
O, P, Q, R, S, T, U,
|
|
|
|
|
V, W, X, Y, Z,
|
|
|
|
|
|
|
|
|
|
// Numbers
|
|
|
|
|
Number0,
|
|
|
|
|
Number1,
|
|
|
|
|
Number2,
|
|
|
|
|
Number3,
|
|
|
|
|
Number4,
|
|
|
|
|
Number5,
|
|
|
|
|
Number6,
|
|
|
|
|
Number7,
|
|
|
|
|
Number8,
|
|
|
|
|
Number9,
|
|
|
|
|
|
|
|
|
|
// Symbols
|
2007-08-03 02:14:31 +02:00
|
|
|
|
Tilde,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
Minus,
|
2007-08-03 02:14:31 +02:00
|
|
|
|
//Equal,
|
|
|
|
|
Plus,
|
2007-08-06 02:08:51 +02:00
|
|
|
|
BracketLeft,
|
|
|
|
|
BracketRight,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
Semicolon,
|
2007-08-03 02:14:31 +02:00
|
|
|
|
Quote,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
Comma,
|
2007-08-03 02:14:31 +02:00
|
|
|
|
Period,
|
2007-07-27 00:56:55 +02:00
|
|
|
|
Slash,
|
|
|
|
|
BackSlash,
|
|
|
|
|
|
|
|
|
|
MaxKeys
|
|
|
|
|
}
|
2007-08-03 02:14:31 +02:00
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|