Key release handling

I have an application that catches SDL key events. It works fine with
SDL 1.2.7. I recently upgraded to SDL 1.2.11, and key release events
are no longer working.

The problem seems to be that I was relying on the unicode character to
be filled in for both keypress and key release events. According to
the SDL docs, the key release is not guaranteed to fill in the unicode
character. The actual behavior seems to have changed between 1.2.7 and
1.2.11.

How can I get that information back? In particular, I want to handle
shifted keys and control keys. So I can’t use the scancode or the SDL
key sym, because neither of those take the modifiers into account. I
don’t want to do it myself, because my first attempt was toupper(ch)
which really doesn’t work so well when somebody presses anything other
than a letter. Should I assume that a shifted f will always be shifted
on both the press and release, and keep a lookup table of every key
seen so far, together with its modifiers, to its unicode value? (If
so, why can’t SDL do that for me?) Is there something simple I’m
missing?

How can I get that information back? In particular, I want to handle
shifted keys and control keys.

Shifted keys and control keys generally compose characters. Are you trying
to find out when the modifiers are pressed and released?

See ya,
-Sam Lantinga, Lead Software Engineer, Blizzard Entertainment

Steve,

I have an application that catches SDL key events.

I was relying on the unicode character to
be filled in for both keypress and key release events. According to
the SDL docs, the key release is not guaranteed to fill in the unicode
character.

What platform are you working on?

How can I get that information back?

If you want to make SDL to return a valid unicode value on SDL_KEYUP
events, you need to modify platform dependent parts of SDL. If you
make your app. platform neutral, you need to modify OS interface parts
for all platforms, that I don’t think is easy.

If you just need one particular platform, the difficulty to do so
seems vary on your platform.

If you are working on X11 (I guess so, since you wrote “key release
event” that is the X11 terminology, as opposed to key up that is SDL
terminology…), I might say it is impossible, since under X11/Xlib
the ability to return a meaningful character code on KeyRelease is
mutually exclusive with the ability to support non Latin-1 characters.

In particular, I want to handle
shifted keys and control keys. So I can’t use the scancode or the SDL
key sym, because neither of those take the modifiers into account.

I’m just curious; if a user pressed A key, pressed Shift key, then
released A key, what unicode value does your application expect? 0x41
(for upper case A) or 0x61 (for lower case A)?

If a user operated the keyboard as follows, what unicode value for the
last KEYUP for B key?

Pressed A
Pressed Shift
Pressed B
Pressed Ctrl
Released A
Released Shift
Released B

Well, you wrote your app. worked with SDL 1.2.7. I could test it
by myself on SDL 1.2.7…--------------------------------------
Start Yahoo! Auction now! Check out the cool campaign
http://pr.mail.yahoo.co.jp/auction/

Steve,

I have an application that catches SDL key events.

I was relying on the unicode character to
be filled in for both keypress and key release events. According to
the SDL docs, the key release is not guaranteed to fill in the unicode
character.

What platform are you working on?

Linux (X11), for the foreseeable future. Eventually Windows in addition. Maybe.

How can I get that information back?

If you want to make SDL to return a valid unicode value on SDL_KEYUP
events, you need to modify platform dependent parts of SDL. If you
make your app. platform neutral, you need to modify OS interface parts
for all platforms, that I don’t think is easy.

If you just need one particular platform, the difficulty to do so
seems vary on your platform.

If you are working on X11 (I guess so, since you wrote “key release
event” that is the X11 terminology, as opposed to key up that is SDL
terminology…), I might say it is impossible, since under X11/Xlib
the ability to return a meaningful character code on KeyRelease is
mutually exclusive with the ability to support non Latin-1 characters.

Why? I’ve heard that before, and find it very believable, but I
haven’t used accented characters since back when you had to hold down
ALT and press the numeric code on the keypad. So I don’t even know how
to type them today.

But I don’t need SDL to give me a unicode value on key release. I
already have to have a key event handling layer above SDL, so all I’m
really looking for is a reasonable algorithm, preferably one that
matches what Everyone Else ™ is doing.

In particular, I want to handle
shifted keys and control keys. So I can’t use the scancode or the SDL
key sym, because neither of those take the modifiers into account.

I’m just curious; if a user pressed A key, pressed Shift key, then
released A key, what unicode value does your application expect? 0x41
(for upper case A) or 0x61 (for lower case A)?

Either one would be fine by me. But I would prefer shift-down, A-down,
shift-up, A-up to produce “A” (not “a”) on the A-up event. Which I
think means that I would answer 0x61 to your question, to be
consistent – I think I want up events to give me back the same code
that was emitted during the down event. (Which implies I should just
keep a mapping from <scancode,modifiers> -> unicode, and use the down
event modifiers when looking up the up event’s scancode. I guess.)

If a user operated the keyboard as follows, what unicode value for the
last KEYUP for B key?

Pressed A
Pressed Shift
Pressed B
Pressed Ctrl
Released A
Released Shift
Released B

By the logic above, which I still agree with for this sequence, I’d
want an uppercase B.

Well, you wrote your app. worked with SDL 1.2.7. I could test it
by myself on SDL 1.2.7…

Oh! Thanks for the offer, but I’m really just looking for offhand
advice, not some deep bugfix or whatever. I don’t have any reason to
believe that SDL is doing anything wrong; I’m just trying to figure
out what the right way of using what it’s producing.

My app accepts some basic keyboard controls, where the controls are
different for control characters and regular characters. (And very
occasionally, shifted characters.) The user can specify key mappings
with strings like “f”, “F”, and “\C-f”. I’d like to use a common
wchar_t underneath, so I’d convert “\C-f” to the same wchar_t that a
key release on the ‘F’ key produces when the Ctrl key was down during
the F-down event. (Sometimes the user will want the trigger to be on
the down event, sometimes on the up; that specification is done
separately from the “\C-f” description.)On 6/13/07, alissa_sabre at yahoo.co.jp <alissa_sabre at yahoo.co.jp> wrote:

I was relying on the unicode character to
be filled in for both keypress and key release events.

How can I get that information back?

under X11/Xlib
the ability to return a meaningful character code on KeyRelease is
mutually exclusive with the ability to support non Latin-1 characters.

Why?

Because XLookupString() that works both on KeyPress and on KeyRelease
supports Latin-1 only, and Xutf8LookupString() that supports all
Unicode characters works only on KeyPress. X Window has no API that
works on KeyRelease and can handle Unicode characters.

But I don’t need SDL to give me a unicode value on key release. I
already have to have a key event handling layer above SDL, so all I’m
really looking for is a reasonable algorithm, preferably one that
matches what Everyone Else ™ is doing.

Ok. I misunderstood your first message. I’m not eligible to answer
your question if it is on SDL application practices for
modifer-keys-aware key-up handling. I have never worked on an
application with such requirements. Sorry for bothering you.--------------------------------------
Start Yahoo! Auction now! Check out the cool campaign
http://pr.mail.yahoo.co.jp/auction/