[OpenTK] Refactor keyboard and mouse handling

A lot of duplicated code is now moved to NativeWindowBase and
LegacyInputDriver.
This commit is contained in:
thefiddler 2014-05-04 17:05:08 +02:00
parent e155d647de
commit d968281a1b
11 changed files with 182 additions and 280 deletions

View file

@ -21,13 +21,11 @@ namespace OpenTK.Input
public sealed class KeyboardDevice : IInputDevice
{
//private IKeyboard keyboard;
private bool[] keys = new bool[(int)Key.LastKey];
private bool[] scancodes = new bool[256];
private string description;
private int numKeys, numFKeys, numLeds;
private IntPtr devID;
private bool repeat;
private KeyboardKeyEventArgs args = new KeyboardKeyEventArgs();
private KeyboardState state;
#region --- Constructors ---
@ -44,7 +42,7 @@ namespace OpenTK.Input
/// <returns>True if the Key is pressed, false otherwise.</returns>
public bool this[Key key]
{
get { return keys[(int)key]; }
get { return state[key]; }
}
/// <summary>
@ -52,9 +50,10 @@ namespace OpenTK.Input
/// </summary>
/// <param name="scancode">The scancode to check.</param>
/// <returns>True if the scancode is pressed, false otherwise.</returns>
[CLSCompliant(false)]
public bool this[uint scancode]
{
get { return scancodes[scancode]; }
get { return scancode < (uint)Key.LastKey && state[(Key)scancode]; }
}
/// <summary>
@ -124,7 +123,7 @@ namespace OpenTK.Input
/// <summary>
/// Occurs when a key is pressed.
/// </summary>
public event EventHandler<KeyboardKeyEventArgs> KeyDown;
public event EventHandler<KeyboardKeyEventArgs> KeyDown = delegate { };
#endregion
@ -133,7 +132,7 @@ namespace OpenTK.Input
/// <summary>
/// Occurs when a key is released.
/// </summary>
public event EventHandler<KeyboardKeyEventArgs> KeyUp;
public event EventHandler<KeyboardKeyEventArgs> KeyUp = delegate { };
#endregion
@ -185,21 +184,28 @@ namespace OpenTK.Input
#region --- Internal Methods ---
#region internal void ClearKeys()
internal void HandleKeyDown(object sender, KeyboardKeyEventArgs e)
{
state = e.Keyboard;
KeyDown(this, e);
}
internal void HandleKeyUp(object sender, KeyboardKeyEventArgs e)
{
state = e.Keyboard;
KeyUp(this, e);
}
internal void ClearKeys()
{
for (int i = 0; i < keys.Length; i++)
keys[i] = false;
for (uint i = 0; i < scancodes.Length; i++)
scancodes[i] = false;
for (Key i = 0; i < Key.LastKey; i++)
state[i] = false;
}
#endregion
internal void SetKey(Key key, uint scancode, KeyModifiers mods, bool state)
#if false
internal void SetKey(Key key, uint scancode, KeyModifiers mods, bool pressed)
{
if (keys[(int)key] != state || KeyRepeat)
if (state[key] != pressed || KeyRepeat)
{
// limit scancode to 8bits, otherwise the assignment
// below will crash randomly
@ -209,6 +215,7 @@ namespace OpenTK.Input
if (state && KeyDown != null)
{
args.Key = key;
args.ScanCode = scancode;
args.Modifiers = mods;
@ -223,28 +230,7 @@ namespace OpenTK.Input
}
}
}
internal KeyModifiers GetModifiers()
{
KeyModifiers mods = 0;
if (this[Key.AltLeft] || this[Key.AltRight])
{
mods |= KeyModifiers.Alt;
}
if (this[Key.ControlLeft] || this[Key.ControlRight])
{
mods |= KeyModifiers.Control;
}
if (this[Key.ShiftLeft] || this[Key.ShiftRight])
{
mods |= KeyModifiers.Shift;
}
return mods;
}
#endif
#endregion
}

View file

