Keyboard Input

Hi,

I’m using SDL to read in from the keyboard. I test to see if the q key
is pressed and go from there. For some reason it detects the key
pressed even if nothing at all is pressed. I’m not sure what I’m doing
wrong.

Here’s the code I’m using

Uint8 *keys;

  keys = SDL_GetKeyState(NULL);
  if (keys[SDLK_q] = SDL_PRESSED)
  {
    gameOver = 1;
  }
  else
  {
    gameOver = 0;
  }

I know this is probably stupid and easy. I’m lost though :frowning:

Mike.

Uint8 *keys;

 keys = SDL_GetKeyState(NULL);
 if (keys[SDLK_q] = SDL_PRESSED)
                    ^
 {
   gameOver = 1;
 }
 else
 {
   gameOver = 0;
 }

== not =.

“Dave Ashley (SDL list)” wrote:

Uint8 *keys;

 keys = SDL_GetKeyState(NULL);
 if (keys[SDLK_q] = SDL_PRESSED)
                    ^
 {
   gameOver = 1;
 }
 else
 {
   gameOver = 0;
 }

== not =.

You said on your website you were new to C so this probably isn’t a
typo, as Dave said you use == if you want to mean “is keys[SDLK_q] equal
to SDL_PRESSED” you use = if you want to set the value of keys[SDLK_q]
to SDL_PRESSED.

John Garrison wrote:

“Dave Ashley (SDL list)” wrote:

Uint8 *keys;

 keys = SDL_GetKeyState(NULL);
 if (keys[SDLK_q] = SDL_PRESSED)
                    ^
 {
   gameOver = 1;
 }
 else
 {
   gameOver = 0;
 }

== not =.

You said on your website you were new to C so this probably isn’t a
typo, as Dave said you use == if you want to mean “is keys[SDLK_q] equal
to SDL_PRESSED” you use = if you want to set the value of keys[SDLK_q]
to SDL_PRESSED.

ok, I tried that, it goes only goes to the else statement now, even if
the key is pressed. I’m probably missing the code to read a keypress I
guess, I tried looking at the aliens demo, but got confused by it :frowning:

Mike.

Mike McLean wrote:

John Garrison wrote:

“Dave Ashley (SDL list)” wrote:

Uint8 *keys;

 keys = SDL_GetKeyState(NULL);
 if (keys[SDLK_q] = SDL_PRESSED)
                    ^
 {
   gameOver = 1;
 }
 else
 {
   gameOver = 0;
 }

== not =.

You said on your website you were new to C so this probably isn’t a
typo, as Dave said you use == if you want to mean “is keys[SDLK_q] equal
to SDL_PRESSED” you use = if you want to set the value of keys[SDLK_q]
to SDL_PRESSED.

ok, I tried that, it goes only goes to the else statement now, even if
the key is pressed. I’m probably missing the code to read a keypress I
guess, I tried looking at the aliens demo, but got confused by it :frowning:

I’ve only used SDL for input once myself, but I had to setup an event. Try
adding this to your variable list:

SDL_Event event;

and adding this right before “keys = SDL_GetKeyState(NULL);”

SDL_PollEvent(&event)

If “keys = SDL_GetKeyState(NULL);” is in a loop (for, while, do, etc.) then
you will need to put SDL_PollEvent($event) in the loop as well.>

Mike.

John Garrison wrote:

Mike McLean wrote:

John Garrison wrote:

“Dave Ashley (SDL list)” wrote:

Uint8 *keys;

 keys = SDL_GetKeyState(NULL);
 if (keys[SDLK_q] = SDL_PRESSED)
                    ^
 {
   gameOver = 1;
 }
 else
 {
   gameOver = 0;
 }

== not =.

You said on your website you were new to C so this probably isn’t a
typo, as Dave said you use == if you want to mean “is keys[SDLK_q] equal
to SDL_PRESSED” you use = if you want to set the value of keys[SDLK_q]
to SDL_PRESSED.

