@@ -62,7 +62,7 @@ static CFTimeInterval GetMachAbsoluteTimeInSeconds()
6262static void resetKeys ()
6363{
6464 ImGuiIO& io = ImGui::GetIO ();
65- memset ( io.KeysDown , 0 , sizeof (io. KeysDown ) );
65+ io.ClearInputKeys ( );
6666 io.KeyCtrl = io.KeyShift = io.KeyAlt = io.KeySuper = false ;
6767}
6868
@@ -106,31 +106,6 @@ bool ImGui_ImplOSX_Init()
106106 // io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
107107 io.BackendPlatformName = " imgui_impl_osx" ;
108108
109- // Keyboard mapping. Dear ImGui will use those indices to peek into the io.KeyDown[] array.
110- const int offset_for_function_keys = 256 - 0xF700 ;
111- io.KeyMap [ImGuiKey_Tab] = ' \t ' ;
112- io.KeyMap [ImGuiKey_LeftArrow] = NSLeftArrowFunctionKey + offset_for_function_keys;
113- io.KeyMap [ImGuiKey_RightArrow] = NSRightArrowFunctionKey + offset_for_function_keys;
114- io.KeyMap [ImGuiKey_UpArrow] = NSUpArrowFunctionKey + offset_for_function_keys;
115- io.KeyMap [ImGuiKey_DownArrow] = NSDownArrowFunctionKey + offset_for_function_keys;
116- io.KeyMap [ImGuiKey_PageUp] = NSPageUpFunctionKey + offset_for_function_keys;
117- io.KeyMap [ImGuiKey_PageDown] = NSPageDownFunctionKey + offset_for_function_keys;
118- io.KeyMap [ImGuiKey_Home] = NSHomeFunctionKey + offset_for_function_keys;
119- io.KeyMap [ImGuiKey_End] = NSEndFunctionKey + offset_for_function_keys;
120- io.KeyMap [ImGuiKey_Insert] = NSInsertFunctionKey + offset_for_function_keys;
121- io.KeyMap [ImGuiKey_Delete] = NSDeleteFunctionKey + offset_for_function_keys;
122- io.KeyMap [ImGuiKey_Backspace] = 127 ;
123- io.KeyMap [ImGuiKey_Space] = 32 ;
124- io.KeyMap [ImGuiKey_Enter] = 13 ;
125- io.KeyMap [ImGuiKey_Escape] = 27 ;
126- io.KeyMap [ImGuiKey_KeyPadEnter] = 3 ;
127- io.KeyMap [ImGuiKey_A] = ' A' ;
128- io.KeyMap [ImGuiKey_C] = ' C' ;
129- io.KeyMap [ImGuiKey_V] = ' V' ;
130- io.KeyMap [ImGuiKey_X] = ' X' ;
131- io.KeyMap [ImGuiKey_Y] = ' Y' ;
132- io.KeyMap [ImGuiKey_Z] = ' Z' ;
133-
134109 // Load cursors. Some of them are undocumented.
135110 g_MouseCursorHidden = false ;
136111 g_MouseCursors[ImGuiMouseCursor_Arrow] = [NSCursor arrowCursor ];
@@ -249,17 +224,52 @@ void ImGui_ImplOSX_NewFrame(NSView* view)
249224 ImGui_ImplOSX_UpdateMouseCursorAndButtons ();
250225}
251226
252- static int mapCharacterToKey (int c)
227+ static ImGuiKey mapCharacterToKey (int c)
253228{
254229 if (c >= ' a' && c <= ' z' )
255- return c - ' a' + ' A' ;
256- if (c == 25 ) // SHIFT+TAB -> TAB
257- return 9 ;
258- if (c >= 0 && c < 256 )
259- return c;
260- if (c >= 0xF700 && c < 0xF700 + 256 )
261- return c - 0xF700 + 256 ;
262- return -1 ;
230+ return (ImGuiKey)(ImGuiKey_A + (c - ' a' ));
231+ if (c >= ' A' && c <= ' Z' )
232+ return (ImGuiKey)(ImGuiKey_A + (c - ' A' ));
233+ if (c >= ' 0' && c <= ' 9' )
234+ return (ImGuiKey)(ImGuiKey_0 + (c - ' 0' ));
235+
236+ switch (c)
237+ {
238+ case 9 : return ImGuiKey_Tab;
239+ case 13 : return ImGuiKey_Enter;
240+ case 27 : return ImGuiKey_Escape;
241+ case 127 : return ImGuiKey_Backspace;
242+ case 25 : return ImGuiKey_Tab; // Shift+Tab workaround
243+ }
244+
245+ // macOS NSEvent function keys (0xF700+)
246+ switch (c)
247+ {
248+ case 0xF700 : return ImGuiKey_UpArrow;
249+ case 0xF701 : return ImGuiKey_DownArrow;
250+ case 0xF702 : return ImGuiKey_LeftArrow;
251+ case 0xF703 : return ImGuiKey_RightArrow;
252+
253+ case 0xF704 : return ImGuiKey_F1;
254+ case 0xF705 : return ImGuiKey_F2;
255+ case 0xF706 : return ImGuiKey_F3;
256+ case 0xF707 : return ImGuiKey_F4;
257+ case 0xF708 : return ImGuiKey_F5;
258+ case 0xF709 : return ImGuiKey_F6;
259+ case 0xF70A : return ImGuiKey_F7;
260+ case 0xF70B : return ImGuiKey_F8;
261+ case 0xF70C : return ImGuiKey_F9;
262+ case 0xF70D : return ImGuiKey_F10;
263+ case 0xF70E : return ImGuiKey_F11;
264+ case 0xF70F : return ImGuiKey_F12;
265+
266+ case 0xF729 : return ImGuiKey_Home;
267+ case 0xF72B : return ImGuiKey_End;
268+ case 0xF72C : return ImGuiKey_PageUp;
269+ case 0xF72D : return ImGuiKey_PageDown;
270+ }
271+
272+ return ImGuiKey_None;
263273}
264274
265275bool ImGui_ImplOSX_HandleEvent (NSEvent * event, NSView * view)
@@ -332,11 +342,13 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
332342 io.AddInputCharacter ((unsigned int )c);
333343
334344 // We must reset in case we're pressing a sequence of special keys while keeping the command pressed
335- int key = mapCharacterToKey (c);
336- if (key != -1 && key < 256 && !io.KeySuper )
337- resetKeys ();
338- if (key != -1 )
339- io.KeysDown [key] = true ;
345+ ImGuiKey key = mapCharacterToKey (c);
346+ if (key != ImGuiKey_None)
347+ {
348+ if (!io.KeySuper && key < ImGuiKey_ModCtrl) // avoid resetKeys() for modifiers and special keys
349+ resetKeys ();
350+ io.AddKeyEvent (key, true );
351+ }
340352 }
341353 return io.WantCaptureKeyboard ;
342354 }
@@ -348,9 +360,11 @@ bool ImGui_ImplOSX_HandleEvent(NSEvent* event, NSView* view)
348360 for (NSUInteger i = 0 ; i < len; i++)
349361 {
350362 int c = [str characterAtIndex: i];
351- int key = mapCharacterToKey (c);
352- if (key != -1 )
353- io.KeysDown [key] = false ;
363+ ImGuiKey key = mapCharacterToKey (c);
364+ if (key != ImGuiKey_None)
365+ {
366+ io.AddKeyEvent (key, false );
367+ }
354368 }
355369 return io.WantCaptureKeyboard ;
356370 }
0 commit comments