Skip to content

Add support for right control to sdl backends #785

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

Merged

Conversation

rswinkle
Copy link
Contributor

@rswinkle rswinkle commented Mar 1, 2025

Can't believe I never noticed this issue before.

Pretty self explanatory: use SDL_GetModState() to get whether either control is down and use that instead of the key state array.

@RobLoach RobLoach merged commit 7408af0 into Immediate-Mode-UI:master Mar 4, 2025
1 check passed
@RobLoach
Copy link
Contributor

RobLoach commented Mar 4, 2025

Should we take the same SDL_GetModState() approach for the shift/alt keys too? Or do you think that's unnecessary.

@rswinkle
Copy link
Contributor Author

rswinkle commented Mar 5, 2025

As far as I can tell we don't use ALT at all but I guess we do use SHIFT and track it specifically unlike CTRL. I think it makes more sense to leave it how it is though because it would actually be slightly more work CPU wise to change it. Either we have a shift_down variable/expression and pass it to nk_input in the same place we have now (when the key event is a SHIFT) which makes the shift_down calculation completely redundant with down, or we remove SHIFT from the switch and call nk_input for shift above it which means many many unnecessary function calls.

That was/is one of the issues with GLFW backends. nk_input is really meant for events and so calling it every frame for a state is just inefficient even if it's sometimes functionally the same for the user. I was actually debating adding all of these to the key callback and guarding all of them for that reason even though the user can't tell the difference:

    nk_input_key(ctx, NK_KEY_TEXT_START, glfwGetKey(win, GLFW_KEY_HOME) == GLFW_PRESS);
    nk_input_key(ctx, NK_KEY_TEXT_END, glfwGetKey(win, GLFW_KEY_END) == GLFW_PRESS);
    nk_input_key(ctx, NK_KEY_SCROLL_START, glfwGetKey(win, GLFW_KEY_HOME) == GLFW_PRESS);
    nk_input_key(ctx, NK_KEY_SCROLL_END, glfwGetKey(win, GLFW_KEY_END) == GLFW_PRESS);
    nk_input_key(ctx, NK_KEY_SHIFT, glfwGetKey(win, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS||
                                    glfwGetKey(win, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS);

and same with these but that'd be a bit more complicated

        nk_input_key(ctx, NK_KEY_COPY, 0);
        nk_input_key(ctx, NK_KEY_PASTE, 0);
        nk_input_key(ctx, NK_KEY_CUT, 0);

Speaking of though, I've been using SDL forever and never knew or forgot a long time ago about the mod member of the key event so thanks for pinging me so I would see that. In looking into that after I saw another minor improvement I should have been aware of in the enum SDL_Keymod:

KMOD_CTRL = KMOD_LCTRL | KMOD_RCTRL,

And similarly for all the other 2 sided modifiers. D`oh!

So that's a small improvement we can make to all 6 SDL backends, not sure it's even worth a separate pull request though. I can do it if you want though.

@rswinkle rswinkle deleted the support_sdl_right_control branch May 6, 2025 23:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants