fragglet / sdl-sopwith

Classic bi-plane shoot-'em up
https://fragglet.github.io/sdl-sopwith
GNU General Public License v2.0
66 stars 13 forks source link

Add support for Numeric Keypad Enter #32

Closed techknight closed 4 months ago

techknight commented 7 months ago

Summary

Changes keyboard handling in these ways:

  1. Alt+Enter using Right Alt now toggles fullscreen mode (previously only Left Alt worked)
  2. Alt+Enter using Numeric Keypad Enter now toggles fullscreen mode (previously only Enter worked)
  3. Numeric Keypad Enter now functions the same as Enter when inputting text
  4. altdown is explicitly set to 0 when fulscreen mode is toggled

Details

I was getting frustrated with entering an IP address for network connections using the numeric keypad, only to find that Numeric Keypad Enter didn't work. 😅

SDL appears to be handling SDLK_KP_ENTER differently than SDLK_RETURN when text input is active? Although I was easily able to add Numeric Keypad Enter when checking for Alt+Enter, I had to explicitly push \n into the input buffer when the key is pressed during text input.

Although I don't regularly press Alt+Enter using Numeric Keypad Enter myself, I decided to support it after verifying that it's customary in other games. (Downwell, RimWorld, Tony Hawk's Pro Skater 1+2)

Finally, I ran into a curious issue in my WSL development environment:

Normally, when you toggle fullscreen mode in SDL Sopwith on Windows or MacOS, Alt is considered no longer pressed even if you're still holding it. Meaning that if you want to want to toggle fullscreen mode a second time, you must first let go of Alt, and then press Alt+Enter again. (Which is totally fine.)

But, under WSL, that doesn't seem to happen naturally, and in fact the SDL_KEYUP for Alt doesn't always appear to be caught. So I've added an altdown = 0 line that explicitly forces the normal behavior:

} else if (altdown && (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER)) {
        vid_fullscreen = !vid_fullscreen;
        Vid_Reset();
        altdown = 0;
        continue;

By adding this, my WSL build behaves as it should, and does not cause any changes in behavior for Windows and MacOS.

Testing

I verified that the keys I worked on still behave as expected elsewhere, such as the key bindings menu.