Weird SHIFT key issue, SDL 1.2.13 on Ubuntu

Good morning all,

I’m having a pretty strange issue with the shift and capslock keys,
using SDL 1.2.13 (the newest version apt-get can find:
libsdl1.2debian-all)on Ubuntu 9.04. I have set up my main loop to
catch key presses and stuff them in a buffer for later use, but I’m
getting really weird results trying to handle the shift/capslock
modifiers. When I have the capslock on, I’m apparently not seeing it
at all, and when I have shift on, I keep getting a repeat of the last
character pressed before the shift key.

For example, when I press the following keys:
abc def ghi shift-j shift-k shift-l
What my program sees is:
abcCCCghiIII

Here’s my event handling code, perhaps someone can spot what I’m doing
wrong? I’ve spent about an hour googling and searching the SDL forum
for something similar, but haven’t had any luck so far.

//Using polling to read input - while will read until all input is
exhausted so input events don’t stack up
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE: // escape key, bail out
{
bQuit = 1;
}
break;
case SDLK_RIGHT:
{
if (iDesiredScreen < 7) // increase current screen
{
iDesiredScreen++;
}
else // or wrap around to beginning
{
iDesiredScreen = 0;
}
}
break;
case SDLK_LEFT:
{
if (iDesiredScreen > 0) // decrease current screen
{
iDesiredScreen–;
}
else // or wrap around to end
{
iDesiredScreen = 7;
}
}
break;
case SDLK_PAGEUP:
case SDLK_PAGEDOWN:
case SDLK_HOME:
case SDLK_END:
// ignore this key
break;
case SDLK_RETURN:
{
cKeyBuf[iBufPtr] = 0; // null terminate the buffer string
printf(“Enter key pressed, buffer: [%s]\n”, cKeyBuf);
iBufPtr = 0; // start buffer at beginning again
}
break;

	      case SDLK_LSHIFT:
	      case SDLK_RSHIFT:
	      case SDLK_LALT:
	      case SDLK_RALT:
	      case SDLK_LCTRL:
	      case SDLK_RCTRL:
	      case SDLK_LSUPER:
	      case SDLK_RSUPER:
	      case SDLK_LMETA:
	      case SDLK_RMETA:
	      case SDLK_MENU:
	      case SDLK_INSERT:
	      case SDLK_DELETE:
	      case SDLK_TAB:
	      case SDLK_NUMLOCK:
	      case SDLK_CAPSLOCK:
	      case SDLK_SCROLLOCK:
	      case SDLK_MODE:
	      case SDLK_HELP:
	      case SDLK_PRINT:
	      case SDLK_SYSREQ:
	      case SDLK_BREAK:
	      case SDLK_POWER:
	      case SDLK_EURO:
	         // ignore keypress for meta keys or weird keys we don't

