MacOS: Several minor fixes:

* Implement MouseWheel event
* Implement KeyPress event
* Fix generation of MouseMove events
* Fix right mouse button up event
This commit is contained in:
kanato 2009-11-14 18:40:56 +00:00
parent 6979e24254
commit ed05d8e12c
3 changed files with 105 additions and 62 deletions

View file

@ -640,6 +640,25 @@ namespace OpenTK.Platform.MacOS.Carbon
return (MouseButton)button; return (MouseButton)button;
} }
static internal int GetEventMouseWheelDelta(IntPtr inEvent)
{
int delta;
unsafe
{
int* d = δ
OSStatus result = API.GetEventParameter(inEvent,
EventParamName.MouseWheelDelta, EventParamType.typeSInt32,
IntPtr.Zero, (uint)sizeof(int), IntPtr.Zero, (IntPtr)d);
if (result != OSStatus.NoError)
throw new MacOSException(result);
}
return delta;
}
static internal OSStatus GetEventWindowMouseLocation(IntPtr inEvent, out HIPoint pt) static internal OSStatus GetEventWindowMouseLocation(IntPtr inEvent, out HIPoint pt)
{ {
HIPoint point; HIPoint point;

View file

@ -121,7 +121,4 @@ namespace OpenTK.Platform.MacOS.Carbon
Command = 0x0100, // Open-Apple - Windows key Command = 0x0100, // Open-Apple - Windows key
Option = 0x0800, // Option key is same position as the alt key on non-mac keyboards. Option = 0x0800, // Option key is same position as the alt key on non-mac keyboards.
} }
partial class API
{
}
} }

View file