ok, I tried that, it goes only goes to the else statement now, even if
the key is pressed. I’m probably missing the code to read a keypress I
guess, I tried looking at the aliens demo, but got confused by it :frowning:

I’ve only used SDL for input once myself, but I had to setup an event. Try
adding this to your variable list:

SDL_Event event;

and adding this right before “keys = SDL_GetKeyState(NULL);”

SDL_PollEvent(&event)

If “keys = SDL_GetKeyState(NULL);” is in a loop (for, while, do, etc.) then
you will need to put SDL_PollEvent($event) in the loop as well.

Mike.

Wow, it worked thanks. That’s very simple.

Mike.

PS thanks again

I just want the results of the keyboard input, and I am not real
concerned about which actual key is in use. For example, if num lock
is depressed and you push the ‘1’ you get a ‘1’ back and not a key
symbol with the 1 key pad code which is different that the 1 key code.

Does the following code look right, or is there a better way? This is
for my US English keyboard.

/*******************************************************************************

  • get_char.c
  • Copyright © 2006 Chris Nystrom
  • This program is public domain.
  • This program is distributed in the hope that it will be useful,
  • but without any warranty whatsoever.
  • Contact Author at:
  • E-Mail: cnystrom at gmail.com
  • Web: http://www.newio.org
  • AIM: nystromchris
  • Soli Deo Gloria

*/

#include <stdlib.h>
#include “SDL.h”

