Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Plasmite

Reading keys on linux

This topic is 5612 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want my program to know what keys are being pressed. (So that more than only one key can be pressed simultaneously.) Over here I can see some talk about DirectInput, but isn''t that for Windows only? I''m programming in Linux. How could my program read keys?

Share this post


Link to post
Share on other sites
Advertisement
perhaps checking out SDL at http://www.libsdl.org, would be beneficial for you. I know that they do have events that are generated, and listeners for when the keys are pressed and such, and this being a Game development site, it seems to fit.

Share this post


Link to post
Share on other sites
Thanks, I think this SDL will be solution to the problem. However, I have a little problem with it: I have a buffer for keys "Uint8 *keys;", and then I try to get states of all keys into it "keys = SDL_GetKeyState(NULL);" but for some reason everything in the buffer remains zero, no matter how much I keep pressing keys on my board. Is my code missing something essential? (What is Uint8? It sounds like a variable.)

Share this post


Link to post
Share on other sites
quote:
Original post by Plasmite
Thanks, I think this SDL will be solution to the problem. However, I have a little problem with it: I have a buffer for keys "Uint8 *keys;", and then I try to get states of all keys into it "keys = SDL_GetKeyState(NULL);" but for some reason everything in the buffer remains zero, no matter how much I keep pressing keys on my board. Is my code missing something essential? (What is Uint8? It sounds like a variable.)


You still have to poll the event handler to fill in the array. Calling SDL_PollEvent(&event) before calling keys = SDL_GetKeyState(NULL) will fill in the array with all of the states of the keys. Note that you must do this periodically, as calling each once will just leave the array with the state at the time you called it.

Share this post


Link to post
Share on other sites
Somehow I''m getting a feeling that I don''t know something I should know, but well... this is a beginners forum.
In a example code I downloaded from here, SDL was used like this:

if(SDL_PollEvent(&E)) {
switch(E.type)
...
case SDL_KEYDOWN:
Keys=GetKeyState(NULL);
break;
}

It doesn''t work when I try it. Obviously SDL_PollEvent always retuns a zero to me, because my programm doesn''t do anything after the "if". (I checked it by putting printf("Hello.\n"); after if. Nothing comes out ever.)

I tried printing this right after the SDL_PollEvent(NULL);
printf("%d %d %d\n", E.key.type, E.key.which, E.key.state);
Hoping to see something, that would have something to do with what I press on my keyboard. But the print is always the same
"1 0 0". So, SDL_PollEvent isn''t doing anything. What should I do before this Poll thing to make it work?

Share this post


Link to post
Share on other sites
If you want to check an individual key then you could do this:

SDL_PollEvent(&event);

if(event.type.keysym.sym == SDLK_UP)
{
//do whatever you need to do for this key press

}


EDIT: you can find the full list of all the keys and their corresponding SDLK_XXX values form the SDL docs

[edited by - Spudder on June 8, 2003 9:03:52 AM]

Share this post


Link to post
Share on other sites
SDL_PollEvent() doesn''t do anything in my program. It always returns zero, nothing happens nowhere. I have just started using Linux, and I don''t know much of these Event things yet. How should I tell SDL functions, that I want them to react to such events as pressing and releasing keys?

(As you can see, one of my problems is that I don''t know what my problem actually is. I think events are my problem now.)

Share this post


Link to post
Share on other sites
GRGHH... I want this program to do nothing, and to termite it self when I press ESC or Q. Why is it not doing this?

#include <SDL/SDL.h>

int main() {

SDL_Event event;
int gameover=0;
while(!gameover) {
if(SDL_PollEvent(&event)) {

switch(event.type) {
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_q:
gameover=1;
break;
} // end switch
break;
} // end switch

} // end if
} // end while

} // end main

(Simple things can become difficult sometimes)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!