1717#include " imgui_impl_osx_v1.85.h"
1818#import < Cocoa/Cocoa.h>
1919#include < mach/mach_time.h>
20+ #include < vector>
2021
2122// CHANGELOG
2223// (minor and older changes stripped away, please see git history for details)
3940@class ImFocusObserver;
4041
4142// Data
42- static double g_Time = 0.0 ;
43- static NSCursor * g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
44- static bool g_MouseCursorHidden = false ;
45- static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {};
46- static bool g_MouseDown[ImGuiMouseButton_COUNT] = {};
43+ static double g_Time = 0.0 ;
44+ static NSCursor * g_MouseCursors[ImGuiMouseCursor_COUNT] = {};
45+ static bool g_MouseCursorHidden = false ;
46+ static bool g_MouseJustPressed[ImGuiMouseButton_COUNT] = {};
47+ static bool g_MouseDown[ImGuiMouseButton_COUNT] = {};
48+
4749static ImFocusObserver* g_FocusObserver = NULL ;
4850
51+ static std::vector<ImGuiKey> g_KeysPressedWithCmd;
52+
4953// Undocumented methods for creating cursors.
5054@interface NSCursor ()
5155+ (id )_windowResizeNorthWestSouthEastCursor ;
@@ -64,6 +68,7 @@ static void resetKeys()
6468 ImGuiIO& io = ImGui::GetIO ();
6569 io.ClearInputKeys ();
6670 io.KeyCtrl = io.KeyShift = io.KeyAlt = io.KeySuper = false ;
71+ g_KeysPressedWithCmd.clear ();
6772}
6873
6974@interface ImFocusObserver : NSObject
@@ -222,6 +227,13 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
222227 g_Time = current_time;
223228
224229 ImGui_ImplOSX_UpdateMouseCursorAndButtons ();
230+
231+ // Generate KeyUp event for all keys pressed while Cmd was pressed.
232+ for (ImGuiKey key : g_KeysPressedWithCmd)
233+ {
234+ io.AddKeyEvent (key, false );
235+ }
236+ g_KeysPressedWithCmd.clear ();
225237}
226238
227239static ImGuiKey mapCharacterToKey (int c)
@@ -341,15 +353,20 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
341353 if (!io.KeySuper && !(c >= 0xF700 && c <= 0xFFFF ) && c != 127 )
342354 io.AddInputCharacter ((unsigned int )c);
343355
344- // We must reset in case we're pressing a sequence of special keys while keeping the command pressed
345356 ImGuiKey key = mapCharacterToKey (c);
346357 if (key != ImGuiKey_None)
347358 {
348- if (!io.KeySuper && key < ImGuiKey_ModCtrl) // avoid resetKeys() for modifiers and special keys
349- resetKeys ();
350359 io.AddKeyEvent (key, true );
360+
361+ // NB: AddKeyEvent swaps Ctrl and Cmd (aka Super) on Mac
362+ if (io.KeyCtrl )
363+ {
364+ // MacOS does not generate KeyUp event when Cmd is pressed.
365+ g_KeysPressedWithCmd.push_back (key);
366+ }
351367 }
352368 }
369+
353370 return io.WantCaptureKeyboard ;
354371 }
355372
@@ -373,18 +390,26 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
373390 {
374391 unsigned int flags = [event modifierFlags ] & NSEventModifierFlagDeviceIndependentFlagsMask;
375392
376- bool oldKeyCtrl = io.KeyCtrl ;
393+ bool oldKeyCtrl = io.KeyCtrl ;
377394 bool oldKeyShift = io.KeyShift ;
378- bool oldKeyAlt = io.KeyAlt ;
395+ bool oldKeyAlt = io.KeyAlt ;
379396 bool oldKeySuper = io.KeySuper ;
380- io.KeyCtrl = flags & NSEventModifierFlagControl;
381- io.KeyShift = flags & NSEventModifierFlagShift;
382- io.KeyAlt = flags & NSEventModifierFlagOption;
383- io.KeySuper = flags & NSEventModifierFlagCommand;
384-
385- // We must reset them as we will not receive any keyUp event if they where pressed with a modifier
397+
398+ bool KeyCtrl = flags & NSEventModifierFlagControl;
399+ bool KeyShift = flags & NSEventModifierFlagShift;
400+ bool KeyAlt = flags & NSEventModifierFlagOption;
401+ bool KeySuper = flags & NSEventModifierFlagCommand;
402+
403+ // NB: AddKeyEvent swaps Ctrl and Cmd (aka Super) on Mac
404+ io.AddKeyEvent (ImGuiKey_ModCtrl, KeyCtrl);
405+ io.AddKeyEvent (ImGuiKey_ModShift, KeyShift);
406+ io.AddKeyEvent (ImGuiKey_ModAlt, KeyAlt);
407+ io.AddKeyEvent (ImGuiKey_ModSuper, KeySuper);
408+
409+ // We must reset keys as we will not receive any keyUp event if they where pressed with a modifier
386410 if ((oldKeyShift && !io.KeyShift ) || (oldKeyCtrl && !io.KeyCtrl ) || (oldKeyAlt && !io.KeyAlt ) || (oldKeySuper && !io.KeySuper ))
387411 resetKeys ();
412+
388413 return io.WantCaptureKeyboard ;
389414 }
390415
0 commit comments