Permalink
Browse files

[Mac] Implemented modifier keys

  • Loading branch information...
1 parent 4066ba8 commit 56e4b3cc85641ab4ed4c6bb286bd00b9f70bf912 @thefiddler thefiddler committed Feb 17, 2014
Showing with 75 additions and 35 deletions.
  1. +71 −31 Source/OpenTK/Platform/MacOS/CarbonGLNative.cs
  2. +4 −4 Source/OpenTK/Platform/MacOS/MacOSKeyMap.cs
@@ -34,6 +34,7 @@
using System.Drawing;
using System.Text;
using OpenTK.Graphics;
+using OpenTK.Input;
namespace OpenTK.Platform.MacOS
{
@@ -362,7 +363,7 @@ private OSStatus ProcessKeyboardEvent(IntPtr inCaller, IntPtr inEvent, EventInfo
break;
}
- OpenTK.Input.Key key;
+ Key key;
switch (evt.KeyboardEventKind)
{
case KeyboardEventKind.RawKeyRepeat:
@@ -371,29 +372,11 @@ private OSStatus ProcessKeyboardEvent(IntPtr inCaller, IntPtr inEvent, EventInfo
break;
case KeyboardEventKind.RawKeyDown:
- Keymap.TryGetValue(code, out key);
- // Legacy keyboard API
- InputDriver.Keyboard[0].SetKey(key, (uint)code, true);
-
- // Raise KeyDown for new keyboard API
- mKeyDownArgs.Key = key;
- KeyDown(this, mKeyDownArgs);
-
- // Raise KeyPress for new keyboard API
- if (!Char.IsControl(mKeyPressArgs.KeyChar))
- {
- OnKeyPress(mKeyPressArgs);
- }
+ ProcessKeyDown(code);
return OSStatus.NoError;
case KeyboardEventKind.RawKeyUp:
- Keymap.TryGetValue(code, out key);
- // Legacy keyboard API
- InputDriver.Keyboard[0].SetKey(key, (uint)code, false);
-
- // Raise KeyUp for new keyboard API
- mKeyUpArgs.Key = key;
- KeyUp(this, mKeyUpArgs);
+ ProcessKeyUp(code);
return OSStatus.NoError;
case KeyboardEventKind.RawKeyModifiersChanged:
@@ -611,6 +594,56 @@ private static void GetCharCodes(IntPtr inEvent, out MacOSKeyCode code, out char
charCode = API.GetEventKeyboardChar(inEvent);
}
+ void ProcessKeyDown(MacOSKeyCode code)
+ {
+ Key key;
+ Keymap.TryGetValue(code, out key);
+
+ // Legacy keyboard API
+ KeyboardDevice keyboard = InputDriver.Keyboard[0];
+ keyboard.SetKey(key, (uint)code, true);
+
+ // Raise KeyDown for new keyboard API
+ mKeyDownArgs.Key = key;
+ mKeyDownArgs.Modifiers = keyboard.GetModifiers();
+
+ KeyDown(this, mKeyDownArgs);
+
+ // Raise KeyPress for new keyboard API
+ if (!Char.IsControl(mKeyPressArgs.KeyChar))
+ {
+ OnKeyPress(mKeyPressArgs);
+ }
+ }
+
+ void ProcessKeyUp(MacOSKeyCode code)
+ {
+ Key key;
+ Keymap.TryGetValue(code, out key);
+
+ // Legacy keyboard API
+ KeyboardDevice keyboard = InputDriver.Keyboard[0];
+ keyboard.SetKey(key, (uint)code, false);
+
+ // Raise KeyUp for new keyboard API
+ mKeyUpArgs.Key = key;
+ mKeyDownArgs.Modifiers = keyboard.GetModifiers();
+
+ KeyUp(this, mKeyUpArgs);
+ }
+
+ void ProcessKey(MacOSKeyCode code, bool pressed)
+ {
+ if (pressed)
+ {
+ ProcessKeyDown(code);
+ }
+ else
+ {
+ ProcessKeyUp(code);
+ }
+ }
+
private void ProcessModifierKey(IntPtr inEvent)
{
MacOSKeyModifiers modifiers = API.GetEventKeyModifiers(inEvent);
@@ -621,25 +654,32 @@ private void ProcessModifierKey(IntPtr inEvent)
bool option = (modifiers & MacOSKeyModifiers.Option) != 0 ? true : false;
bool shift = (modifiers & MacOSKeyModifiers.Shift) != 0 ? true : false;
- Debug.Print("Modifiers Changed: {0}", modifiers);
-
Input.KeyboardDevice keyboard = InputDriver.Keyboard[0];
if (keyboard[OpenTK.Input.Key.AltLeft] ^ option)
- keyboard.SetKey(OpenTK.Input.Key.AltLeft, (uint)MacOSKeyCode.OptionAlt, option);
+ {
+ ProcessKey(MacOSKeyCode.OptionAlt, option);
+ }
if (keyboard[OpenTK.Input.Key.ShiftLeft] ^ shift)
- keyboard.SetKey(OpenTK.Input.Key.ShiftLeft, (uint)MacOSKeyCode.Shift, shift);
-
+ {
+ ProcessKey(MacOSKeyCode.Shift, shift);
+ }
+
if (keyboard[OpenTK.Input.Key.WinLeft] ^ command)
- keyboard.SetKey(OpenTK.Input.Key.WinLeft, (uint)MacOSKeyCode.Command, command);
-
+ {
+ ProcessKey(MacOSKeyCode.Command, command);
+ }
+
if (keyboard[OpenTK.Input.Key.ControlLeft] ^ control)
- keyboard.SetKey(OpenTK.Input.Key.ControlLeft, (uint)MacOSKeyCode.Control, control);
+ {
+ ProcessKey(MacOSKeyCode.Control, control);
+ }
if (keyboard[OpenTK.Input.Key.CapsLock] ^ caps)
- keyboard.SetKey(OpenTK.Input.Key.CapsLock, (uint)MacOSKeyCode.CapsLock, caps);
-
+ {
+ ProcessKey(MacOSKeyCode.CapsLock, caps);
+ }
}
Rect GetClientSize()
@@ -43,7 +43,7 @@ public MacOSKeyMap()
// comments indicate members of the Key enum that are missing
Add(MacOSKeyCode.A, Key.A);
- // AltLeft
+ Add(MacOSKeyCode.OptionAlt, Key.AltLeft);
// AltRight
Add(MacOSKeyCode.B, Key.B);
@@ -55,7 +55,7 @@ public MacOSKeyMap()
// Capslock
// Clear
Add(MacOSKeyCode.Comma, Key.Comma);
- // ControlLeft
+ Add(MacOSKeyCode.Control, Key.ControlLeft);
// ControlRight
Add(MacOSKeyCode.D, Key.D);
Add(MacOSKeyCode.Del, Key.Delete);
@@ -139,7 +139,7 @@ public MacOSKeyMap()
Add(MacOSKeyCode.S, Key.S);
// ScrollLock
Add(MacOSKeyCode.Semicolon, Key.Semicolon);
- //Key.ShiftLeft
+ Add(MacOSKeyCode.Shift, Key.ShiftLeft);
//Key.ShiftRight
Add(MacOSKeyCode.Slash, Key.Slash);
// Key.Sleep
@@ -151,7 +151,7 @@ public MacOSKeyMap()
Add(MacOSKeyCode.Up, Key.Up);
Add(MacOSKeyCode.V, Key.V);
Add(MacOSKeyCode.W, Key.W);
- // WinKeyLeft
+ Add(MacOSKeyCode.Command, Key.WinLeft);
// WinKeyRight
Add(MacOSKeyCode.X, Key.X);
Add(MacOSKeyCode.Y, Key.Y);

0 comments on commit 56e4b3c

Please sign in to comment.