Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard storage can't fit keycodes that are large #37

Closed
ocornut opened this issue Aug 27, 2014 · 3 comments
Closed

Keyboard storage can't fit keycodes that are large #37

ocornut opened this issue Aug 27, 2014 · 3 comments

Comments

@ocornut
Copy link
Owner

ocornut commented Aug 27, 2014

mattiasljungstrom says:
"I'm using SDL2, and when I input the key mappings from SDL2 for special keys they all have very large values. This also seems to brake the internal code of ImGui. I think it assumes all key code values are between 0-255?"

@mattiasljungstrom
Copy link

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 );
}
}
}

@ocornut
Copy link
Owner Author

ocornut commented Sep 30, 2014

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)

@ocornut ocornut closed this as completed Sep 30, 2014
@ocornut
Copy link
Owner Author

ocornut commented Sep 30, 2014

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.

tom-seddon pushed a commit to tom-seddon/imgui that referenced this issue Jan 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants