[Input] Legacy keyboard respects the KeyRepeat field.

If the legacy keyboard device receives a key down event with IsRepeat set it
will only raise it's own key down event if it's KeyRepeat field is set to true.

This is as documented, regression casused by refactoring. Fixes issue #201.

Also change the GameWindowState example to show setting of KeyRepeat to true
and false and how that changes the event counts for the legacy and new keyboard
devices.
This commit is contained in:
Fraser Waters 2014-11-23 14:36:41 +01:00
parent 6deec73fc9
commit e855d2eb33
2 changed files with 20 additions and 38 deletions

View file

@ -70,7 +70,7 @@ namespace Examples.Tests
: base(800, 600, GraphicsMode.Default)
{
VSync = VSyncMode.On;
Keyboard.KeyRepeat = true;
Keyboard.KeyRepeat = false;
KeyDown += KeyDownHandler;
KeyUp += KeyUpHandler;
KeyPress += KeyPressHandler;
@ -159,22 +159,26 @@ namespace Examples.Tests
p = PointToScreen(p);
OpenTK.Input.Mouse.SetPosition(p.X, p.Y);
break;
case Key.R:
Keyboard.KeyRepeat = !Keyboard.KeyRepeat;
break;
}
if (!keyboard_keys.ContainsKey(e.Key))
{
keyboard_keys.Add(e.Key, 0);
}
keyboard_keys[e.Key] = e.IsRepeat ? 1 : 0;
keyboard_keys[e.Key] = keyboard_keys[e.Key] + 1;
keyboard_modifiers = e.Modifiers;
keyboard_state = e.Keyboard;
}
void KeyUpHandler(object sender, KeyboardKeyEventArgs e)
{
keyboard_keys.Remove(e.Key);
keyboard_modifiers = e.Modifiers;
keyboard_state = e.Keyboard;
keyboard_keys.Remove(e.Key);
keyboard_modifiers = e.Modifiers;
keyboard_state = e.Keyboard;
}
void KeyboardDeviceDownHandler(object sender, KeyboardKeyEventArgs e)
@ -183,14 +187,14 @@ namespace Examples.Tests
{
legacy_keyboard_keys.Add(e.Key, 0);
}
legacy_keyboard_keys[e.Key] = e.IsRepeat ? 1 : 0;
legacy_keyboard_keys[e.Key] = legacy_keyboard_keys[e.Key] + 1;
legacy_keyboard_modifiers = e.Modifiers;
legacy_keyboard_state = e.Keyboard;
}
void KeyboardDeviceUpHandler(object sender, KeyboardKeyEventArgs e)
{
legacy_keyboard_keys.Remove(e.Key);
legacy_keyboard_keys.Remove(e.Key);
legacy_keyboard_modifiers = e.Modifiers;
legacy_keyboard_state = e.Keyboard;
}

View file

@ -210,7 +210,15 @@ namespace OpenTK.Input
internal void HandleKeyDown(object sender, KeyboardKeyEventArgs e)
{
state = e.Keyboard;
KeyDown(this, e);
// 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)
@ -219,36 +227,6 @@ namespace OpenTK.Input
KeyUp(this, e);
}
#if false
internal void SetKey(Key key, uint scancode, KeyModifiers mods, bool pressed)
{
if (state[key] != pressed || KeyRepeat)
{
// limit scancode to 8bits, otherwise the assignment
// below will crash randomly
scancode &= 0xff;
keys[(int)key] = scancodes[scancode] = state;
if (state && KeyDown != null)
{
args.Key = key;
args.ScanCode = scancode;
args.Modifiers = mods;
KeyDown(this, args);
}
else if (!state && KeyUp != null)
{
args.Key = key;
args.ScanCode = scancode;
args.Modifiers = mods;
KeyUp(this, args);
}
}
}
#endif
#endregion
}
}