Convert SDL2 "scancode" To ASCII Character?

Hi,

I am updating my SDL2 2-D video game engine now.
How would I convert an SDL2 “scancode” to an ASCII character?
Thanks!

Also, I am using SDL 2.0.4 (which is current version in Linux Mint 18)

I see “SDL_GetKeyFromScancode” in the official docs.
Is the “SDL_Keycode” returned in ASCII like a char or something?

This may be what you need:

Thanks for the reply,
but I am looking for a method to get ASCII char of SDL2 “scancode”.
Anyone know how to do the above?
Thanks!

Maybe if you describe what you’re trying to do (what you want to use the
ASCII Character for, what ASCII character you’re expecting for Keys like
Alt or F1 or Ä, …) we’d be able to give you better advice.

Well…
If user presses key [z] on keyboard I would like to get a “z” as a char.
Also need this for new high score name input.
Thanks!

Ok, two things:
For Text Input (Name in Highscore) you should use SDL_TEXTINPUT, as Olli suggested.

Scancodes are useless for “Getting ‘z’ as a char if a user presses [Z]” - for example, on German keyboards, when you press Z, you get SDL_SCANCODE_Y - because on German keyboards the Z key is on the position of the Y key on US keyboards.
What you want to use here is Keycodes (event->key.keysym.sym, giving you SDL_Keycode like SDLK_z).
The SDLK_* constants are identical to ASCII values for keys where it makes sense (like 1,2,…9,0, A,…,Z), but I’d suggest using https://wiki.libsdl.org/SDL_GetKeyName to get a name to display, so for the F1 key you’d get “F1”, for example.

1 Like

Ok, thanks for the info…
I’ll try this out in the morning…
Preparing the SDL2 2-D engine for a new open-source cross-platform game project…
Thanks!