Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Key Pad and Shift - How?

This thread is locked; no one can reply to it. rss feed Print
Key Pad and Shift - How?
Brigand
Member #16,665
April 2017

I apologize if this has already been answered, though I couldn't seem to find anything.

I am trying to implement a system where the user uses the keypad for navigation, and can change the behavior of the keypad based on whether they are holding down ALT, CTRL, or SHIFT.

For ALT and CTRL, no problem - holding one of these keys and pressing a key pad number produces the desired result. However, with SHIFT the key pad behaves as if SHIFT isnt pressed at all.

I though this was an artifact or bug with using al_get_keyboard_state (or maybe desired behavior for some inscrutable reason), so I created my own keyboard state handler for the modifier keys....and ended up with the same results.

Baffled, I output the events and keycodes that were being processed, and I found out something strange: when you press a key on the keypad while holding one of the SHIFT keys, allegro actually creates an event simulating the SHIFT key being released, the keypad being pressed, and then the SHIFT being repressed. Or maybe, the keyboard controller itself does it, and it has nothing to do with allegro?

This behavior makes my manual state processing irrelevant, and I can't seem to find a way to detect a SHIFT + key pad press.

Am I missing something? This is clearly not a bug in the implementation - there are 2 separate, deliberate keyboard events (SHIFT UP, then SHIFT DOWN) happening that were not generated by the keyboard.

Is there some way to work around this?

Polybios
Member #12,293
October 2010

What operating system and Allegro version are you using?

The behavior you describe does not occur on my system (Linux, current git version of Allegro) with ex_keyboard_events.

Brigand
Member #16,665
April 2017

Windows 10 (64bit), Allegro 5.0.10

I should also add (I just found out)....this only occurs when NumLock is ON... Behavior is as expected when NumLock is off.

As a side question, is it possible to disable numlock via Allegro, at least as far as what event it reads?

Chris Katko
Member #1,881
January 2002
avatar

Is it a laptop? Is it numlock... or FN lock?

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Polybios
Member #12,293
October 2010

Brigand said:

As a side question, is it possible to disable numlock via Allegro, at least as far as what event it reads?

Only the unichar part of the events should be different, the keycode should be the same. So pressing 7 / Home should produce a 044, ALLEGRO_KEY_PAD_7 ("KP_Home") keycode, regardless whether numlock is enabled.

You can change the keyboard LED state, but it seems to merely override keyboard state. I haven't tried though.

Just to make sure: Does the ex_keyboard_events example report the same sequence of events?

torhu
Member #2,727
September 2002
avatar

Brigand said:

Baffled, I output the events and keycodes that were being processed, and I found out something strange: when you press a key on the keypad while holding one of the SHIFT keys, allegro actually creates an event simulating the SHIFT key being released, the keypad being pressed, and then the SHIFT being repressed. Or maybe, the keyboard controller itself does it, and it has nothing to do with allegro?

It's probably related to the behavior of the Shift key when NumLock is on. For instance, if you hold Shift and press NumPad 2 with NumLock enabled, it acts like down arrow. It's not Allegro doing this, the extra events are coming from Windows.

You could ask the user to disable NumLock, or find a different solution, like using Space instead of Shift.

Chris Katko
Member #1,881
January 2002
avatar

I was just playing a game in DOSBOX , on Win7 64-bit, and changing numberlock affected whether or not I could go diagonal directions.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

Polybios
Member #12,293
October 2010

torhu said:

the extra events are coming from Windows.

That's right. I've reproduced it under Windows 7 with ex_keyboard_events, holding shift and pressing 7 on the keypad with numlock on:

KEY_DOWN  code=215, char=' ' (   0), modifiers=00000000, [LSHIFT]
KEY_UP    code=215, char=' ' (   0), modifiers=00000000, [LSHIFT]  <--- held down
KEY_DOWN  code=044, char=' ' (   0), modifiers=00000000, [PAD 7]
KEY_CHAR  code=044, char=' ' (   0), modifiers=00000200, [PAD 7]
repeat    code=044, char=' ' (   0), modifiers=00000200, [PAD 7]
...
repeat    code=044, char=' ' (   0), modifiers=00000200, [PAD 7]
KEY_UP    code=044, char=' ' (   0), modifiers=00000000, [PAD 7]
KEY_DOWN  code=215, char=' ' (   0), modifiers=00000000, [LSHIFT]  <--- still held down
KEY_UP    code=215, char=' ' (   0), modifiers=00000000, [LSHIFT]

So one press of PAD_7 while LSHIFT is being held down produces KEY_UP for LSHIFT, KEY_DOWN for PAD_7, ..., KEY_UP for PAD_7 and KEY_DOWN for LSHIFT each time PAD_7 is pressed.

It does not happen with CTRL or ALT, CAPSLOCK state is irrelevant.

Go to: