#region --- License ---
/* Copyright (c) 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
#region --- Using directives ---
using System;
using OpenTK.Input;
using System.Diagnostics;
#endregion
namespace OpenTK.Input
{
///
/// Represents a keyboard device and provides methods to query its status.
///
public sealed class KeyboardDevice : IInputDevice
{
//private IKeyboard keyboard;
private bool[] keys = new bool[Enum.GetValues(typeof(Key)).Length];
private string description;
private int numKeys, numFKeys, numLeds;
private IntPtr devID;
private bool repeat;
#region --- Constructors ---
internal KeyboardDevice() { }
#endregion
#region --- IKeyboard members ---
///
/// Gets a value indicating the status of the specified Key.
///
/// The Key to check.
/// True if the Key is pressed, false otherwise.
public bool this[Key key]
{
get { return keys[(int)key]; }
internal set
{
if (keys[(int)key] != value || KeyRepeat)
{
keys[(int)key] = value;
if (value && KeyDown != null)
KeyDown(this, key);
else if (!value && KeyUp != null)
KeyUp(this, key);
}
}
}
///
/// Gets an integer representing the number of keys on this KeyboardDevice.
///
public int NumberOfKeys
{
get { return numKeys; }
internal set { numKeys = value; }
}
///
/// Gets an integer representing the number of function keys (F-keys) on this KeyboardDevice.
///
public int NumberOfFunctionKeys
{
get { return numFKeys; }
internal set { numFKeys = value; }
}
///
/// Gets a value indicating the number of led indicators on this KeyboardDevice.
///
public int NumberOfLeds
{
get { return numLeds; }
internal set { numLeds = value; }
}
///
/// Gets an IntPtr representing a device dependent ID.
///
public IntPtr DeviceID
{
get { return devID; }
internal set { devID = value; }
}
#region public bool KeyRepeat
///
/// Gets or sets a System.Boolean indicating key repeat status.
///
///
/// If KeyRepeat is true, multiple KeyDown events will be generated while a key is being held.
/// Otherwise only one KeyDown event will be reported.
///
/// 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).
///
///
/// Set to true to handle text input (where keyboard repeat is desirable), but set to false
/// for game input.
///
///
public bool KeyRepeat
{
get { return repeat; }
set { repeat = value; }
}
#endregion
#region public event KeyDownEvent KeyDown;
///
/// Occurs when a key is pressed.
///
public event KeyDownEvent KeyDown;
#endregion
#region public event KeyUpEvent KeyUp;
///
/// Occurs when a key is released.
///
public event KeyUpEvent KeyUp;
#endregion
#endregion
#region --- IInputDevice Members ---
public string Description
{
get { return description; }
internal set { description = value; }
}
public InputDeviceType DeviceType
{
get { return InputDeviceType.Keyboard; }
}
#endregion
#region --- Public Methods ---
/// Returns the hash code for this KeyboardDevice.
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
//return base.GetHashCode();
return (int)(numKeys ^ numFKeys ^ numLeds ^ devID.GetHashCode() ^ description.GetHashCode());
}
///
/// Returns a System.String representing this KeyboardDevice.
///
/// A System.String representing this KeyboardDevice.
public override string ToString()
{
//return base.ToString();
return String.Format("ID: {0} ({1}). Keys: {2}, Function keys: {3}, Leds: {4}",
DeviceID, Description, NumberOfKeys, NumberOfFunctionKeys, NumberOfLeds);
}
#endregion
#region --- Internal Methods ---
#region internal void ClearKeys()
internal void ClearKeys()
{
for (int i = 0; i < keys.Length; i++)
if (this[(Key)i]) // Make sure KeyUp events are *not* raised for keys that are up, even if key repeat is on.
this[(Key)i] = false;
}
#endregion
#endregion
}
[Obsolete]
public delegate void KeyDownEvent(KeyboardDevice sender, Key key);
[Obsolete]
public delegate void KeyUpEvent(KeyboardDevice sender, Key key);
#region public enum Key : int
///
/// The available keyboard keys.
///
public enum Key : int
{
/// A key outside the known keys.
Unknown = 0,
// Modifiers
/// The left shift key.
ShiftLeft,
/// The left shift key (equivalent to ShiftLeft).
LShift = ShiftLeft,
/// The right shift key.
ShiftRight,
/// The right shift key (equivalent to ShiftRight).
RShift = ShiftRight,
/// The left control key.
ControlLeft,
/// The left control key (equivalent to ControlLeft).
LControl = ControlLeft,
/// The right control key.
ControlRight,
/// The right control key (equivalent to ControlRight).
RControl = ControlRight,
/// The left alt key.
AltLeft,
/// The left alt key (equivalent to AltLeft.
LAlt = AltLeft,
/// The right alt key.
AltRight,
/// The right alt key (equivalent to AltRight).
RAlt = AltRight,
/// The left win key.
WinLeft,
/// The left win key (equivalent to WinLeft).
LWin = WinLeft,
/// The right win key.
WinRight,
/// The right win key (equivalent to WinRight).
RWin = WinRight,
/// The menu key.
Menu,
// Function keys (hopefully enough for most keyboards - mine has 26)
// on X11 reports up to 35 function keys.
/// The F1 key.
F1,
/// The F2 key.
F2,
/// The F3 key.
F3,
/// The F4 key.
F4,
/// The F5 key.
F5,
/// The F6 key.
F6,
/// The F7 key.
F7,
/// The F8 key.
F8,
/// The F9 key.
F9,
/// The F10 key.
F10,
/// The F11 key.
F11,
/// The F12 key.
F12,
/// The F13 key.
F13,
/// The F14 key.
F14,
/// The F15 key.
F15,
/// The F16 key.
F16,
/// The F17 key.
F17,
/// The F18 key.
F18,
/// The F19 key.
F19,
/// The F20 key.
F20,
/// The F21 key.
F21,
/// The F22 key.
F22,
/// The F23 key.
F23,
/// The F24 key.
F24,
/// The F25 key.
F25,
/// The F26 key.
F26,
/// The F27 key.
F27,
/// The F28 key.
F28,
/// The F29 key.
F29,
/// The F30 key.
F30,
/// The F31 key.
F31,
/// The F32 key.
F32,
/// The F33 key.
F33,
/// The F34 key.
F34,
/// The F35 key.
F35,
// Direction arrows
/// The up arrow key.
Up,
/// The down arrow key.
Down,
/// The left arrow key.
Left,
/// The right arrow key.
Right,
/// The enter key.
Enter,
/// The escape key.
Escape,
/// The space key.
Space,
/// The tab key.
Tab,
/// The backspace key.
BackSpace,
/// The backspace key (equivalent to BackSpace).
Back = BackSpace,
/// The insert key.
Insert,
/// The delete key.
Delete,
/// The page up key.
PageUp,
/// The page down key.
PageDown,
/// The home key.
Home,
/// The end key.
End,
/// The caps lock key.
CapsLock,
/// The scroll lock key.
ScrollLock,
/// The print screen key.
PrintScreen,
/// The pause key.
Pause,
/// The num lock key.
NumLock,
// Special keys
/// The clear key (Keypad5 with NumLock disabled, on typical keyboards).
Clear,
/// The sleep key.
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,
VolumeDown,
TrackPrevious,
TrackNext,*/
// Keypad keys
/// The keypad 0 key.
Keypad0,
/// The keypad 1 key.
Keypad1,
/// The keypad 2 key.
Keypad2,
/// The keypad 3 key.
Keypad3,
/// The keypad 4 key.
Keypad4,
/// The keypad 5 key.
Keypad5,
/// The keypad 6 key.
Keypad6,
/// The keypad 7 key.
Keypad7,
/// The keypad 8 key.
Keypad8,
/// The keypad 9 key.
Keypad9,
/// The keypad divide key.
KeypadDivide,
/// The keypad multiply key.
KeypadMultiply,
/// The keypad subtract key.
KeypadSubtract,
/// The keypad minus key (equivalent to KeypadSubtract).
KeypadMinus = KeypadSubtract,
/// The keypad add key.
KeypadAdd,
/// The keypad plus key (equivalent to KeypadAdd).
KeypadPlus = KeypadAdd,
/// The keypad decimal key.
KeypadDecimal,
/// The keypad enter key.
KeypadEnter,
// Letters
/// The A key.
A,
/// The B key.
B,
/// The C key.
C,
/// The D key.
D,
/// The E key.
E,
/// The F key.
F,
/// The G key.
G,
/// The H key.
H,
/// The I key.
I,
/// The J key.
J,
/// The K key.
K,
/// The L key.
L,
/// The M key.
M,
/// The N key.
N,
/// The O key.
O,
/// The P key.
P,
/// The Q key.
Q,
/// The R key.
R,
/// The S key.
S,
/// The T key.
T,
/// The U key.
U,
/// The V key.
V,
/// The W key.
W,
/// The X key.
X,
/// The Y key.
Y,
/// The Z key.
Z,
// Numbers
/// The number 0 key.
Number0,
/// The number 1 key.
Number1,
/// The number 2 key.
Number2,
/// The number 3 key.
Number3,
/// The number 4 key.
Number4,
/// The number 5 key.
Number5,
/// The number 6 key.
Number6,
/// The number 7 key.
Number7,
/// The number 8 key.
Number8,
/// The number 9 key.
Number9,
// Symbols
/// The tilde key.
Tilde,
/// The minus key.
Minus,
//Equal,
/// The plus key.
Plus,
/// The left bracket key.
BracketLeft,
/// The left bracket key (equivalent to BracketLeft).
LBracket = BracketLeft,
/// The right bracket key.
BracketRight,
/// The right bracket key (equivalent to BracketRight).
RBracket = BracketRight,
/// The semicolon key.
Semicolon,
/// The quote key.
Quote,
/// The comma key.
Comma,
/// The period key.
Period,
/// The slash key.
Slash,
/// The backslash key.
BackSlash,
}
#endregion
}