From 56e4b3cc85641ab4ed4c6bb286bd00b9f70bf912 Mon Sep 17 00:00:00 2001 From: thefiddler Date: Mon, 17 Feb 2014 23:16:34 +0100 Subject: [PATCH] [Mac] Implemented modifier keys --- .../OpenTK/Platform/MacOS/CarbonGLNative.cs | 102 ++++++++++++------ Source/OpenTK/Platform/MacOS/MacOSKeyMap.cs | 8 +- 2 files changed, 75 insertions(+), 35 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs index 88511fb7..b3307e16 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs @@ -34,6 +34,7 @@ using System.Diagnostics; using System.Drawing; using System.Text; using OpenTK.Graphics; +using OpenTK.Input; namespace OpenTK.Platform.MacOS { @@ -362,7 +363,7 @@ namespace OpenTK.Platform.MacOS break; } - OpenTK.Input.Key key; + Key key; switch (evt.KeyboardEventKind) { case KeyboardEventKind.RawKeyRepeat: @@ -371,29 +372,11 @@ namespace OpenTK.Platform.MacOS 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 @@ namespace OpenTK.Platform.MacOS 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 @@ namespace OpenTK.Platform.MacOS 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() diff --git a/Source/OpenTK/Platform/MacOS/MacOSKeyMap.cs b/Source/OpenTK/Platform/MacOS/MacOSKeyMap.cs index e80aee52..c33f542a 100644 --- a/Source/OpenTK/Platform/MacOS/MacOSKeyMap.cs +++ b/Source/OpenTK/Platform/MacOS/MacOSKeyMap.cs @@ -43,7 +43,7 @@ namespace OpenTK.Platform.MacOS // 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 @@ namespace OpenTK.Platform.MacOS // 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 @@ namespace OpenTK.Platform.MacOS 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 @@ namespace OpenTK.Platform.MacOS 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);