Added KeyDown and KeyUp events to IKeyboard and Keyboard.

Improved input logger test to hook on the aforementioned events.
Removed Debug printing of keys in OpenTK.Input.Keyboard.
Beautified X11Keyboard.cs
Added experimental code to handle X11 keyboard events (not used yet).
This commit is contained in:
the_fiddler 2007-08-20 13:45:04 +00:00
parent 2254388903
commit 33527aa2c9
5 changed files with 81 additions and 19 deletions

View file

@ -60,6 +60,24 @@ namespace Examples.Tests
public S02_RawInput_Logger()
{
this.CreateWindow(new OpenTK.Platform.DisplayMode(100, 100));
foreach (OpenTK.Input.Keyboard k in this.Keyboard)
{
k.KeyDown += new OpenTK.Input.KeyDownEvent(LogKeyDown);
k.KeyUp += new OpenTK.Input.KeyUpEvent(LogKeyUp);
}
}
void LogKeyDown(object sender, OpenTK.Input.Key key)
{
Trace.WriteLine(String.Format("OpenTK key {0} pressed on Keyboard: ({1}).",
key, sender as OpenTK.Input.Keyboard));
}
void LogKeyUp(object sender, OpenTK.Input.Key key)
{
Trace.WriteLine(String.Format("OpenTK key {0} released on Keyboard: ({1}).",
key, sender as OpenTK.Input.Keyboard));
}
public override void OnLoad(EventArgs e)
@ -75,6 +93,7 @@ namespace Examples.Tests
GL.Clear(GL.Enums.ClearBufferMask.COLOR_BUFFER_BIT);
Context.SwapBuffers();
Thread.Sleep(1);
}
}
}

View file

@ -13,5 +13,17 @@ namespace OpenTK.Input
int NumberOfFunctionKeys { get; }
int NumberOfLeds { get; }
long DeviceID { get; }
event KeyDownEvent KeyDown;
event KeyUpEvent KeyUp;
}
public delegate void KeyDownEvent(object sender, Key key);
public delegate void KeyUpEvent(object sender, Key key);
//public class KeyEventArgs : System.EventArgs
//{
// private Key key;
// public Key Key { get { return key; } }
//}
}

View file

@ -15,7 +15,7 @@ using System.Diagnostics;
namespace OpenTK.Input
{
public class Keyboard : IKeyboard
public sealed class Keyboard : IKeyboard
{
//private IKeyboard keyboard;
private bool[] keys = new bool[(int)Key.MaxKeys];
@ -38,8 +38,16 @@ namespace OpenTK.Input
get { return keys[(int)k]; }
internal set
{
Debug.Print("OpenTK key {0} {1}.", k, value ? "pressed" : "released");
keys[(int)k] = value;
if (value && KeyDown != null)
{
KeyDown(this, k);
}
else if (!value && KeyUp != null)
{
KeyUp(this, k);
}
}
}
@ -70,6 +78,16 @@ namespace OpenTK.Input
internal set { devID = value; }
}
/// <summary>
/// Occurs when a key is pressed.
/// </summary>
public event KeyDownEvent KeyDown;
/// <summary>
/// Occurs when a key is released.
/// </summary>
public event KeyUpEvent KeyUp;
#endregion
#region --- IInputDevice Members ---
@ -87,6 +105,8 @@ namespace OpenTK.Input
#endregion
#region --- Public Methods ---
public override int GetHashCode()
{
//return base.GetHashCode();
@ -96,9 +116,11 @@ namespace OpenTK.Input
public override string ToString()
{
//return base.ToString();
return String.Format("Keyboard: '{0}', {1} keys, {2} function keys, {3} leds. Device ID: {4}",
description, NumberOfKeys, NumberOfFunctionKeys, NumberOfLeds, DeviceID);
return String.Format("ID: {0} (keys: {1}, function keys: {2}, leds: {3}",
DeviceID, NumberOfKeys, NumberOfFunctionKeys, NumberOfLeds);
}
#endregion
}
#region public enum Key : int

View file

@ -133,6 +133,16 @@ namespace OpenTK.Platform.X11
{
keyboardDriver.ProcessKeyboardEvent(e.KeyEvent);
}
/*
if (API.Pending(window.Display) > 0)
{
Functions.XPeekEvent(window.Display, ref e);
if (e.type == XEventName.KeyRelease || e.type == XEventName.KeyPress)
{
keyboardDriver.ProcessKeyboardEvent(e.KeyEvent);
}
}
*/
}
#endregion

View file

@ -204,21 +204,20 @@ namespace OpenTK.Platform.X11
switch (keysym.ToInt64())
{
default:
if (keymap.ContainsKey((XKey)keysym))
{
keyboards[0][keymap[(XKey)keysym]] = pressed;
}
else if (keymap.ContainsKey((XKey)keysym2))
{
keyboards[0][keymap[(XKey)keysym2]] = pressed;
}
else
{
Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2);
return false;
}
return true;
if (keymap.ContainsKey((XKey)keysym))
{
keyboards[0][keymap[(XKey)keysym]] = pressed;
}
else if (keymap.ContainsKey((XKey)keysym2))
{
keyboards[0][keymap[(XKey)keysym2]] = pressed;
}
else
{
Debug.Print("KeyCode {0} (Keysym: {1}, {2}) not mapped.", e.keycode, (XKey)keysym, (XKey)keysym2);
return false;
}
return true;
}
}