The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL 2: How to read shift+X keys?
Sparks


Joined: 01 Jun 2013
Posts: 47
https://wiki.libsdl.org/SDLKeycodeLookup
Here, it shows (for example) 33 = 0x21 ('!') = SDLK_EXCLAIM.
https://wiki.libsdl.org/SDL_Scancode
Here it shows "!" = (none) = SDLK_EXCLAIM.

In the event loop I check for SDL_KEYDOWN, and then do, SDL_GetScancodeName(event->key.keysym.scancode), and it shows "1", and SDL_GetKeyName(event->key.keysym.sym) also shows "1" instead of showing "!".
I then try shift + "a" (should be "A") but, it shows "a".
What am I missing here?
SDL 2: How to read shift+X keys?
Jonny D


Joined: 12 Sep 2009
Posts: 932
What you are missing is the SDL_TEXTINPUT event.  See here:https://wiki.libsdl.org/SDL_TextInputEvent



Call SDL_StartTextInput(), then start checking for SDL_TEXTINPUT events.  Use the characters that the event gives you.  This is a change from SDL1.2, where properly dealing with different keyboard layouts, input devices (e.g. mobile virtual keyboard), and different character sets (or Unicode) was not possible.


Jonny D



On Thu, Feb 26, 2015 at 3:48 PM, Sparks wrote:
Quote:
https://wiki.libsdl.org/SDLKeycodeLookup
Here, it shows (for example) 33 = 0x21 ('!') = SDLK_EXCLAIM.
https://wiki.libsdl.org/SDL_Scancode
Here it shows "!" = (none) = SDLK_EXCLAIM.

In the event loop I check for SDL_KEYDOWN, and then do, SDL_GetScancodeName(event->key.keysym.scancode), and it shows "1", and SDL_GetKeyName(event->key.keysym.sym) also shows "1" instead of showing "!".
I then try shift + "a" (should be "A") but, it shows "a".
What am I missing here?


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Re: SDL 2: How to read shift+X keys?
Sparks


Joined: 01 Jun 2013
Posts: 47
Yes, I know about this, but, that isn't what I need, SDL_TEXTINPUT is too limiting with having to do SDL_Start/StopTextInput() when we want keyboard input, since other keys can be used at the same time. That is why we must manually do this, like the "old" way, which seems broken now?
If both the scancode and the keycode both have "!" as an entry, then, it should be possible to have "!" given in the keycode or scancode.

Uppercase 'A' should be value 65, lower case 'a' should be 97 according to the ASCII table, that is the same behavior we want with SDL 2.

Jonny D wrote:
What you are missing is the SDL_TEXTINPUT event.  See here:https://wiki.libsdl.org/SDL_TextInputEvent



Call SDL_StartTextInput(), then start checking for SDL_TEXTINPUT events.  Use the characters that the event gives you.  This is a change from SDL1.2, where properly dealing with different keyboard layouts, input devices (e.g. mobile virtual keyboard), and different character sets (or Unicode) was not possible.


Jonny D



On Thu, Feb 26, 2015 at 3:48 PM, Sparks <> wrote:
Quote:
https://wiki.libsdl.org/SDLKeycodeLookup
Here, it shows (for example) 33 = 0x21 ('!') = SDLK_EXCLAIM.
https://wiki.libsdl.org/SDL_Scancode
Here it shows "!" = (none) = SDLK_EXCLAIM.

In the event loop I check for SDL_KEYDOWN, and then do, SDL_GetScancodeName(event->key.keysym.scancode), and it shows "1", and SDL_GetKeyName(event->key.keysym.sym) also shows "1" instead of showing "!".
I then try shift + "a" (should be "A") but, it shows "a".
What am I missing here?
MrTAToad


Joined: 13 Feb 2014
Posts: 205
Location: Chichester, England
Don't forget SDL_KEYDOWN/UP deal mainly with scancodes, which only represent one key type, regardless of whether there is an alternative type when the shift, alt or ctrl key is held down.

You could try various conversion routines, like getting the key name from the scancode and then checking to see if the shift key is pressed, and which point you convert the ASCII code to uppercase. However, there are a few exceptions - for example, with space you would get the word "Space".
Re: SDL 2: How to read shift+X keys?
Sparks


Joined: 01 Jun 2013
Posts: 47
Sparks wrote:
Yes, I know about this, but, that isn't what I need, SDL_TEXTINPUT is too limiting with having to do SDL_Start/StopTextInput() when we want keyboard input, since other keys can be used at the same time. That is why we must manually do this, like the "old" way, which seems broken now?
If both the scancode and the keycode both have "!" as an entry, then, it should be possible to have "!" given in the keycode or scancode.

Uppercase 'A' should be value 65, lower case 'a' should be 97 according to the ASCII table, that is the same behavior we want with SDL 2.

Just to make it more clear, before SDL 1 had this struct
typedef struct{
Uint8 scancode;
SDLKey sym;
SDLMod mod;
Uint16 unicode;
} SDL_keysym;

Where SDL_keysym.unicode would have, in this case contained "!" and "A" respectively. The SDL_keysym.sym would have been "1" and "a" respectively in my example.

That is what I need, and I can't seem to find a way to mimic that behavior anymore? Sad
SDL 2: How to read shift+X keys?
Jonny D


Joined: 12 Sep 2009
Posts: 932
What would be wrong with using SDL_StartTextInput() and leaving it on for desktop systems?  You would do the same processing, unless I'm misunderstanding what you're doing.

Alternatively, use SDL_GetModState() and you can then shift certain ascii values yourself.


Jonny D






On Thu, Feb 26, 2015 at 5:17 PM, Sparks wrote:
Quote:



