0

I'm trying to check if the colon key is pressed, I'm using a English keyboard which means colon is shift + ;, my though was to read the keyboard state and then check the status of the scancode:

  SDL_PumpEvents();
  const uint8 *keyState = SDL_GetKeyboardState(NULL);

  printf("keystate %d\n", keyState[scancode]);

but then I realized there is a scancode for the keypad colon SDL_SCANCODE_KP_COLON equivalent toSDLK_KP_COLON, but there is no scancode for SDLK_COLON (In case you are wondering: I tried with the keypad version and is not working).

So, I wonder why there is no equivalent scancode for SDLK_COLON and what's the best way to do this instead?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Hugo
  • 2,139
  • 4
  • 22
  • 30
  • 1
    Check `Shift`+`;` separately. Scancodes are for physical buttons, `:` isn't a separate button. – HolyBlackCat Feb 14 '23 at 14:46
  • Right, but I would like to do it in a way that's layout agnostic. My understanding is keycodes are for buttons but scancodes are meant to be layout agnostic. – Hugo Feb 14 '23 at 14:49
  • 1
    Depends on what you mean by "agnostic". Scancodes are for physical button locations, they ignore the labels printed on keycaps (e.g. WASD game controls should use scancodes, that'll remap to ZQSD on AZERTY), while keycodes correspond to symbols on keycaps (text editing should use keycodes). – HolyBlackCat Feb 14 '23 at 14:51
  • Well, let's put it this way: what if I don't know the current layout, how can I check if the right combinations of keys that correspond to `:` are pressed? – Hugo Feb 14 '23 at 14:57
  • Either a keycode, or `SDL_TEXTEDITING` event (the latter respects the current language setting, for folks switching between 2+ languages). You'd use a scancode if you wanted 'a key two positions to the left from Enter'. – HolyBlackCat Feb 14 '23 at 15:02
  • Can you tell us what you're using the hotkeys for? – HolyBlackCat Feb 14 '23 at 15:08
  • Sure, I'm building an UI and when you press `:` it should open a command line bar vim style – Hugo Feb 14 '23 at 15:29
  • Depends on how you want your users to memorize this hotkey. Is it "shift + whatever is under my right pinky"? Then scancode. Or is it "as if you want to type `:`"? Then keycode or `SDL_TEXTEDITING`. Vim uses the latter, but terminals don't expose anything other than `SDL_TEXTEDITING`. – HolyBlackCat Feb 14 '23 at 15:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/251870/discussion-between-hugo-and-holyblackcat). – Hugo Feb 14 '23 at 15:49

1 Answers1

2

There is no colon scancode because there is no key on an ANSI (US) layout keyboard that produces a colon without modifiers. SDLK_COLON exists because there are layouts where a key produces a colon without modifier, like the French layout where the key corresponding to SDL_SCANCODE_PERIOD (dot . or greater-than sign >) makes a colon : or a slash / (with shift). So if you press that key on a French keyboard, you receive an event with SDL_SCANCODE_PERIOD and SDLK_COLON.

What I assume you want is to know if the user typed a colon, which has nothing to do with scancodes or even keycodes when dealing with different keyboard layouts. For that, you need a SDL_TextInputEvent. Check out the wiki's tutorial about it.

Or you could use SDL_SCANCODE_SEMICOLON, check what that key corresponds to for the user, and display that so the user knows what key to press (it would be the ù key for a French keyboard).

Nelfeal
  • 12,593
  • 1
  • 20
  • 39