@ -46,7 +46,7 @@ namespace OpenTK.Input
#region Fields
Key key;
KeyModifiers mods;
KeyboardState state;
uint scancode;
#endregion
@ -97,7 +97,7 @@ namespace OpenTK.Input
/// <value><c>true</c> if pressed; otherwise, <c>false</c>.</value>
public bool Alt
{
get { return (mods & KeyModifiers.Alt) != 0; }
get { return state[Key.AltLeft] || state[Key.AltRight]; }
}
/// <summary>
@ -106,7 +106,7 @@ namespace OpenTK.Input
/// <value><c>true</c> if pressed; otherwise, <c>false</c>.</value>
public bool Control
{
get { return (mods & KeyModifiers.Control) != 0; }
get { return state[Key.ControlLeft] || state[Key.ControlRight]; }
}
/// <summary>
@ -115,7 +115,7 @@ namespace OpenTK.Input
/// <value><c>true</c> if pressed; otherwise, <c>false</c>.</value>
public bool Shift
{
get { return (mods & KeyModifiers.Shift) != 0; }
get { return state[Key.ShiftLeft] || state[Key.ShiftRight]; }
}
/// <summary>
@ -125,8 +125,24 @@ namespace OpenTK.Input
/// <value>The modifiers.</value>
public KeyModifiers Modifiers
{
get { return mods; }
internal set { mods = value; }
get
{
KeyModifiers mods = 0;
mods |= Alt ? KeyModifiers.Alt : 0;
mods |= Control ? KeyModifiers.Control : 0;
mods |= Shift ? KeyModifiers.Shift : 0;
return mods;
}
}
/// <summary>
/// Gets the current <see cref="OpenTK.Input.KeyboardState"/>.
/// </summary>
/// <value>The keyboard.</value>
public KeyboardState Keyboard
{
get { return state; }
internal set { state = value; }
}
#endregion

View file

@ -43,9 +43,6 @@ namespace OpenTK.Input
const int NumInts = ((int)Key.LastKey + IntSize - 1) / IntSize;
// The following line triggers bogus CS0214 in gmcs 2.0.1, sigh...
unsafe fixed int Keys[NumInts];
const int CodesSize = 256;
unsafe fixed int Codes[CodesSize];
bool is_connected;
#endregion
@ -61,6 +58,7 @@ namespace OpenTK.Input
public bool this[Key key]
{
get { return IsKeyDown(key); }
internal set { SetKeyState(key, value); }
}
/// <summary>
@ -71,7 +69,7 @@ namespace OpenTK.Input
/// <returns>True if code is pressed; false otherwise.</returns>
public bool this[short code]
{
get { return IsKeyDown(code); }
get { return IsKeyDown((Key)code); }
}
/// <summary>
@ -89,7 +87,7 @@ namespace OpenTK.Input
/// <param name="code">The scan code to check.</param>
public bool IsKeyDown(short code)
{
return ReadBit(code,true);
return code >= 0 && code < (short)Key.LastKey && ReadBit(code);
}
/// <summary>
@ -107,7 +105,7 @@ namespace OpenTK.Input
/// <param name="code">The scan code to check.</param>
public bool IsKeyUp(short code)
{
return !ReadBit(code,true);
return !IsKeyDown(code);
}
/// <summary>
@ -212,62 +210,51 @@ namespace OpenTK.Input
#region Internal Members
internal void SetKeyState(Key key, byte code, bool down)
internal void SetKeyState(Key key, bool down)
{
if (down)
{
EnableBit((int)key);
EnableBit(code,true);
}
else
{
DisableBit((int)key);
DisableBit(code, true);
}
}
internal bool ReadBit(int offset, bool ScanCode = false)
internal bool ReadBit(int offset)
{
ValidateOffset(offset, ScanCode);
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
{
if (ScanCode)
fixed (int* c = Codes) { return (*(c + int_offset) & (1 << bit_offset)) != 0u; }
else
fixed (int* k = Keys) { return (*(k + int_offset) & (1 << bit_offset)) != 0u; }
fixed (int* k = Keys) { return (*(k + int_offset) & (1 << bit_offset)) != 0u; }
}
}
internal void EnableBit(int offset, bool ScanCode = false)
internal void EnableBit(int offset)
{
ValidateOffset(offset, ScanCode);
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
{
if (ScanCode)
fixed (int* c = Codes) { *(c + int_offset) |= 1 << bit_offset; }
else
fixed (int* k = Keys) { *(k + int_offset) |= 1 << bit_offset; }
fixed (int* k = Keys) { *(k + int_offset) |= 1 << bit_offset; }
}
}
internal void DisableBit(int offset, bool ScanCode = false)
internal void DisableBit(int offset)
{
ValidateOffset(offset, ScanCode);
ValidateOffset(offset);
int int_offset = offset / 32;
int bit_offset = offset % 32;
unsafe
{
if (ScanCode)
fixed (int* c = Codes) { *(c + int_offset) &= ~(1 << bit_offset); }
else
fixed (int* k = Keys) { *(k + int_offset) &= ~(1 << bit_offset); }
fixed (int* k = Keys) { *(k + int_offset) &= ~(1 << bit_offset); }
}
}
@ -281,12 +268,6 @@ namespace OpenTK.Input
for (int i = 0; i < NumInts; i++)
*(k1 + i) |= *(k2 + i);
}
int* c2 = other.Codes;
fixed (int* c1 = Codes)
{
for (int i = 0; i < CodesSize; i++)
*(c1 + i) |= *(c2 + i);
}
}
IsConnected |= other.IsConnected;
}
@ -300,10 +281,10 @@ namespace OpenTK.Input
#region Private Members
static void ValidateOffset(int offset, bool ScanCode)
static void ValidateOffset(int offset)
{
if (offset < 0 || offset >= (ScanCode ? 256 : NumInts * IntSize))
throw new ArgumentOutOfRangeException("offset");
if (offset < 0 || offset >= NumInts * IntSize)
throw new ArgumentOutOfRangeException();
}
#endregion

