keyboard buffer

BlitzMax Forums/BlitzMax Programming/keyboard buffer

Cajun17(Posted 2005) [#1]
The keyboard input inconsistencies are getting on my nerves. As far as I can tell GetChar() returns the Ascii code of the most recently pressed key from the buffer. That's fine and dandy except the buffer only holds codes 0-127. What about the directional keys, number pad, F#, etc?

I could fill the gaps, but I don't want to use KeyHit or KeyDown for 2 reasons.
1)The order the keys are hit matters.

2)An aplication that uses a lot of keys would be very slow if I had to check every key every loop.

3)Half of my input comes in Ascii while keyhit & keydown return keycodes so comparing could get messy

So anyone have a solution or suggestion?


gellyware(Posted 2005) [#2]
Take a look at these two commands:

Asc and Chr

'ASC
'Get character value of the first character of a String

print Asc("A") '65
Print "A"[0] '65 - equivalent index style implementation

'CHR
'Chr returns a String of length 1 containing the unicode character of the value.

print Chr(65) 'A


Also you may want to look at the docks under keyHit() and keyDown(). You will find a table for keycodes that you can use such as:

if keyHit(KEY_ESCAPE) then ...




hope this helps.


Cajun17(Posted 2005) [#3]
Yes I know about those commands maybe I didn't explain well enough.

If you look at an Ascii table and then the Blitz key codes you'll notice that they're similar, but not the same.

When you use KeyHit() and KeyDown() the values are given as key codes.

To poll the whole keyboard is 100+ checks every loop. Now there's a command called GetChar() that returns the next character in the key buffer.

In other words if I hit 'a' 5 times I can read from the buffer until it's empty instead of checking every key. Also The order the keys is pressed can be preserved. There are 2 problems with this command.

1) GetChar() returns the Ascii code so Asc("a") = 97 <> Asc("A") = 65 while they are the same in keycodes. This can be worked around, but it's annoying.

2) The bigger problem is that seemingly only ascii codes 0-127 are supported by GetChar() so things like the directional arrows and the F1-F12 buttons can't be read from the buffer.

Anyway I don't think there's an easy solution, but the gui mod might provide a fuller input implementation.


Robert(Posted 2005) [#4]

To poll the whole keyboard is 100+ checks every loop.


If you look at the code for KeyHit and KeyDown, you'll see that all they do is return values from an array. Therefore the "100+" checks would take no time at all.


Nigel Brown(Posted 2005) [#5]
Or, perhaps you could use the method (GetChar()-32) and use the KeyHit() for the arrow keys and F1-F12?


Cajun17(Posted 2005) [#6]
Nigel, I think using half and half would be more trouble then it's worth, but it could work.

Robert, I did look at the code, but checking every key every loop is still slower then handling only the keys you know were used. This is for a UI module so I'm trying to make it bledding lightning fast.

Anyway, I found a solution. Only poll the keys that are being used by the application, (Duh).

Thanks for the replies fellas.