use normally. we handle shift separately.
break;
case SDLK_BACKSPACE:
// handle backspace
break;
default:
// handle all other keys here
{
if (event.key.keysym.mod & KMOD_SHIFT)
{
// handle shifted input
switch(event.key.keysym.sym)
{
case ‘`’:
ch = ‘~’;
break;
// etc etc - all non-alpha characters with
different shift characters go here
}
}
else
{
ch = event.key.keysym.sym;
}
// is it printable?
if (ch >= 32 && ch < 128)
{
// add key to buffer
if ((event.key.keysym.mod & KMOD_SHIFT) ||
(event.key.keysym.mod & KMOD_CAPS))
{
cKeyBuf[iBufPtr++] = toupper(ch);
}
else
{
cKeyBuf[iBufPtr++] = ch;
}
}
}
break;
} // end switch (event.key.keysym.sym)
}//end if event.type == SDK_KEYDOWN
}//end while SDL_PollEvent

Thanks,

-Justin

I’m not sure exactly where the problem is, but here are two things:

I wouldn’t trust using event.key.keysym.sym as a char. It is an
enumeration containing things like SDLK_c, SDLK_ESCAPE, etc. You use
it as a char in the default case, though I’m not sure that is safe.

I think you really want to look into the unicode field
(event.key.keysym.unicode) and SDL_EnableUNICODE().

Jonny DOn Wed, Nov 4, 2009 at 9:58 AM, Justin Coleman wrote:

Good morning all,

I’m having a pretty strange issue with the shift and capslock keys,
using SDL 1.2.13 (the newest version apt-get can find:
libsdl1.2debian-all)on Ubuntu 9.04. I have set up my main loop to
catch key presses and stuff them in a buffer for later use, but I’m
getting really weird results trying to handle the shift/capslock
modifiers. When I have the capslock on, I’m apparently not seeing it
at all, and when I have shift on, I keep getting a repeat of the last
character pressed before the shift key.

For example, when I press the following keys:
abc def ghi shift-j shift-k shift-l
What my program sees is:
abcCCCghiIII

Here’s my event handling code, perhaps someone can spot what I’m doing
wrong? I’ve spent about an hour googling and searching the SDL forum
for something similar, but haven’t had any luck so far.

//Using polling to read input - while will read until all input is
exhausted so input events don’t stack up
? ? ? while (SDL_PollEvent(&event))
? ? ? ? ? {
? ? ? ? ? if (event.type == SDL_KEYDOWN)
? ? ? ? ? ? ?{
? ? ? ? ? ? ?switch(event.key.keysym.sym)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ?case SDLK_ESCAPE: // escape key, bail out
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? bQuit = 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_RIGHT:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (iDesiredScreen < 7) // increase current screen
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen++;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? // or wrap around to beginning
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen = 0;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_LEFT:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (iDesiredScreen > 0) // decrease current screen
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen–;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? // or wrap around to end
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen = 7;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_PAGEUP:
? ? ? ? ? ? ?case SDLK_PAGEDOWN:
? ? ? ? ? ? ?case SDLK_HOME:
? ? ? ? ? ? ?case SDLK_END:
? ? ? ? ? ? ? ? // ignore this key
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_RETURN:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? cKeyBuf[iBufPtr] = 0; // null terminate the buffer string
? ? ? ? ? ? ? ? printf(“Enter key pressed, buffer: [%s]\n”, cKeyBuf);
? ? ? ? ? ? ? ? iBufPtr = 0; // start buffer at beginning again
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ?case SDLK_LSHIFT:
? ? ? ? ? ? ?case SDLK_RSHIFT:
? ? ? ? ? ? ?case SDLK_LALT:
? ? ? ? ? ? ?case SDLK_RALT:
? ? ? ? ? ? ?case SDLK_LCTRL:
? ? ? ? ? ? ?case SDLK_RCTRL:
? ? ? ? ? ? ?case SDLK_LSUPER:
? ? ? ? ? ? ?case SDLK_RSUPER:
? ? ? ? ? ? ?case SDLK_LMETA:
? ? ? ? ? ? ?case SDLK_RMETA:
? ? ? ? ? ? ?case SDLK_MENU:
? ? ? ? ? ? ?case SDLK_INSERT:
? ? ? ? ? ? ?case SDLK_DELETE:
? ? ? ? ? ? ?case SDLK_TAB:
? ? ? ? ? ? ?case SDLK_NUMLOCK:
? ? ? ? ? ? ?case SDLK_CAPSLOCK:
? ? ? ? ? ? ?case SDLK_SCROLLOCK:
? ? ? ? ? ? ?case SDLK_MODE:
? ? ? ? ? ? ?case SDLK_HELP:
? ? ? ? ? ? ?case SDLK_PRINT:
? ? ? ? ? ? ?case SDLK_SYSREQ:
? ? ? ? ? ? ?case SDLK_BREAK:
? ? ? ? ? ? ?case SDLK_POWER:
? ? ? ? ? ? ?case SDLK_EURO:
? ? ? ? ? ? ? ? // ignore keypress for meta keys or weird keys we don’t
use normally. we handle shift separately.
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_BACKSPACE:
? ? ? ? ? ? ? ? // handle backspace
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?default:
? ? ? ? ? ? ? ? ? ?// handle all other keys here
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? if (event.key.keysym.mod & KMOD_SHIFT)
? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?// handle shifted input
? ? ? ? ? ? ? ? ? ? ? ? ?switch(event.key.keysym.sym)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? case ‘`’:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ch = ‘~’;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? // etc etc - all non-alpha characters with
different shift characters go here
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?ch = event.key.keysym.sym;
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? // is it printable?
? ? ? ? ? ? ? ? ? ? ? if (ch >= 32 && ch < 128)
? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?// add key to buffer
? ? ? ? ? ? ? ? ? ? ? ? ?if ((event.key.keysym.mod & KMOD_SHIFT) ?||
(event.key.keysym.mod & KMOD_CAPS))
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? cKeyBuf[iBufPtr++] = toupper(ch);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? cKeyBuf[iBufPtr++] = ch;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? } // end switch (event.key.keysym.sym)
? ? ? ? ? ? ?}//end if event.type == SDK_KEYDOWN
? ? ? ? ? }//end while SDL_PollEvent

Thanks,

-Justin


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You’re probably right in the long run. I checked SDL_keysym.h and in
one of the comments there it states they’re deliberately mapped to
ASCII values for the 0-127 range (with some skipped, including
uppercase letters). The SDL Wiki says Unicode adds some overhead to
events, and I was trying to keep things as light as possible.

I guess I’ll go ahead and change to use Unicode, although anyone who
has insight into the actual issue is more than welcome to chime in :slight_smile:

Thanks Jonny!

-JustinOn Wed, Nov 4, 2009 at 10:14 AM, Jonathan Dearborn wrote:

I’m not sure exactly where the problem is, but here are two things:

I wouldn’t trust using event.key.keysym.sym as a char. ?It is an
enumeration containing things like SDLK_c, SDLK_ESCAPE, etc. ?You use
it as a char in the default case, though I’m not sure that is safe.

I think you really want to look into the unicode field
(event.key.keysym.unicode) and SDL_EnableUNICODE().

Jonny D

On Wed, Nov 4, 2009 at 9:58 AM, Justin Coleman <@Justin_Coleman> wrote:

Good morning all,

I’m having a pretty strange issue with the shift and capslock keys,
using SDL 1.2.13 (the newest version apt-get can find:
libsdl1.2debian-all)on Ubuntu 9.04. I have set up my main loop to
catch key presses and stuff them in a buffer for later use, but I’m
getting really weird results trying to handle the shift/capslock
modifiers. When I have the capslock on, I’m apparently not seeing it
at all, and when I have shift on, I keep getting a repeat of the last
character pressed before the shift key.

For example, when I press the following keys:
abc def ghi shift-j shift-k shift-l
What my program sees is:
abcCCCghiIII

Here’s my event handling code, perhaps someone can spot what I’m doing
wrong? I’ve spent about an hour googling and searching the SDL forum
for something similar, but haven’t had any luck so far.

//Using polling to read input - while will read until all input is
exhausted so input events don’t stack up
? ? ? while (SDL_PollEvent(&event))
? ? ? ? ? {
? ? ? ? ? if (event.type == SDL_KEYDOWN)
? ? ? ? ? ? ?{
? ? ? ? ? ? ?switch(event.key.keysym.sym)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ?case SDLK_ESCAPE: // escape key, bail out
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? bQuit = 1;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_RIGHT:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (iDesiredScreen < 7) // increase current screen
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen++;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? // or wrap around to beginning
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen = 0;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_LEFT:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? if (iDesiredScreen > 0) // decrease current screen
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen–;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? ? // or wrap around to end
? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ?iDesiredScreen = 7;
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_PAGEUP:
? ? ? ? ? ? ?case SDLK_PAGEDOWN:
? ? ? ? ? ? ?case SDLK_HOME:
? ? ? ? ? ? ?case SDLK_END:
? ? ? ? ? ? ? ? // ignore this key
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_RETURN:
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? cKeyBuf[iBufPtr] = 0; // null terminate the buffer string
? ? ? ? ? ? ? ? printf(“Enter key pressed, buffer: [%s]\n”, cKeyBuf);
? ? ? ? ? ? ? ? iBufPtr = 0; // start buffer at beginning again
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ?case SDLK_LSHIFT:
? ? ? ? ? ? ?case SDLK_RSHIFT:
? ? ? ? ? ? ?case SDLK_LALT:
? ? ? ? ? ? ?case SDLK_RALT:
? ? ? ? ? ? ?case SDLK_LCTRL:
? ? ? ? ? ? ?case SDLK_RCTRL:
? ? ? ? ? ? ?case SDLK_LSUPER:
? ? ? ? ? ? ?case SDLK_RSUPER:
? ? ? ? ? ? ?case SDLK_LMETA:
? ? ? ? ? ? ?case SDLK_RMETA:
? ? ? ? ? ? ?case SDLK_MENU:
? ? ? ? ? ? ?case SDLK_INSERT:
? ? ? ? ? ? ?case SDLK_DELETE:
? ? ? ? ? ? ?case SDLK_TAB:
? ? ? ? ? ? ?case SDLK_NUMLOCK:
? ? ? ? ? ? ?case SDLK_CAPSLOCK:
? ? ? ? ? ? ?case SDLK_SCROLLOCK:
? ? ? ? ? ? ?case SDLK_MODE:
? ? ? ? ? ? ?case SDLK_HELP:
? ? ? ? ? ? ?case SDLK_PRINT:
? ? ? ? ? ? ?case SDLK_SYSREQ:
? ? ? ? ? ? ?case SDLK_BREAK:
? ? ? ? ? ? ?case SDLK_POWER:
? ? ? ? ? ? ?case SDLK_EURO:
? ? ? ? ? ? ? ? // ignore keypress for meta keys or weird keys we don’t
use normally. we handle shift separately.
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?case SDLK_BACKSPACE:
? ? ? ? ? ? ? ? // handle backspace
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ?default:
? ? ? ? ? ? ? ? ? ?// handle all other keys here
? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? if (event.key.keysym.mod & KMOD_SHIFT)
? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?// handle shifted input
? ? ? ? ? ? ? ? ? ? ? ? ?switch(event.key.keysym.sym)
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? case ‘`’:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ch = ‘~’;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ? ? ? ? ? // etc etc - all non-alpha characters with
different shift characters go here
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?ch = event.key.keysym.sym;
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? // is it printable?
? ? ? ? ? ? ? ? ? ? ? if (ch >= 32 && ch < 128)
? ? ? ? ? ? ? ? ? ? ? ? ?{
? ? ? ? ? ? ? ? ? ? ? ? ?// add key to buffer
? ? ? ? ? ? ? ? ? ? ? ? ?if ((event.key.keysym.mod & KMOD_SHIFT) ?||
(event.key.keysym.mod & KMOD_CAPS))
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? cKeyBuf[iBufPtr++] = toupper(ch);
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?else
? ? ? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? cKeyBuf[iBufPtr++] = ch;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? } // end switch (event.key.keysym.sym)
? ? ? ? ? ? ?}//end if event.type == SDK_KEYDOWN
? ? ? ? ? }//end while SDL_PollEvent

Thanks,

-Justin


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org