View file

@ -136,8 +136,6 @@ namespace OpenTK.Platform.MacOS
private Nullable<WindowBorder> deferredWindowBorder;
private Nullable<WindowBorder> previousWindowBorder;
private WindowState windowState = WindowState.Normal;
private OpenTK.Input.KeyboardKeyEventArgs keyArgs = new OpenTK.Input.KeyboardKeyEventArgs();
private KeyPressEventArgs keyPressArgs = new KeyPressEventArgs((char)0);
private string title;
private RectangleF previousBounds;
private int normalLevel;
@ -387,13 +385,6 @@ namespace OpenTK.Platform.MacOS
return modifiers;
}
private void GetKey(ushort keyCode, NSEventModifierMask modifierFlags, OpenTK.Input.KeyboardKeyEventArgs args)
{
args.Key = MacOSKeyMap.GetKey((Carbon.MacOSKeyCode)keyCode);
args.Modifiers = GetModifiers(modifierFlags);
args.ScanCode = (uint)keyCode;
}
private MouseButton GetMouseButton(int cocoaButtonIndex)
{
if (cocoaButtonIndex == 0) return MouseButton.Left;
@ -419,14 +410,15 @@ namespace OpenTK.Platform.MacOS
{
case NSEventType.KeyDown:
{
var keyCode = Cocoa.SendUshort(e, selKeyCode);
var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
MacOSKeyCode keyCode = (MacOSKeyCode)Cocoa.SendUshort(e, selKeyCode);
//var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
var isARepeat = Cocoa.SendBool(e, selIsARepeat);
GetKey(keyCode, modifierFlags, keyArgs);
//GetKey(keyCode, modifierFlags, keyArgs);
Key key = MacOSKeyMap.GetKey(keyCode);
if (!isARepeat || InputDriver.Keyboard[0].KeyRepeat)
{
OnKeyDown(keyArgs);
OnKeyDown(key);
}
var s = Cocoa.FromNSString(Cocoa.SendIntPtr(e, selCharactersIgnoringModifiers));
@ -435,10 +427,9 @@ namespace OpenTK.Platform.MacOS
int intVal = (int)c;
if (!Char.IsControl(c) && (intVal < 63232 || intVal > 63235))
{
// For some reason, arrow keys (mapped 63232-63235) are seen as non-control characters, so get rid of those.
keyPressArgs.KeyChar = c;
OnKeyPress(keyPressArgs);
// For some reason, arrow keys (mapped 63232-63235)
// are seen as non-control characters, so get rid of those.
OnKeyPress(c);
}
}
}
@ -446,11 +437,11 @@ namespace OpenTK.Platform.MacOS
case NSEventType.KeyUp:
{
var keyCode = Cocoa.SendUshort(e, selKeyCode);
var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
GetKey(keyCode, modifierFlags, keyArgs);
OnKeyUp(keyArgs);
MacOSKeyCode keyCode = (MacOSKeyCode)Cocoa.SendUshort(e, selKeyCode);
//var modifierFlags = (NSEventModifierMask)Cocoa.SendUint(e, selModifierFlags);
//GetKey(keyCode, modifierFlags, keyArgs);
Key key = MacOSKeyMap.GetKey(keyCode);
OnKeyUp(key);
}
break;
@ -521,9 +512,7 @@ namespace OpenTK.Platform.MacOS
// Only raise events when the mouse has actually moved
if (MouseState.X != p.X || MouseState.Y != p.Y)
{
MouseState.X = p.X;
MouseState.Y = p.Y;
OnMouseMove();
OnMouseMove(p.X, p.Y);
}
}
break;
@ -548,8 +537,7 @@ namespace OpenTK.Platform.MacOS
// Only raise wheel events when the user has actually scrolled
if (dx != 0 || dy != 0)
{
MouseState.SetScrollRelative(dx, dy);
OnMouseWheel();
OnMouseWheel(dx, dy);
}
}
break;
@ -559,8 +547,7 @@ namespace OpenTK.Platform.MacOS
case NSEventType.OtherMouseDown:
{
var buttonNumber = Cocoa.SendInt(e, selButtonNumber);
MouseState[GetMouseButton(buttonNumber)] = true;
OnMouseDown();
OnMouseDown(GetMouseButton(buttonNumber));
}
break;
@ -569,8 +556,7 @@ namespace OpenTK.Platform.MacOS
case NSEventType.OtherMouseUp:
{
var buttonNumber = Cocoa.SendInt(e, selButtonNumber);
MouseState[GetMouseButton(buttonNumber)] = false;
OnMouseUp();
OnMouseUp(GetMouseButton(buttonNumber));
}
break;
}

View file

@ -390,7 +390,8 @@ namespace OpenTK.Platform.MacOS
{
Debug.Print("[Warning] Key {0} not mapped.", usage);
}
keyboard.State.SetKeyState(RawKeyMap[usage], (byte)usage, v_int != 0);
keyboard.State[RawKeyMap[usage]] = v_int != 0;
break;
}
}

View file

@ -45,19 +45,22 @@ namespace OpenTK.Platform
readonly MouseMoveEventArgs MouseMoveArgs = new MouseMoveEventArgs();
readonly MouseWheelEventArgs MouseWheelArgs = new MouseWheelEventArgs();
protected readonly KeyboardKeyEventArgs KeyDownArgs = new KeyboardKeyEventArgs();
protected readonly KeyboardKeyEventArgs KeyUpArgs = new KeyboardKeyEventArgs();
protected readonly KeyPressEventArgs KeyPressArgs = new KeyPressEventArgs((char)0);
readonly KeyboardKeyEventArgs KeyDownArgs = new KeyboardKeyEventArgs();
readonly KeyboardKeyEventArgs KeyUpArgs = new KeyboardKeyEventArgs();
readonly KeyPressEventArgs KeyPressArgs = new KeyPressEventArgs((char)0);
// In order to simplify mouse event implementation,
// we can store the current mouse state here.
protected MouseState MouseState = new MouseState();
protected KeyboardState KeyboardState = new KeyboardState();
MouseState PreviousMouseState = new MouseState();
internal NativeWindowBase()
{
LegacyInputDriver = new LegacyInputDriver(this);
MouseState.SetIsConnected(true);
KeyboardState.SetIsConnected(true);
PreviousMouseState.SetIsConnected(true);
}
@ -118,18 +121,30 @@ namespace OpenTK.Platform
WindowStateChanged(this, e);
}
protected void OnKeyDown(KeyboardKeyEventArgs e)
protected void OnKeyDown(Key key)
{
KeyboardState.SetKeyState(key, true);
var e = KeyDownArgs;
e.Keyboard = KeyboardState;
e.Key = key;
KeyDown(this, e);
}
protected void OnKeyPress(KeyPressEventArgs e)
protected void OnKeyPress(char c)
{
var e = KeyPressArgs;
e.KeyChar = c;
KeyPress(this, e);
}
protected void OnKeyUp(KeyboardKeyEventArgs e)
protected void OnKeyUp(Key key)
{
KeyboardState.SetKeyState(key, false);
var e = KeyUpArgs;
e.Keyboard = KeyboardState;
e.Key = key;
KeyUp(this, e);
}
@ -143,48 +158,31 @@ namespace OpenTK.Platform
MouseEnter(this, e);
}
protected void OnMouseDown()
protected void OnMouseDown(MouseButton button)
{
MouseState[button] = true;
var e = MouseDownArgs;
e.Mouse = MouseState;
// Find which button caused this event
for (MouseButton b = MouseButton.Left; b < MouseButton.LastButton; b++)
{
if (!PreviousMouseState[b] && MouseState[b])
{
e.Button = b;
PreviousMouseState = MouseState;
MouseDown(this, e);
return;
}
}
Debug.WriteLine("OnMouseDown called without pressing a button");
MouseDown(this, e);
}
protected void OnMouseUp()
protected void OnMouseUp(MouseButton button)
{
MouseState[button] = false;
var e = MouseUpArgs;
e.Mouse = MouseState;
// Find which button caused this event
for (MouseButton b = MouseButton.Left; b < MouseButton.LastButton; b++)
{
if (PreviousMouseState[b] && !MouseState[b])
{
e.Button = b;
PreviousMouseState = MouseState;
MouseUp(this, e);
return;
}
}
Debug.WriteLine("OnMouseUp called without pressing a button");
MouseUp(this, e);
}
protected void OnMouseMove()
protected void OnMouseMove(int x, int y)
{
MouseState.X = x;
MouseState.Y = y;
var e = MouseMoveArgs;
e.Mouse = MouseState;
e.XDelta = MouseState.X - PreviousMouseState.X;
@ -199,14 +197,15 @@ namespace OpenTK.Platform
MouseMove(this, e);
}
protected void OnMouseWheel()
protected void OnMouseWheel(float dx, float dy)
{
MouseState.SetScrollRelative(dx, dy);
var e = MouseWheelArgs;
e.Mouse = MouseState;
e.ValuePrecise = MouseState.Scroll.Y;
e.DeltaPrecise = MouseState.Scroll.Y - PreviousMouseState.Scroll.Y;
if (e.DeltaPrecise == 0)
if (dx == 0 && dy == 0)
{
Debug.WriteLine("OnMouseWheel called without moving the mouse wheel.");
}

View file

@ -58,16 +58,16 @@ namespace OpenTK.Platform.SDL2
{
Keymod mod = SDL.GetModState();
state.SetKeyState(Key.LAlt, (byte)Scancode.LALT, (mod & Keymod.LALT) != 0);
state.SetKeyState(Key.RAlt, (byte)Scancode.RALT, (mod & Keymod.RALT) != 0);
state.SetKeyState(Key.LControl, (byte)Scancode.LCTRL, (mod & Keymod.LCTRL) != 0);
state.SetKeyState(Key.RControl, (byte)Scancode.RCTRL, (mod & Keymod.CTRL) != 0);
state.SetKeyState(Key.LShift, (byte)Scancode.LSHIFT, (mod & Keymod.LSHIFT) != 0);
state.SetKeyState(Key.RShift, (byte)Scancode.RSHIFT, (mod & Keymod.RSHIFT) != 0);
state.SetKeyState(Key.Menu, (byte)Scancode.APPLICATION, (mod & Keymod.GUI) != 0);
state.SetKeyState(Key.CapsLock, (byte)Scancode.CAPSLOCK, (mod & Keymod.CAPS) != 0);
state.SetKeyState(Key.NumLock, (byte)Scancode.NUMLOCKCLEAR, (mod & Keymod.NUM) != 0);
//state.SetKeyState(Key., (byte)Scancode.MODE, (mod & Keymod.MODE) != 0);
state[Key.LAlt] = (mod & Keymod.LALT) != 0;
state[Key.RAlt] = (mod & Keymod.RALT) != 0;
state[Key.LControl] = (mod & Keymod.LCTRL) != 0;
state[Key.RControl] = (mod & Keymod.RCTRL) != 0;
state[Key.LShift] = (mod & Keymod.LSHIFT) != 0;
state[Key.RShift] = (mod & Keymod.RSHIFT) != 0;
state[Key.Menu] = (mod & Keymod.GUI) != 0;
state[Key.CapsLock] = (mod & Keymod.CAPS) != 0;
state[Key.NumLock] = (mod & Keymod.NUM) != 0;
//state[Key.] = (mod & Keymod.MODE) != 0;
}
#endregion
@ -83,7 +83,7 @@ namespace OpenTK.Platform.SDL2
if (key != Key.Unknown)
{
state.SetKeyState(key, (byte)scancode, pressed);
state[key] = pressed;
}
}

