Archived

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

speeding up keyboard event processing(in linux)

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

Recommended Posts

Hi all, I''m working on a tetris clone to get a grasp on game programming(like so many other people around here ) and everything is going well with one exception, i''m experiencing a lot of lag with keyboard input. Just as some background info, i''m using the X11 and OpenGL libraries for windowing, communicating with the keyboard, etc. Here''s a snippet of my code to give you an idea of what i''ve got so far for keyboard checking: // In the main game loop switch(event.type) { . . . case KeyPress: { ks = XLookupKeysym( (XKeyEvent *) &event, 0); keyPressed(ks); break; } . . . } //in KeyPressed(key) switch(key) { . . . case XK_Left: { if(blockpos > -10 // bounds checking MoveLeft(); // moves the block left break; } case XK_Right: { if(blockpos < 10) MoveRight(); break; } . . . } The problem comes if you choose to hold the left or right key down(or up key, i just didn''t include the code) instead of tapping the key. I''m guessing what''s going on is that a lot of events are building up in a queue and it''s having to process them all before it can move on. Anyhow, if anyone has any suggestions on how to avoid this problem the help would be greatly appreciated. Thanks, Crimson

Share on other sites
I don''t know what''s wrong with your stuff, but here''s a little code on how I do it (for multiple keypresses):

  char keys[0xFFFF];int key;int events;XEvent event;events = XPending(display ... );while (events--) { XNextEvent(display ... &event); if (event.type == KeyPress) { key = event.keycode; keys[key] = 1; } if (event.type == KeyRelease) { key = event.keycode; keys[key] = 0; key = 0; }}

Note that I wrote this in a very short time. I''ve left some work to you.
I use it for most programs, and I haven''t noted any "lagging" yet.

Share on other sites
Its been a while since I did straight up X11 programming with the XLib event structure but I think that it registers _BOTH_ a key down and a key up. So when you hold down the key you should only get one event and when you let up you''ll get another event. Is this correct? I''m not sure if you get a "key still down" event when you hold down the key, but if you do maybe you can change the frequency at which it sends these messages.

This probably wasn''t any help at all.

Share on other sites
KeyRelease was what i was missing. Thanks.

I do have one more quick question.
I was using printf to output event.xkey.keycode to the screen. It showed the Escape key as having a value of 9, the down arrow as 100, and so on. What i was wondering was, is this constant or does it change from keyboard to keyboard and/or machine to machine(linux distro differences maybe)?

Thanks,
Crimson