/* Copyright (c) 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
using System;
namespace OpenTK.Input
{
///
/// Represents a keyboard device and provides methods to query its status.
///
public sealed class KeyboardDevice : IInputDevice
{
//private IKeyboard keyboard;
private IntPtr devID;
private KeyboardState state;
internal KeyboardDevice() { }
///
/// 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 state[key]; }
}
///
/// Gets a value indicating the status of the specified Key.
///
/// The scancode to check.
/// True if the scancode is pressed, false otherwise.
[CLSCompliant(false)]
public bool this[uint scancode]
{
get { return scancode < (uint)Key.LastKey && state[(Key)scancode]; }
}
///
/// Gets an integer representing the number of keys on this KeyboardDevice.
///
public int NumberOfKeys { get; internal set; }
///
/// Gets an integer representing the number of function keys (F-keys) on this KeyboardDevice.
///
public int NumberOfFunctionKeys { get; internal set; }
///
/// Gets a value indicating the number of led indicators on this KeyboardDevice.
///
public int NumberOfLeds { get; internal set; }
///
/// Gets an IntPtr representing a device dependent ID.
///
public IntPtr DeviceID
{
get { return devID; }
internal set { devID = value; }
}
///
/// 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; set; }
///
/// Occurs when a key is pressed.
///
public event EventHandler KeyDown = delegate { };
///
/// Occurs when a key is released.
///
public event EventHandler KeyUp = delegate { };
///
/// Gets a which describes this instance.
///
public string Description { get; internal set; }
///
/// Gets the for this instance.
///
public InputDeviceType DeviceType
{
get { return InputDeviceType.Keyboard; }
}
///
/// Retrieves the combined for all keyboard devices.
/// This method is equivalent to .
///
/// An structure containing the combined state for all keyboard devices.
///
public KeyboardState GetState()
{
return Keyboard.GetState();
}
///
/// Retrieves the for the specified keyboard device.
/// This method is equivalent to .
///
/// The index of the keyboard device.
/// An structure containing the combined state for all keyboard devices.
///
public KeyboardState GetState(int index)
{
return Keyboard.GetState(index);
}
/// Returns the hash code for this KeyboardDevice.
/// A 32-bit signed integer hash code.
public override int GetHashCode()
{
//return base.GetHashCode();
return (int)(NumberOfKeys ^ NumberOfFunctionKeys ^ NumberOfLeds ^ 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);
}
internal void HandleKeyDown(object sender, KeyboardKeyEventArgs e)
{
state = e.Keyboard;
// KeyRepeat IsRepeat KeyDown
// False False True
// False True False
// True False True
// True True True
if (this.KeyRepeat || !e.IsRepeat)
{
KeyDown(this, e);
}
}
internal void HandleKeyUp(object sender, KeyboardKeyEventArgs e)
{
state = e.Keyboard;
KeyUp(this, e);
}
}
}