View file

@ -66,12 +66,6 @@ namespace OpenTK.Platform.SDL2
// to .Net UTF16 strings
char[] DecodeTextBuffer = new char[32];
// Argument for KeyPress event (allocated once to avoid runtime allocations)
readonly KeyPressEventArgs keypress_args = new KeyPressEventArgs('\0');
// Argument for KeyDown and KeyUp events (allocated once to avoid runtime allocations)
readonly KeyboardKeyEventArgs key_args = new KeyboardKeyEventArgs();
static readonly Dictionary<uint, Sdl2NativeWindow> windows =
new Dictionary<uint, Sdl2NativeWindow>();
@ -221,36 +215,29 @@ namespace OpenTK.Platform.SDL2
button_pressed ? true : false);
}
window.MouseState[Sdl2Mouse.TranslateButton(ev.Button)] = button_pressed;
window.MouseState.X = ev.X;
window.MouseState.Y = ev.Y;
MouseButton button = Sdl2Mouse.TranslateButton(ev.Button);
if (button_pressed)
{
window.OnMouseDown();
window.OnMouseDown(button);
}
else
{
window.OnMouseUp();
window.OnMouseUp(button);
}
}
static void ProcessKeyEvent(Sdl2NativeWindow window, Event ev)
{
bool key_pressed = ev.Key.State == State.Pressed;
var key = ev.Key.Keysym;
window.key_args.Key = TranslateKey(key.Scancode);
window.key_args.ScanCode = (uint)key.Scancode;
window.key_args.Modifiers = Sdl2KeyMap.GetModifiers(key.Mod);
Key key = TranslateKey(ev.Key.Keysym.Scancode);
if (key_pressed)
{
window.OnKeyDown(window.key_args);
window.OnKeyDown(key);
}
else
{
window.OnKeyUp(window.key_args);
window.OnKeyUp(key);
}
//window.keyboard.SetKey(TranslateKey(key.scancode), (uint)key.scancode, key_pressed);
}
static unsafe void ProcessTextInputEvent(Sdl2NativeWindow window, TextInputEvent ev)
@ -281,23 +268,19 @@ namespace OpenTK.Platform.SDL2
for (int i = 0; i < decoded_length; i++)
{
window.keypress_args.KeyChar = window.DecodeTextBuffer[i];
window.OnKeyPress(window.keypress_args);
window.OnKeyPress(window.DecodeTextBuffer[i]);
}
}
static void ProcessMouseMotionEvent(Sdl2NativeWindow window, MouseMotionEvent ev)
{
//float scale = window.ClientSize.Width / (float)window.Size.Width;
window.MouseState.X = ev.X;
window.MouseState.Y = ev.Y;
window.OnMouseMove();
window.OnMouseMove(ev.X, ev.Y);
}
static void ProcessMouseWheelEvent(Sdl2NativeWindow window, MouseWheelEvent ev)
{
window.MouseState.SetScrollRelative(ev.X, ev.Y);
window.OnMouseWheel();
window.OnMouseWheel(ev.X, ev.Y);
}
static void ProcessWindowEvent(Sdl2NativeWindow window, WindowEvent e)

