GithubHelp home page GithubHelp logo

Comments (3)

mattiasljungstrom avatar mattiasljungstrom commented on May 8, 2024

I've solved this issue in my code by using the SDL2 "scancodes" instead of the key codes. If anyone else is using SDL2 this is how I have it setup:

//
// setup SDL2 keymapping
//
io.KeyMap[ImGuiKey_Tab] = SDL_GetScancodeFromKey( SDLK_TAB );
io.KeyMap[ImGuiKey_LeftArrow] = SDL_GetScancodeFromKey( SDLK_LEFT );
io.KeyMap[ImGuiKey_RightArrow] = SDL_GetScancodeFromKey( SDLK_RIGHT );
io.KeyMap[ImGuiKey_UpArrow] = SDL_GetScancodeFromKey( SDLK_UP );
io.KeyMap[ImGuiKey_DownArrow] = SDL_GetScancodeFromKey( SDLK_DOWN );
io.KeyMap[ImGuiKey_Home] = SDL_GetScancodeFromKey( SDLK_HOME );
io.KeyMap[ImGuiKey_End] = SDL_GetScancodeFromKey( SDLK_END );
io.KeyMap[ImGuiKey_Delete] = SDL_GetScancodeFromKey( SDLK_DELETE );
io.KeyMap[ImGuiKey_Backspace] = SDL_GetScancodeFromKey( SDLK_BACKSPACE );
io.KeyMap[ImGuiKey_Enter] = SDL_GetScancodeFromKey( SDLK_RETURN );
io.KeyMap[ImGuiKey_Escape] = SDL_GetScancodeFromKey( SDLK_ESCAPE );
io.KeyMap[ImGuiKey_A] = SDLK_a;
io.KeyMap[ImGuiKey_C] = SDLK_c;
io.KeyMap[ImGuiKey_V] = SDLK_v;
io.KeyMap[ImGuiKey_X] = SDLK_x;
io.KeyMap[ImGuiKey_Y] = SDLK_y;
io.KeyMap[ImGuiKey_Z] = SDLK_z;

And then later I parse the events more or less like this (some code missing)

case SDL_KEYDOWN:
case SDL_KEYUP:
{
SDL_Scancode key = SDL_GetScancodeFromKey(_keycode);
if (key>=0 && key<512)
{
SDL_Keymod modstate = SDL_GetModState();

    ImGuiIO& io = ImGui::GetIO();

    if (bPressed)
    {
        io.KeysDown[key] = true;
    }
    else
    {
        io.KeysDown[key] = false;
    }
    io.KeyCtrl = (modstate & KMOD_CTRL) != 0;
    io.KeyShift = (modstate & KMOD_SHIFT) != 0;
}

}

//
case SDL_TEXTINPUT:
{
for (uint i=0; i<text.length(); i++)
{
uint32_t keycode = text[i];
if (keycode>=32 && keycode<=255)
{
//printable ASCII characters
ImGui::GetIO().AddInputCharacter( (char)keycode );
}
}
}

from imgui.

ocornut avatar ocornut commented on May 8, 2024

I'll close this for now. The issue you reported is valid and still exist but until we find a library where key values cannot fit within 0..512 i don't think it is worth fixing.
(I don't have an idea for a fix that would be lightweight and not confusing for the most common user atm)

from imgui.

ocornut avatar ocornut commented on May 8, 2024

At worse one can easily store the 17 keys that ImGui use in a contiguous array and just pass that to ImGui.

So instead of doing

io.KeyMap[ImGuiKey_Tab] = SDL_GetScancodeFromKey( SDLK_TAB );
io.KeyMap[ImGuiKey_LeftArrow] = SDL_GetScancodeFromKey( SDLK_LEFT );

and being unable to fill the KeyDown[] array you can do:

io.KeyMap[ImGuiKey_Tab] = ImGuiKey_Tab;
io.KeyMap[ImGuiKey_LeftArrow] = ImGuiKey_LeftArrow;

and fill the KeyDown[] array using ImGuiKey_* indices. The code would only be 2 lines longer to setup the keymap table with a for loop.

from imgui.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.