Skip to content

Commit

Permalink
Support keycode translation on macos shell
Browse files Browse the repository at this point in the history
Differential Revision: D68856456

fbshipit-source-id: b9080d64ad0a75cd406a59d0cd31982c39fb5418
  • Loading branch information
Ren Liu authored and facebook-github-bot committed Feb 3, 2025
1 parent 8df2bb5 commit c60cc8f
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
7 changes: 5 additions & 2 deletions IGLU/imgui/InputListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// @MARK:COVERAGE_EXCLUDE_FILE

#include "InputListener.h"

#include "KeyCodeTranslator.h"
// ImGui has a very awkward expectation when it comes to processing inputs and making decisions
// based on them. This is what it expects clients to do, in order, every frame:
// 1. Send ImGui all events via the input parameters in ImGuiIO.
Expand Down Expand Up @@ -70,7 +70,10 @@ bool InputListener::process(const igl::shell::KeyEvent& event) {
makeCurrentContext();

ImGuiIO& io = ImGui::GetIO();
io.KeysDown[event.key] = event.isDown;
// support IsKeyDown
io.KeysDown[keyFromShellKeyEvent(event)] = event.isDown;
// support IsKeyPressed
io.KeysData[keyFromShellKeyEvent(event)].Down = event.isDown;
return io.WantCaptureKeyboard;
}

Expand Down
54 changes: 54 additions & 0 deletions IGLU/imgui/KeyCodeTranslator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "KeyCodeTranslator.h"

#include <igl/Macros.h>

#if IGL_PLATFORM_APPLE
namespace {
enum KeyModifiers {
kVK_Return = 0x24,
kVK_Tab = 0x30,
kVK_Delete = 0x33,
kVK_Escape = 0x35,
kVK_Shift = 0x38,
kVK_Option = 0x3A,
kVK_Control = 0x3B,
};

IGL_MAYBE_UNUSED ImGuiKey keyFromShellKeyEventApple(igl::shell::KeyEvent event) {
KeyModifiers keyCode = (KeyModifiers)event.key;

switch (keyCode) {
case KeyModifiers::kVK_Return:
return ImGuiKey_Enter;
case KeyModifiers::kVK_Tab:
return ImGuiKey_Tab;
case KeyModifiers::kVK_Delete:
return ImGuiKey_Backspace;
case KeyModifiers::kVK_Escape:
return ImGuiKey_Escape;
case KeyModifiers::kVK_Shift:
return ImGuiKey_LeftShift;
case KeyModifiers::kVK_Option:
return ImGuiKey_LeftAlt;
default:
return (ImGuiKey)keyCode;
}
}
} // namespace
#endif

namespace iglu::imgui {
ImGuiKey keyFromShellKeyEvent(igl::shell::KeyEvent event) {
#if IGL_PLATFORM_APPLE
return keyFromShellKeyEventApple(event);
#endif
return (ImGuiKey)event.key;
}
} // namespace iglu::imgui
15 changes: 15 additions & 0 deletions IGLU/imgui/KeyCodeTranslator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include "imgui.h"
#include <shell/shared/input/KeyListener.h>

namespace iglu::imgui {
ImGuiKey keyFromShellKeyEvent(igl::shell::KeyEvent event);
} // namespace iglu::imgui

0 comments on commit c60cc8f

Please sign in to comment.