View file

@ -393,8 +393,7 @@ namespace OpenTK.Platform.Windows
if (!Char.IsControl(c))
{
KeyPressArgs.KeyChar = c;
OnKeyPress(KeyPressArgs);
OnKeyPress(c);
}
}
@ -437,9 +436,7 @@ namespace OpenTK.Platform.Windows
if (points == 0 || (points == -1 && lastError == Constants.ERROR_POINT_NOT_FOUND))
{
// Just use the mouse move position
MouseState.X = point.X;
MouseState.Y = point.Y;
OnMouseMove();
OnMouseMove(point.X, point.Y);
}
else if (points == -1)
{
@ -477,9 +474,7 @@ namespace OpenTK.Platform.Windows
position.Y -= 65536;
}
Functions.ScreenToClient(handle, ref position);
MouseState.X = position.X;
MouseState.Y = position.Y;
OnMouseMove();
OnMouseMove(position.X, position.Y);
}
}
mouse_last_timestamp = timestamp;
@ -508,37 +503,32 @@ namespace OpenTK.Platform.Windows
{
// This is due to inconsistent behavior of the WParam value on 64bit arch, whese
// wparam = 0xffffffffff880000 or wparam = 0x00000000ff100000
MouseState.SetScrollRelative(0, ((long)wParam << 32 >> 48) / 120.0f);
OnMouseWheel();
OnMouseWheel(0, ((long)wParam << 32 >> 48) / 120.0f);
}
void HandleMouseHWheel(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
// This is due to inconsistent behavior of the WParam value on 64bit arch, whese
// wparam = 0xffffffffff880000 or wparam = 0x00000000ff100000
MouseState.SetScrollRelative(((long)wParam << 32 >> 48) / 120.0f, 0);
OnMouseWheel();
OnMouseWheel(((long)wParam << 32 >> 48) / 120.0f, 0);
}
void HandleLButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.SetCapture(window.Handle);
MouseState[MouseButton.Left] = true;
OnMouseDown();
OnMouseDown(MouseButton.Left);
}
void HandleMButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.SetCapture(window.Handle);
MouseState[MouseButton.Middle] = true;
OnMouseDown();
OnMouseDown(MouseButton.Middle);
}
void HandleRButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.SetCapture(window.Handle);
MouseState[MouseButton.Right] = true;
OnMouseDown();
OnMouseDown(MouseButton.Right);
}
void HandleXButtonDown(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
@ -547,29 +537,25 @@ namespace OpenTK.Platform.Windows
MouseButton button =
((wParam.ToInt32() & 0xFFFF0000) >> 16) == 1 ?
MouseButton.Button1 : MouseButton.Button2;
MouseState[button] = true;
OnMouseDown();
OnMouseDown(button);
}
void HandleLButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.ReleaseCapture();
MouseState[MouseButton.Left] = false;
OnMouseUp();
OnMouseUp(MouseButton.Left);
}
void HandleMButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.ReleaseCapture();
MouseState[MouseButton.Middle] = false;
OnMouseUp();
OnMouseUp(MouseButton.Middle);
}
void HandleRButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
{
Functions.ReleaseCapture();
MouseState[MouseButton.Right] = false;
OnMouseUp();
OnMouseUp(MouseButton.Right);
}
void HandleXButtonUp(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
@ -578,8 +564,7 @@ namespace OpenTK.Platform.Windows
MouseButton button =
((wParam.ToInt32() & 0xFFFF0000) >> 16) == 1 ?
MouseButton.Button1 : MouseButton.Button2;
MouseState[button] = false;
OnMouseUp();
OnMouseUp(button);
}
void HandleKeyboard(IntPtr handle, WindowMessage message, IntPtr wParam, IntPtr lParam)
@ -605,15 +590,11 @@ namespace OpenTK.Platform.Windows
{
if (pressed)
{
KeyDownArgs.Key = key;
KeyDownArgs.Modifiers = InputDriver.Keyboard[0].GetModifiers();
OnKeyDown(KeyDownArgs);
OnKeyDown(key);
}
else
{
KeyUpArgs.Key = key;
KeyUpArgs.Modifiers = InputDriver.Keyboard[0].GetModifiers();
OnKeyUp(KeyUpArgs);
OnKeyUp(key);
}
}
}