Sparks wrote:

Yes, I know about this, but, that isn't what I need, SDL_TEXTINPUT is too limiting with having to do SDL_Start/StopTextInput() when we want keyboard input, since other keys can be used at the same time. That is why we must manually do this, like the "old" way, which seems broken now?
If both the scancode and the keycode both have "!" as an entry, then, it should be possible to have "!" given in the keycode or scancode.

Uppercase 'A' should be value 65, lower case 'a' should be 97 according to the ASCII table, that is the same behavior we want with SDL 2.




Just to make it more clear, before SDL 1 had this struct
typedef struct{
Uint8 scancode;
SDLKey sym;
SDLMod mod;
Uint16 unicode;
} SDL_keysym;

Where SDL_keysym.unicode would have, in this case contained "!" and "A" respectively. The SDL_keysym.sym would have been "1" and "a" respectively in my example.

That is what I need, and I can't seem to find a way to mimic that behavior anymore?


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

SDL 2: How to read shift+X keys?
Daniel Gibson
Guest

On 02/27/2015 12:49 AM, Jonathan Dearborn wrote:
Quote:
What would be wrong with using SDL_StartTextInput() and leaving it on
for desktop systems? You would do the same processing, unless I'm
misunderstanding what you're doing.

Agree.

Quote:

Alternatively, use SDL_GetModState() and you can then shift certain
ascii values yourself.


Bad idea.. this might somehow work for a-z, but will utterly fail at all
other keys (including numbers), because of different keyboard layouts.
Just use SDL_TEXTINPUT events for textinput.
It's really worth the effort.

Cheers,
Daniel

Quote:
Jonny D



On Thu, Feb 26, 2015 at 5:17 PM, Sparks
<mailto:> wrote:

__



Sparks wrote:

Yes, I know about this, but, that isn't what I need, SDL_TEXTINPUT
is too limiting with having to do SDL_Start/StopTextInput() when we
want keyboard input, since other keys can be used at the same time.
That is why we must manually do this, like the "old" way, which
seems broken now?
If both the scancode and the keycode both have "!" as an entry,
then, it should be possible to have "!" given in the keycode or
scancode.

Uppercase 'A' should be value 65, lower case 'a' should be 97
according to the ASCII table, that is the same behavior we want with
SDL 2.



Just to make it more clear, before SDL 1 had this struct
typedef struct{
Uint8 scancode;
SDLKey sym;
SDLMod mod;
Uint16 unicode;
} SDL_keysym;

Where SDL_keysym.unicode would have, in this case contained "!" and
"A" respectively. The SDL_keysym.sym would have been "1" and "a"
respectively in my example.

That is what I need, and I can't seem to find a way to mimic that
behavior anymore? Sad

_______________________________________________
SDL mailing list
<mailto:
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org




_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Re: SDL 2: How to read shift+X keys?
Sparks


Joined: 01 Jun 2013
Posts: 47
Jonny D wrote:
What would be wrong with using SDL_StartTextInput() and leaving it on for desktop systems?  You would do the same processing, unless I'm misunderstanding what you're doing.

Alternatively, use SDL_GetModState() and you can then shift certain ascii values yourself.


Jonny D


I thought if you leave SDL_StartTextInput() 'on', that it would swallow up all key presses from that point on, until you turn that off?
Do you still get SDL_KEYDOWN events, along with SDL_TEXTINPUT & SDL_TEXTEDITING events?
In other words, would I get a SDL_KEYDOWN event for pressing "shift a" (would generate a 'a'), and that will be "A" in the SDL_TEXTINPUT event?
SDL 2: How to read shift+X keys?
Jonny D


Joined: 12 Sep 2009
Posts: 932
Yeah, you still get SDL_KEYDOWN when it is on.  That way you can handle non-printing keys too (like backspace).

Good luck!
Jonny D






On Thu, Feb 26, 2015 at 7:14 PM, Sparks wrote:
Quote:
Jonny D wrote:What would be wrong with using SDL_StartTextInput() and leaving it on for desktop systems?  You would do the same processing, unless I'm misunderstanding what you're doing.

Alternatively, use SDL_GetModState() and you can then shift certain ascii values yourself.


Jonny D




I thought if you leave SDL_StartTextInput() 'on', that it would swallow up all key presses from that point on, until you turn that off?
Do you still get SDL_KEYDOWN events, along with SDL_TEXTINPUT & SDL_TEXTEDITING events?
In other words, would I get a SDL_KEYDOWN event for pressing "shift a" (would generate a 'a'), and that will be "A" in the SDL_TEXTINPUT event?


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Quick Question
trevor.shawn.williams


Joined: 07 Nov 2014
Posts: 6
What exactly are you trying to do? I can't tell from the conversation if you're trying to issue alternate game commands if a mod key is pressed (press 'w' to walk, press 'w' + 'Shift' to run) or implement some kind of text input interface like a chat client. Is your initial post the results of debugging or do you really need the character output? Sorry if you've made it clear, and I've just missed it.
Re: SDL 2: How to read shift+X keys?
mbabuskov


Joined: 08 Feb 2015
Posts: 29
Sparks wrote:
Do you still get SDL_KEYDOWN events, along with SDL_TEXTINPUT & SDL_TEXTEDITING events?


Yes.

Sparks wrote:
In other words, would I get a SDL_KEYDOWN event for pressing "shift a" (would generate a 'a'), and that will be "A" in the SDL_TEXTINPUT event?


Yes, exactly.

You can use SDL_KEYDOWN to detect stuff like Backspace to erase the last char or ESC being pressed to cancel editing.