Skip to content

Commit

Permalink
Inputs: Add optional support for untranslated key codes for backends
Browse files Browse the repository at this point in the history
  • Loading branch information
thedmd committed Dec 13, 2021
1 parent c7ba4d6 commit e6d69b6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
17 changes: 17 additions & 0 deletions imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,8 @@ ImGuiIO::ImGuiIO()
MouseDoubleClickMaxDist = 6.0f;
for (int i = 0; i < ImGuiKey_COUNT; i++)
KeyMap[i] = -1;
for (int i = 0; i < ImGuiKey_COUNT; i++)
ScancodeMap[i] = -1;
KeyRepeatDelay = 0.275f;
KeyRepeatRate = 0.050f;
UserData = NULL;
Expand Down Expand Up @@ -4786,6 +4788,21 @@ int ImGui::GetKeyIndex(ImGuiKey imgui_key)
return g.IO.KeyMap[imgui_key];
}

int ImGui::GetUntranslatedKeyIndex(ImGuiKey imgui_key)
{
IM_ASSERT(imgui_key >= 0 && imgui_key < ImGuiKey_COUNT);
ImGuiContext& g = *GImGui;

if (g.IO.BackendFlags & ImGuiBackednFlags_HasUntranslatedKeys)
{
int key_index = g.IO.ScancodeMap[imgui_key];
if (key_index >= 0)
return key_index;
}

return g.IO.KeyMap[imgui_key];
}

const char* ImGui::GetKeyName(ImGuiKey imgui_key)
{
static const char* const key_names[] =
Expand Down
7 changes: 5 additions & 2 deletions imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ namespace ImGui
// - For 'int user_key_index' you can use your own indices/enums according to how your backend/engine stored them in io.KeysDown[].
// - We don't know the meaning of those value. You can use GetKeyIndex() to map a ImGuiKey_ value into the user index.
IMGUI_API int GetKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index. == io.KeyMap[key]
IMGUI_API int GetUntranslatedKeyIndex(ImGuiKey imgui_key); // map ImGuiKey_* values into user's key index == io.ScancodeMap[key] if supported, otherwise act like GetKeyIndex
IMGUI_API const char* GetKeyName(ImGuiKey imgui_key); // returns English name of the key
IMGUI_API int FindImGuiKey(int user_key_index); // finds ImGuiKey_* associated to specified user_key_index. Returns ImGuiKey_None if not found.
IMGUI_API bool IsKeyDown(int user_key_index); // is key being held. == io.KeysDown[user_key_index].
Expand Down Expand Up @@ -1544,7 +1545,8 @@ enum ImGuiBackendFlags_
ImGuiBackendFlags_HasGamepad = 1 << 0, // Backend Platform supports gamepad and currently has one connected.
ImGuiBackendFlags_HasMouseCursors = 1 << 1, // Backend Platform supports honoring GetMouseCursor() value to change the OS cursor shape.
ImGuiBackendFlags_HasSetMousePos = 1 << 2, // Backend Platform supports io.WantSetMousePos requests to reposition the OS mouse position (only used if ImGuiConfigFlags_NavEnableSetMousePos is set).
ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3 // Backend Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices.
ImGuiBackendFlags_RendererHasVtxOffset = 1 << 3, // Backend Renderer supports ImDrawCmd::VtxOffset. This enables output of large meshes (64K+ vertices) while still using 16-bit indices.
ImGuiBackednFlags_HasUntranslatedKeys = 1 << 4, // Backend Platform supports physical keyboard layout mapping to ImGuiKey_XXX.
};

// Enumeration for PushStyleColor() / PopStyleColor()
Expand Down Expand Up @@ -1932,7 +1934,8 @@ struct ImGuiIO
float MouseDoubleClickTime; // = 0.30f // Time for a double-click, in seconds.
float MouseDoubleClickMaxDist; // = 6.0f // Distance threshold to stay in to validate a double-click, in pixels.
float MouseDragThreshold; // = 6.0f // Distance threshold before considering we are dragging.
int KeyMap[ImGuiKey_COUNT]; // <unset> // Map of indices into the KeysDown[512] entries array which represent your "native" keyboard state.
int KeyMap[ImGuiKey_COUNT]; // <unset> // Map of logical ImGuiKey_XXX key indices into the KeysDown[512] entries array which represent your "native" keyboard state.
int ScancodeMap[ImGuiKey_COUNT]; // <unset> // Map of physical ImGuiKey_XXXX key indices into the KeysDown[512] entries array which represent your "native" keyboard state.
float KeyRepeatDelay; // = 0.250f // When holding a key/button, time before it starts repeating, in seconds (for buttons in Repeat mode, etc.).
float KeyRepeatRate; // = 0.050f // When holding a key/button, rate at which it repeats, in seconds.
void* UserData; // = NULL // Store your own data for retrieval by callbacks.
Expand Down
1 change: 1 addition & 0 deletions imgui_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ void ImGui::ShowDemoWindow(bool* p_open)
ImGui::CheckboxFlags("io.BackendFlags: HasMouseCursors", &backend_flags, ImGuiBackendFlags_HasMouseCursors);
ImGui::CheckboxFlags("io.BackendFlags: HasSetMousePos", &backend_flags, ImGuiBackendFlags_HasSetMousePos);
ImGui::CheckboxFlags("io.BackendFlags: RendererHasVtxOffset", &backend_flags, ImGuiBackendFlags_RendererHasVtxOffset);
ImGui::CheckboxFlags("io.BackendFlags: HasUntranslatedKeys", &backend_flags, ImGuiBackednFlags_HasUntranslatedKeys);
ImGui::TreePop();
ImGui::Separator();
}
Expand Down

0 comments on commit e6d69b6

Please sign in to comment.