[SDL] keyboard input
thyma2
Posted messages
21
Status
Member
-
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
[Dal] Posted messages 6122 Registration date Status Contributor Last intervention -
Hello,
I'm working on a project that uses the SDL library.
I created a function that retrieves keyboard input from the user.
Unfortunately, not all keyboard inputs are working.
The characters I can activate with a single key press work (for example a, b, c, d...)
but for all characters that require a combination of keys, it doesn't work (for instance 1, 2, 3 ...)
because my keyboard doesn't have dedicated number keys; I need to press & and Shift at the same time to type 1.
Well, I could, in theory, detect special characters and the Shift key and activate the letter only if both keys are pressed. But that's a bad idea because if the keyboard is not set to the same language, it won't work anymore. Moreover, the SDL documentation is based on the English keyboard, and it's impossible to find the characters é or è.
To retrieve the input, I use the keyup and keydown functions from SDL.
Feel free to ask me to clarify my problem, of course.
Have a good day!
I'm working on a project that uses the SDL library.
I created a function that retrieves keyboard input from the user.
Unfortunately, not all keyboard inputs are working.
The characters I can activate with a single key press work (for example a, b, c, d...)
but for all characters that require a combination of keys, it doesn't work (for instance 1, 2, 3 ...)
because my keyboard doesn't have dedicated number keys; I need to press & and Shift at the same time to type 1.
Well, I could, in theory, detect special characters and the Shift key and activate the letter only if both keys are pressed. But that's a bad idea because if the keyboard is not set to the same language, it won't work anymore. Moreover, the SDL documentation is based on the English keyboard, and it's impossible to find the characters é or è.
To retrieve the input, I use the keyup and keydown functions from SDL.
Feel free to ask me to clarify my problem, of course.
Have a good day!
3 answers
-
Hi thyma2,
There are no keyup and keydown functions in SDL2.
I think you are referring to the SDL_KEYUP and SDL_KEYDOWN event types in an SDL_Event union obtained with the SDL_PollEvent() function and the scancode or keycode retrieved via the key fields, then keysym.
In any case, this method is typically used to retrieve information about pressed keys (rather than entered characters), usually to build a user interface that allows the program to react to key presses.
To retrieve typed text, you should look into the SDL_TEXTINPUT event type, which will capture the text entered in utf8.
https://wiki.libsdl.org/Tutorials/TextInput
Dal -
Hi
thank you for your response
so I tried to use this function instead of the ones I created.
but I have a compilation error undefined reference to function sdl TextInput
it's strange because I am including sdl and the other sdl functions work.
maybe it's due to the fact that I am using arch linux and not windows? -
Re - hi
yes, I was on sdl1, and this function only exists in sdl2
but since I need it, I'm migrating to sdl2 (it's annoying, I have to redo a lot of things)
I'll check back tonight or tomorrow to see if this method works on sdl2-
Yes, it's for SDL2.
In SDL 1.2 you had SDL_EnableUNICODE() to set up SDL to obtain the Unicode character associated with the pressed key.
You can find examples on Lazy Foo's site http://lazyfoo.net/SDL_tutorials/lesson23/index.php (it's in C++).
But it didn't work well for several reasons (see https://wiki.libsdl.org/MigrationGuide?highlight=%28SDL_EnableUNICODE%29) and it was abandoned in SDL2, which creates a different type of event and processes for handling input in UTF-8 that are distinct from those for handling key presses.
Before migrating all your code to SDL2, just create a short SDL2 test program based on the tutorial code.
If you are on Linux, your console should be able to display UTF-8 characters with a simple printf(). You can also display the retrieved UTF-8 string in an SDL window with SDL_ttf as a surface using TTF_RenderUTF8_Solid() or save it to a file and open it with an editor that supports UTF-8 (which shouldn't be a problem on Linux).
-