Managing key events in SDL and C ++

advertisements

I'm in the process of migrating a program from GLUT to SDL. In my current program pressing the a key results in a different response then pressing the A key. This was pretty straightforward to do in GLUT as it the keyboard function callback passed in the ASCII value of the key that was pressed.

void keyPressedFn(unsigned char key, int x, int y){
    switch(key){
    case 'a':
    // do work for a
    break;
    case 'A':
    // do work for A
    break;
    }
}

I'm struggling to replicate similar functionality in SDL as pressing the a key produces the same response regardless of if SHIFT or CAPS LOCK are pressed as well.

Is there a simple way of replicating the above function in SDL?

Edit: In the example above I only show how to handle one key, in practice however, I am going to have a list of about 15 keys that I want to respond to differently if the shift key is pressed as well.


Check the keyboard modifiers that are present when you get a keydown event. For example:

while(SDL_PollEvent(&event))
{
  switch(event.type)
  {
  case SDL_KEYDOWN:
    if(event.key.keysym.sym == SDLK_a)
    {
      if(event.key.keysym.mod & KMOD_SHIFT)
      {
        // Handle 'A'
      }
      else
      {
        // Handle 'a'
      }
    }
    break;

  ...

  }
}