int get_char(SDL_keysym sym)
{
int c;

    // Do not want the modifier keys by themselves
    if (sym.sym >= SDLK_NUMLOCK && sym.sym <= SDLK_COMPOSE) {
            return (0);
    }

    c = sym.sym;

    if ((sym.mod & KMOD_LSHIFT) || (sym.mod & KMOD_RSHIFT)
        || (sym.mod & KMOD_CAPS)) {

            // Get upper case
            if (c >= SDLK_a && c <= SDLK_z) {
                    c = c - 32;
            } else if (c == SDLK_BACKQUOTE) {
                    c = '~';
            } else if (c == SDLK_1) {
                    c = SDLK_EXCLAIM;
            } else if (c == SDLK_2) {
                    c = SDLK_AT;
            } else if (c == SDLK_3) {
                    c = SDLK_HASH;
            } else if (c == SDLK_4) {
                    c = SDLK_DOLLAR;
            } else if (c == SDLK_5) {
                    c = '%';
            } else if (c == SDLK_6) {
                    c = SDLK_CARET;
            } else if (c == SDLK_7) {
                    c = SDLK_AMPERSAND;
            } else if (c == SDLK_8) {
                    c = SDLK_ASTERISK;
            } else if (c == SDLK_9) {
                    c = SDLK_LEFTPAREN;
            } else if (c == SDLK_0) {
                    c = SDLK_RIGHTPAREN;
            } else if (c == SDLK_MINUS) {
                    c = SDLK_UNDERSCORE;
            } else if (c == SDLK_EQUALS) {
                    c = SDLK_PLUS;
            } else if (c == SDLK_LEFTBRACKET) {
                    c = '{';
            } else if (c == SDLK_RIGHTBRACKET) {
                    c = '}';
            } else if (c == SDLK_SEMICOLON) {
                    c = SDLK_COLON;
            } else if (c == SDLK_QUOTE) {
                    c = SDLK_QUOTEDBL;
            } else if (c == SDLK_COMMA) {
                    c = SDLK_LESS;
            } else if (c == SDLK_PERIOD) {
                    c = SDLK_GREATER;
            } else if (c == SDLK_SLASH) {
                    c = SDLK_QUESTION;
            } else if (c == SDLK_BACKSLASH) {
                    c = '|';
            }
    }
    // control keys
    if ((sym.mod & KMOD_LCTRL) || (sym.mod & KMOD_RCTRL)) {
            if (c >= SDLK_a && c <= SDLK_z) {
                    c = c - 96;
            }
    }
    // numeric key pad
    if (c == SDLK_KP_DIVIDE) {
            c = SDLK_SLASH;
    } else if (c == SDLK_KP_MULTIPLY) {
            c = SDLK_ASTERISK;
    } else if (c == SDLK_KP_MINUS) {
            c = SDLK_MINUS;
    } else if (c == SDLK_KP_PLUS) {
            c = SDLK_PLUS;
    } else if (c == SDLK_KP_ENTER) {
            c = SDLK_RETURN;
    }

    if (sym.mod & KMOD_NUM) {
            if (c == SDLK_KP0) {
                    c = SDLK_0;
            } else if (c == SDLK_KP1) {
                    c = SDLK_1;
            } else if (c == SDLK_KP2) {
                    c = SDLK_2;
            } else if (c == SDLK_KP3) {
                    c = SDLK_3;
            } else if (c == SDLK_KP4) {
                    c = SDLK_4;
            } else if (c == SDLK_KP5) {
                    c = SDLK_5;
            } else if (c == SDLK_KP6) {
                    c = SDLK_6;
            } else if (c == SDLK_KP7) {
                    c = SDLK_7;
            } else if (c == SDLK_KP8) {
                    c = SDLK_8;
            } else if (c == SDLK_KP9) {
                    c = SDLK_9;
            } else if (c == SDLK_KP_PERIOD) {
                    c = SDLK_PERIOD;
            }
    } else {
            if (c == SDLK_KP0) {
                    c = SDLK_INSERT;
            } else if (c == SDLK_KP1) {
                    c = SDLK_END;
            } else if (c == SDLK_KP2) {
                    c = SDLK_DOWN;
            } else if (c == SDLK_KP3) {
                    c = SDLK_PAGEDOWN;
            } else if (c == SDLK_KP4) {
                    c = SDLK_LEFT;
            } else if (c == SDLK_KP5) {
                    c = 0;
            } else if (c == SDLK_KP6) {
                    c = SDLK_RIGHT;
            } else if (c == SDLK_KP7) {
                    c = SDLK_HOME;
            } else if (c == SDLK_KP8) {
                    c = SDLK_UP;
            } else if (c == SDLK_KP9) {
                    c = SDLK_PAGEUP;
            } else if (c == SDLK_KP_PERIOD) {
                    c = SDLK_DELETE;
            }
    }

    return (c);

}

int main(int argc, char **argv)
{
int c;
SDL_Event ev;
SDL_keysym sym;

    SDL_Init(SDL_INIT_EVERYTHING);

    SDL_SetVideoMode(640, 480, 0, SDL_ANYFORMAT);

    while (SDL_WaitEvent(&ev)) {

            switch (ev.type) {
            case SDL_QUIT:
                    exit(0);
                    break;

            case SDL_KEYDOWN:
                    sym = ev.key.keysym;
                    c = get_char(sym);
                    if (0 != c) {
                            printf("key = %d : %c\n", c, c);
                            fflush(stdout);
                    }

                    if (c == SDLK_ESCAPE) {
                            SDL_Quit();
                            exit(0);
                    }
                    break;

            default:
                    break;
            }
    }

    SDL_Quit();
    exit(0);

}


E-Mail: Chris Nystrom
http://www.newio.org/
AIM: nystromchris

Try calling SDL_EnableUNICODE(1) and using the keysym.unicode value -
that’s the actual letter typed.

– JoshOn 2/25/06, Chris Nystrom wrote:

I just want the results of the keyboard input, and I am not real
concerned about which actual key is in use. For example, if num lock
is depressed and you push the ‘1’ you get a ‘1’ back and not a key
symbol with the 1 key pad code which is different that the 1 key code.

Does the following code look right, or is there a better way? This is
for my US English keyboard.