@ -64,6 +64,8 @@ namespace OpenTK.Platform.MacOS
static Dictionary<IntPtr, WeakReference> mWindows = new Dictionary<IntPtr, WeakReference>(); static Dictionary<IntPtr, WeakReference> mWindows = new Dictionary<IntPtr, WeakReference>();
KeyPressEventArgs mKeyPressArgs = new KeyPressEventArgs((char)0);
#endregion #endregion
#region AGL Device Hack #region AGL Device Hack
@ -331,25 +333,35 @@ namespace OpenTK.Platform.MacOS
private OSStatus ProcessKeyboardEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) private OSStatus ProcessKeyboardEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData)
{ {
System.Diagnostics.Debug.Assert(evt.EventClass == EventClass.Keyboard); System.Diagnostics.Debug.Assert(evt.EventClass == EventClass.Keyboard);
MacOSKeyCode code; MacOSKeyCode code = (MacOSKeyCode)0;
char charCode; char charCode = '\0';
//Debug.Print("Processing keyboard event {0}", evt.KeyboardEventKind);
switch (evt.KeyboardEventKind)
{
case KeyboardEventKind.RawKeyDown:
case KeyboardEventKind.RawKeyRepeat:
case KeyboardEventKind.RawKeyUp:
GetCharCodes(inEvent, out code, out charCode);
mKeyPressArgs.KeyChar = charCode;
break;
}
switch (evt.KeyboardEventKind) switch (evt.KeyboardEventKind)
{ {
case KeyboardEventKind.RawKeyRepeat: case KeyboardEventKind.RawKeyRepeat:
GetCharCodes(inEvent, out code, out charCode);
InputDriver.Keyboard[0].KeyRepeat = true; InputDriver.Keyboard[0].KeyRepeat = true;
goto case KeyboardEventKind.RawKeyDown; goto case KeyboardEventKind.RawKeyDown;
case KeyboardEventKind.RawKeyDown: case KeyboardEventKind.RawKeyDown:
GetCharCodes(inEvent, out code, out charCode); OnKeyPress(mKeyPressArgs);
InputDriver.Keyboard[0][Keymap[code]] = true; InputDriver.Keyboard[0][Keymap[code]] = true;
return OSStatus.EventNotHandled; return OSStatus.EventNotHandled;
case KeyboardEventKind.RawKeyUp: case KeyboardEventKind.RawKeyUp:
GetCharCodes(inEvent, out code, out charCode);
InputDriver.Keyboard[0][Keymap[code]] = false; InputDriver.Keyboard[0][Keymap[code]] = false;
return OSStatus.EventNotHandled; return OSStatus.EventNotHandled;
case KeyboardEventKind.RawKeyModifiersChanged: case KeyboardEventKind.RawKeyModifiersChanged:
@ -362,6 +374,7 @@ namespace OpenTK.Platform.MacOS
} }
private OSStatus ProcessWindowEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData) private OSStatus ProcessWindowEvent(IntPtr inCaller, IntPtr inEvent, EventInfo evt, IntPtr userData)
{ {
System.Diagnostics.Debug.Assert(evt.EventClass == EventClass.Window); System.Diagnostics.Debug.Assert(evt.EventClass == EventClass.Window);
@ -426,21 +439,6 @@ namespace OpenTK.Platform.MacOS
} }
} }
if (this.windowState == WindowState.Fullscreen)
{
InputDriver.Mouse[0].Position = new Point((int)pt.X, (int)pt.Y);
}
else
{
// ignore clicks in the title bar
if (pt.Y < mTitlebarHeight)
return OSStatus.EventNotHandled;
InputDriver.Mouse[0].Position =
new Point((int)pt.X, (int)(pt.Y - mTitlebarHeight));
}
switch (evt.MouseEventKind) switch (evt.MouseEventKind)
{ {
case MouseEventKind.MouseDown: case MouseEventKind.MouseDown:
@ -462,9 +460,11 @@ namespace OpenTK.Platform.MacOS
} }
break; return OSStatus.NoError;
case MouseEventKind.MouseUp: case MouseEventKind.MouseUp:
button = API.GetEventMouseButton(inEvent);
switch (button) switch (button)
{ {
case MouseButton.Primary: case MouseButton.Primary:
@ -482,12 +482,43 @@ namespace OpenTK.Platform.MacOS
button = API.GetEventMouseButton(inEvent); button = API.GetEventMouseButton(inEvent);
break; return OSStatus.NoError;
case MouseEventKind.WheelMoved:
int delta = API.GetEventMouseWheelDelta(inEvent) / 3;
InputDriver.Mouse[0].Wheel += delta;
return OSStatus.NoError;
case MouseEventKind.MouseMoved: case MouseEventKind.MouseMoved:
case MouseEventKind.MouseDragged: case MouseEventKind.MouseDragged:
//Debug.Print("MouseMoved: {0}", InputDriver.Mouse[0].Position); if (this.windowState == WindowState.Fullscreen)
{
Point mousePosInClient = new Point((int)pt.X, (int)pt.Y);
if (mousePosInClient.X != InputDriver.Mouse[0].X ||
mousePosInClient.Y != InputDriver.Mouse[0].Y)
{
InputDriver.Mouse[0].Position = mousePosInClient;
}
}
else
{
// ignore clicks in the title bar
if (pt.Y < mTitlebarHeight)
return OSStatus.EventNotHandled;
Point mousePosInClient = new Point((int)pt.X, (int)(pt.Y - mTitlebarHeight));
if (mousePosInClient.X != InputDriver.Mouse[0].X ||
mousePosInClient.Y != InputDriver.Mouse[0].Y)
{
InputDriver.Mouse[0].Position = mousePosInClient;
}
}
return OSStatus.EventNotHandled; return OSStatus.EventNotHandled;
@ -496,8 +527,6 @@ namespace OpenTK.Platform.MacOS
return OSStatus.EventNotHandled; return OSStatus.EventNotHandled;
} }
return OSStatus.EventNotHandled;
} }
private static void GetCharCodes(IntPtr inEvent, out MacOSKeyCode code, out char charCode) private static void GetCharCodes(IntPtr inEvent, out MacOSKeyCode code, out char charCode)
@ -598,18 +627,6 @@ namespace OpenTK.Platform.MacOS
bounds = GetRegion().ToRectangle(); bounds = GetRegion().ToRectangle();
} }
protected virtual void OnClosing(CancelEventArgs e)
{
if (Closing != null)
Closing(this, e);
}
protected virtual void OnClosed()
{
if (Closed != null)
Closed(this, EventArgs.Empty);
}
#endregion #endregion
#region INativeGLWindow Members #region INativeGLWindow Members
@ -894,8 +911,7 @@ namespace OpenTK.Platform.MacOS
windowState = value; windowState = value;
if (WindowStateChanged != null) OnWindowStateChanged();
WindowStateChanged(this, EventArgs.Empty);
OnResize(); OnResize();
} }
@ -928,44 +944,55 @@ namespace OpenTK.Platform.MacOS
WindowBorderChanged(this, EventArgs.Empty); WindowBorderChanged(this, EventArgs.Empty);
} }
} }
} }
public event EventHandler<EventArgs> Idle; #region --- Event wrappers ---
private void OnKeyPress(KeyPressEventArgs keyPressArgs)
{
if (KeyPress != null)
KeyPress(this, keyPressArgs);
}
private void OnWindowStateChanged()
{
if (WindowStateChanged != null)
WindowStateChanged(this, EventArgs.Empty);
}
protected virtual void OnClosing(CancelEventArgs e)
{
if (Closing != null)
Closing(this, e);
}
protected virtual void OnClosed()
{
if (Closed != null)
Closed(this, EventArgs.Empty);
}
#endregion
public event EventHandler<EventArgs> Idle;
public event EventHandler<EventArgs> Load; public event EventHandler<EventArgs> Load;
public event EventHandler<EventArgs> Unload; public event EventHandler<EventArgs> Unload;
public event EventHandler<EventArgs> Move; public event EventHandler<EventArgs> Move;
public event EventHandler<EventArgs> Resize; public event EventHandler<EventArgs> Resize;
public event EventHandler<CancelEventArgs> Closing; public event EventHandler<CancelEventArgs> Closing;
public event EventHandler<EventArgs> Closed; public event EventHandler<EventArgs> Closed;
public event EventHandler<EventArgs> Disposed; public event EventHandler<EventArgs> Disposed;
public event EventHandler<EventArgs> IconChanged; public event EventHandler<EventArgs> IconChanged;
public event EventHandler<EventArgs> TitleChanged; public event EventHandler<EventArgs> TitleChanged;
public event EventHandler<EventArgs> ClientSizeChanged; public event EventHandler<EventArgs> ClientSizeChanged;
public event EventHandler<EventArgs> VisibleChanged; public event EventHandler<EventArgs> VisibleChanged;
public event EventHandler<EventArgs> WindowInfoChanged; public event EventHandler<EventArgs> WindowInfoChanged;
public event EventHandler<EventArgs> FocusedChanged; public event EventHandler<EventArgs> FocusedChanged;
public event EventHandler<EventArgs> WindowBorderChanged; public event EventHandler<EventArgs> WindowBorderChanged;
public event EventHandler<EventArgs> WindowStateChanged; public event EventHandler<EventArgs> WindowStateChanged;
public event EventHandler<KeyPressEventArgs> KeyPress; public event EventHandler<KeyPressEventArgs> KeyPress;
public event EventHandler<EventArgs> MouseEnter; public event EventHandler<EventArgs> MouseEnter;
public event EventHandler<EventArgs> MouseLeave; public event EventHandler<EventArgs> MouseLeave;
#endregion #endregion