Allegro.cc - Online Community

Allegro.cc Forums » Allegro Development » Could KEY_DOWN, KEYCHAR and KEYUP all have the modifier and unichar fields?

This thread is locked; no one can reply to it. rss feed Print
Could KEY_DOWN, KEYCHAR and KEYUP all have the modifier and unichar fields?
jmasterx
Member #11,410
October 2009

I'm fine with the fact that keychar event has the repeat flag, but the fact that keydown no longer has modifiers and unicode broke my GUI API in a way that I really do not know how to fix it. My GUI API is backend based so that's why I used unichar, and then used a switch case for things like meta, alt, shift, etc. I now find myself in a situation where I have no way to determine if the key I pressed will fire a keychar event (atleast not without a big switch case).

I think it would be a good idea if all 3 key events carried around modifier and unicode information. My original issue was in the fact that keyup did not carry unicode, and now keydown does not either. If it is currently assumed that keydown will never produce a unicode, then it shouldn't hurt anything if they all have it. Filling 2 extra ints each keystroke isn't that expensive imo.

Thanks

Peter Wang
Member #23
April 2000

I'm not quite sure what you're asking for. The unused fields are always filled with zero if that helps.

Matthew Leverton
Supreme Loser
January 1999
avatar

jmasterx said:

Filling 2 extra ints each keystroke isn't that expensive imo.

That's not the issue. The problem is that KEY_DOWN and KEY_UP don't necessarily correspond to unicode characters. It doesn't really make sense for them to include that information. They represent key presses. the KEY_CHAR event represents a unicode character.

So if you have, say, a widget for entering text, you'd use KEY_CHAR for displaying the characters and KEY_DOWN for backspace.

Peter Wang
Member #23
April 2000

Actually you should use KEY_CHAR for back space as well as you want auto repeat. KEY_CHAR is appropriate for almost anything in a gui I think.

jmasterx
Member #11,410
October 2009

What I mean is, for example in the button, when I get a key down, I check if it is space and if it is, it makes the button looked pushed in until space is released. If a key char was not preceded by a key down and was its own thing, I'd just treat them both as key downs. The problem if I do that is that I'd get sometimes 2 keydowns in my gui hence my issue.

Thomas Fjellstrom
Member #476
June 2000
avatar

You don't need to track key down and key up anymore. You'll get space and backspace events from KEY_CHAR. Skip the keydown entirely, it aught to work fine.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

jmasterx
Member #11,410
October 2009

Yes I understand that but, I designed it to be like Windows controls. So in Windows if you push down on space the button looks pushed in until space is released. If I was doing this Allegro specific, I'd use the keycode for everything, but I don't since it also works with SDL. Keychar works fine for textboxes and what not yes, but when I need to know when its pressed and released it is an issue.

In C# a key down allows you to go: if e.keycode == KEYS_A
which I would then use allegros keycode for that but since it is independent I rely on modifier and unicode field

Otherwise I'd have to wrap allegros keycode or something which might not work with all other backends

Thomas Fjellstrom
Member #476
June 2000
avatar

objects that want character input should take CHAR events, ones that want to track key state should use DOWN/UP events.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

jmasterx
Member #11,410
October 2009

Yes that's what I was already doing for my GUI, except I generated my own keychar events based on the keydown, but I don't know how to do this anymore with the new design because the keydown only gives me a keycode which doesn't work for me, I still need a way to kow if there was a capitol J for instance which I could manually do by checking for shift when I get a keycode of allegro_key_j but this seems error prone when allegro can do it for me.

could there simply just then be a way to know if a keydown is about to produce a keychar? I could would with that.

Thomas Fjellstrom
Member #476
June 2000
avatar

So if your gui already splits them up, why not just map allegro's events directly to your gui events?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

jmasterx
Member #11,410
October 2009

It would be easier and more flexable if there was a boolean indicating if the keydown will also produce a keychar. That gives users the flexibility of doing whatever without having those fields.

Even though my gui is split up it still wants unichar on keydown and keyup

Thomas Fjellstrom
Member #476
June 2000
avatar

Char events don't map 1:1 with keydown or keyup. Anything else will just give you wrong results in most locales.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

jmasterx
Member #11,410
October 2009

Wouldn't pressing shift result in a keydown but no keychar, then pressing j while holding shift result in a keydown followed by a keychar event that would have a J unicode value in it right?

I really have no other way to fix this :(

Thomas Fjellstrom
Member #476
June 2000
avatar

Shift isn't its own key afaik. Its a modifier on a key. So you'd only get the key down, once you hit another key with shift. Then you'd also get a key char from it.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

jmasterx
Member #11,410
October 2009

Exactly, that is why I proposed just having a boolean that just notifies the user if this keydown will produce a keychar, this way one could ignore that keydown and wait for the keychar if that is their design. This would allow me to control what I consider is a keychar and what is a keydown and so my keydown would have the unichar field.

This is a example of what I mean. The keydown does infact allow them to query numbers and letters, but then keychar is used for the keypress which is like my design, but they still have a way of knowing if '0' was pressed on keydown which is what I do but I use the unichar field.

#SelectExpand
1' Boolean flag used to determine when a character other than a number is entered. 2Private nonNumberEntered As Boolean = False 3 4 5' Handle the KeyDown event to determine the type of character entered into the control. 6Private Sub textBox1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) _ 7 Handles textBox1.KeyDown 8 ' Initialize the flag to false. 9 nonNumberEntered = False 10 11 ' Determine whether the keystroke is a number from the top of the keyboard. 12 If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then 13 ' Determine whether the keystroke is a number from the keypad. 14 If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then 15 ' Determine whether the keystroke is a backspace. 16 If e.KeyCode <> Keys.Back Then 17 ' A non-numerical keystroke was pressed. 18 ' Set the flag to true and evaluate in KeyPress event. 19 nonNumberEntered = True 20 End If 21 End If 22 End If 23 'If shift key was pressed, it's not a number. 24 If Control.ModifierKeys = Keys.Shift Then 25 nonNumberEntered = true 26 End If 27End Sub 'textBox1_KeyDown 28 29 30' This event occurs after the KeyDown event and can be used 31' to prevent characters from entering the control. 32Private Sub textBox1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) _ 33 Handles textBox1.KeyPress 34 ' Check for the flag being set in the KeyDown event. 35 If nonNumberEntered = True Then 36 ' Stop the character from being entered into the control since it is non-numerical. 37 e.Handled = True 38 End If 39End Sub 'textBox1_KeyPress

Evert
Member #794
November 2000
avatar

Shift isn't its own key afaik. Its a modifier on a key. So you'd only get the key down, once you hit another key with shift.

If that were the case, it'd be a lot harder to map shift to an action key in your game.
At least on OS X this isn't true.

jmasterx
Member #11,410
October 2009

Well I fixed it by doing:

#SelectExpand
1 //key down 2 case ALLEGRO_EVENT_KEY_DOWN: 3 if(al_peek_next_event(queue,&peekEvent)) 4 if(peekEvent.type != ALLEGRO_EVENT_KEY_CHAR) 5 { 6 handleKeyDown(createKeyboard(&event->keyboard,true)); 7 } 8 9 break; 10 11 //key char 12 case ALLEGRO_EVENT_KEY_CHAR: 13 if(event->keyboard.repeat) 14 { 15 handleKeyRepeat(createKeyboard(&event->keyboard,false,true)); 16 } 17 else 18 { 19 handleKeyDown(createKeyboard(&event->keyboard,true)); 20 } 21 22 break;

torhu
Member #2,727
September 2002
avatar

From looking at the source, it appears that you'll always get a KEY_CHAR for every KEY_DOWN. Doesn't that solve the problem?

jmasterx
Member #11,410
October 2009

Yes, as I said, I fixed it, it's still a bit hackier than I wanted though

Go to: