Could KEY_DOWN, KEYCHAR and KEYUP all have the modifier and unichar fields?
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.
I'm not quite sure what you're asking for. The unused fields are always filled with zero if that helps.
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.
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.
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.
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.
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
objects that want character input should take CHAR events, ones that want to track key state should use DOWN/UP events.
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.
So if your gui already splits them up, why not just map allegro's events directly to your gui events?
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
Char events don't map 1:1 with keydown or keyup. Anything else will just give you wrong results in most locales.
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
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.
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.
1' Boolean flag used to determine when a character other than a number is entered.
2Private nonNumberEntered As Boolean = False
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
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
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
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.
Well I fixed it by doing:
1 //key down
11 //key char
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?
Thread #605855. Printed from Allegro.cc
Yes, as I said, I fixed it, it's still a bit hackier than I wanted though