I'm writing a game in C with GTK+ 2.0. It creates a full-screen window with a drawing area and uses it.The game logics runs in a timeout callback and needs to know if a key is pressed at the moment or not.5 keys are being tracked: [Up]. [Right], [Down], [Left] and [Space].To do this, I have an array of 5 gboolean values.

Code:

gboolean keys[5];

I also connect a callback function that saves the current key (up/down) state:

This basically assigns TRUE or FALSE to the [0 .. 4] elements of the array.Everything is working, except one thing:If in the game you hold [Up] and [Space] in the same time and then press [Left]. (i.e. three keys simultaniously), the application will not notice anything.If you do the same but press the [Right] key, it will work with no problem.It works perfectly if I change [Space] to [A] key.

It looks like [Space] key is blocking [Left] key if [Up] is also pressed.I first thought it is somehow connected to the fact that if a key is held down, key press events keep coming repeatedly. (like when you hold "a" you get "aaaaaaaaa"), but that doesn't make sense, and it is not possible in GTK+ to disable repeating keys in a cross-platform way.

I've also tried:

Code:

g_signal_connect(G_OBJECT(window), "key_press_event", ... );

instead of:

Code:

gtk_key_snooper_install(on_key_press, ... );

but it doesn't make any difference.

I'm using Debian.

P.S.: While I was writing this message, I noticed that the same is happening with the [G] key. [G] is blocking [Left] in the same way, and doesn't affect the [Right] key.In the same time [C] works perfectly (like [A]). May be these are some special hotkeys?

P.S.#2: Now I noticed that if you hold down [T+G], keys [Up] and [Right] are blocked, whereas [Left] and [Down] work! What is that???

Have you tried this with other keyboards. What I think you have come across is a limitation in the hardware of the keyboard itself. The keyboard is normally made up of an X-Y matrix and not individual switches which is the scanned for key presses or releases. This matrix may mask some keys if others are already pressed. Also the processor in the keyboard may limit the number of keys it can keep track of to 3. But some times reserving one of those for processing the meta keys (shift L, shift R, Alt etc...)

I would advise against asking for the user to hold down more than two keys at a time, as keyboards can vary.

As a note your application worked on my system Debian Wheezy(testing) using GTK+ 2.24. I am using a logitech USB keyboard.

IMHO, event model used in GTK is not suitable for game programming. Users would like to hold the "FIre" key. Therefore, the related event handler is continually called and any other things are blocked.

The alternative way is to check the keyboard regularly, say 30 times a second. This is sufficient since humans actually cannot press (down and up) keyboard very fast.

Who is online

Users browsing this forum: Google [Bot] and 3 guests

You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum