Archived

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

Reading keys on linux

This topic is 5301 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
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
Afaik you have to create a display (via SDL_SetVideoMode) to be able to poll events (Here's the link to the corresponding documentation). As I can see you did'nt even initialize SDL.
Add those two lines in the beginning of your program and it shoud work:

int main(int argc, char *argv[])
{
SDL_Event event;
int gameover=0;

// Add following two lines

SDL_Init(SDL_INIT_VIDEO);
SDL_SetVideoMode(320, 200, 0, SDL_ANYFORMAT);

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


return 0;
}



[edited by - baumep on June 9, 2003 6:01:05 AM]

Share this post


Link to post
Share on other sites
Alright, I''m reading keys with SDL now. This is silly, but now I have one window for GL, and one for SDL. If I''m in openGL window, glut functions work but keys don''t. Keys work when I''m in SDL window, but the GL window is the one that''s supposed to be fullscreen. How am I supposed to tell glut functions that they should draw their stuff to the SDL window? (I assume then it could be fullscreen.)

I downloaded one SDL example code from here, that''s supposed to enable me to use openGL, but I don''t understand anything out of it. I don''t want to just start coping it. (nothing would come out of it either).

Share this post


Link to post
Share on other sites
You just initialize the SDL window with something like


SDL_Surface* screen = SDL_SetVideoMode(1024, 768, 32, SDL_OPENGL );


And then do anything GL. The SDL window will be the context. It works pretty well for me.

Share this post


Link to post
Share on other sites
I checked some h files, and figured out I should use that SDL_OPENGL flag in SDL_SetVideoMode(). Now, if I use glutCreateWindow() also, it creates me another window (which I don''t want to happen). If I don''t use it, however, other glut functions give me Segmentation Fault error message.

Actually: "Fatal signal: Segmentation Fault (SDL Parachute Deployed)"

That sounds like SDL has done something, but:

screen=SDL_SetVideoMode(1024,768,32,SDL_OPENGL);
printf("A\n");
glutDisplayFunc(&DrawGLScene);
printf("B\n");

results in printing only A before error message. So I guess it''s glut functions not wanting to cooperate.

Share this post


Link to post
Share on other sites
I realized I can use glVertex3f() and other stuff without glutDisplayFunc() and those other glut...Func things. So, I got rid of Segmentation Faults and have my keys working!!! Finally!!!

I think I''m running out of problems, but there''s one more thing. (I think this is the last one.) The window remains black, as if I would not be drawing anything there.
There was a line "glutSwapBuffers();" after all the drawing. Now when using SDL window I can''t have that there. Should I put there something that would do the same thing with SDL window''s buffers?

Share this post


Link to post
Share on other sites
Actually I just realized, that I could have checked that SDL_GL_SwapBuffers() from the example code. So, keys and graphics working simultaniusly! Alright.
And hey. These NeHe is a great place. I''ve never received so much reasonable answers to such a long row of questions.

Share this post


Link to post
Share on other sites