View file

@ -188,7 +188,7 @@ namespace OpenTK.Platform.Windows
if (is_valid)
{
keyboard.SetKeyState(key, (byte)scancode, pressed);
keyboard.SetKeyState(key, pressed);
processed = true;
}

View file

@ -763,18 +763,6 @@ namespace OpenTK.Platform.X11
return cursor;
}
void SetMouseClamped(int x, int y,
int left, int top, int width, int height)
{
// Clamp mouse to the specified rectangle.
x = Math.Max(x, left);
x = Math.Min(x, width);
y = Math.Max(y, top);
y = Math.Min(y, height);
MouseState.X = x;
MouseState.Y = y;
}
#endregion
#region INativeWindow Members
@ -861,18 +849,12 @@ namespace OpenTK.Platform.X11
if (pressed)
{
// Raise KeyDown event
KeyDownArgs.Key = key;
KeyDownArgs.ScanCode = (uint)e.KeyEvent.keycode;
//KeyDownArgs.Modifiers = keyboard.GetModifiers();
OnKeyDown(KeyDownArgs);
OnKeyDown(key);
}
else
{
// Raise KeyUp event
KeyUpArgs.Key = key;
KeyUpArgs.ScanCode = (uint)e.KeyEvent.keycode;
//KeyUpArgs.Modifiers = keyboard.GetModifiers();
OnKeyUp(KeyUpArgs);
OnKeyUp(key);
}
if (pressed)
@ -888,8 +870,7 @@ namespace OpenTK.Platform.X11
{
if (!Char.IsControl(chars[i]))
{
KeyPressArgs.KeyChar = chars[i];
OnKeyPress(KeyPressArgs);
OnKeyPress(chars[i]);
}
}
}
@ -922,10 +903,9 @@ namespace OpenTK.Platform.X11
}
else if (!CursorVisible)
{
SetMouseClamped(
MouseState.X + x - mouse_rel_x,
MouseState.Y + y - mouse_rel_y,
0, 0, Width, Height);
OnMouseMove(
MathHelper.Clamp(MouseState.X + x - mouse_rel_x, 0, Width),
MathHelper.Clamp(MouseState.Y + y - mouse_rel_y, 0, Height));
mouse_rel_x = x;
mouse_rel_y = y;
@ -935,53 +915,42 @@ namespace OpenTK.Platform.X11
}
else
{
SetMouseClamped(x, y, 0, 0, Width, Height);
OnMouseMove(
MathHelper.Clamp(x, 0, Width),
MathHelper.Clamp(y, 0, Height));
mouse_rel_x = x;
mouse_rel_y = y;
}
OnMouseMove();
break;
}
case XEventName.ButtonPress:
switch (e.ButtonEvent.button)
{
case 1: MouseState.EnableBit((int)MouseButton.Left); break;
case 2: MouseState.EnableBit((int)MouseButton.Middle); break;
case 3: MouseState.EnableBit((int)MouseButton.Right); break;
case 4: MouseState.SetScrollRelative(0, 1); break;
case 5: MouseState.SetScrollRelative(0, -1); break;
case 6: MouseState.EnableBit((int)MouseButton.Button1); break;
case 7: MouseState.EnableBit((int)MouseButton.Button2); break;
case 8: MouseState.EnableBit((int)MouseButton.Button3); break;
case 9: MouseState.EnableBit((int)MouseButton.Button4); break;
case 10: MouseState.EnableBit((int)MouseButton.Button5); break;
case 11: MouseState.EnableBit((int)MouseButton.Button6); break;
case 12: MouseState.EnableBit((int)MouseButton.Button7); break;
case 13: MouseState.EnableBit((int)MouseButton.Button8); break;
case 14: MouseState.EnableBit((int)MouseButton.Button9); break;
int dx, dy;
MouseButton button = X11KeyMap.TranslateButton(e.ButtonEvent.button, out dx, out dy);
if (button != MouseButton.LastButton)
{
OnMouseDown(button);
}
else if (dx != 0 || dy != 0)
{
OnMouseWheel(dx, dy);
}
}
OnMouseDown();
break;
case XEventName.ButtonRelease:
switch (e.ButtonEvent.button)
{
case 1: MouseState.DisableBit((int)MouseButton.Left); break;
case 2: MouseState.DisableBit((int)MouseButton.Middle); break;
case 3: MouseState.DisableBit((int)MouseButton.Right); break;
case 6: MouseState.DisableBit((int)MouseButton.Button1); break;
case 7: MouseState.DisableBit((int)MouseButton.Button2); break;
case 8: MouseState.DisableBit((int)MouseButton.Button3); break;
case 9: MouseState.DisableBit((int)MouseButton.Button4); break;
case 10: MouseState.DisableBit((int)MouseButton.Button5); break;
case 11: MouseState.DisableBit((int)MouseButton.Button6); break;
case 12: MouseState.DisableBit((int)MouseButton.Button7); break;
case 13: MouseState.DisableBit((int)MouseButton.Button8); break;
case 14: MouseState.DisableBit((int)MouseButton.Button9); break;
int dx, dy;
MouseButton button = X11KeyMap.TranslateButton(e.ButtonEvent.button, out dx, out dy);
if (button != MouseButton.LastButton)
{
OnMouseUp(button);
}
}
OnMouseUp();
break;
case XEventName.FocusIn: