New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[core] IsKeyPressed / GetKeyPressed #535
Comments
If you're looking for a good design to work from, BearLibTerminal has an event model worth stealing. http://foo.wyrd.name/en:bearlibterminal Events are always in a queue. You can "read" an event to get its value, which also consumes it, or you can "peek" without consuming it. There are convenience functions to test whether keys are down (I think). There is also a completely separate API for handling "string" input, for things like multi-character UTF-8 input. Event handling is very much worth getting right. |
Hi @irskep! Thanks for the reference! Actually, redesigning that functionality is planned but not enough resources to make it soon. |
For the Raspberry Pi platform this was fixed in #785 by introducing a FIFO buffer for keydown events. It was made compatible with the existing API by pushing any extra key presses within this frame onto the next frames. Perhaps this functionality should be ported to other platforms too? |
@Berni8k On RPI keyboard events reading runs in a second thread, so, it makes sense the FIFO buffer but on other platforms, raylib is tied to GLFW functionality. Using this mechanism it's possible to store all pressed unicode characters on a certain frame into a
It would only happen if typing more than 60 characters per second or multiple characters are typed at once. Used current implementation with raygui
GLFW also provides |
This isn't really about more than 60 characters per second, but more about 2 characters in 16ms. People type in bursts not at a consistent speed. If you have some sort of speech-to-text system, then it can quite easily happen that you get 8 characters under 16ms, although in game context it probably is quite rare. |
If the framerate dips below 60fps this window becomes larger tho. Dips to 30fps are common and that widens the window to 33ms. Likely possible for a fast typist to hit two keys within 33ms if they are pressed by separate hands. Even more likely that this happens if the game is 2 player local multiplayer. In such a case players hitting keys at a average rate of 1 per second would on average get a missed key once per minute (If the game runs at 60fps) Its not a big deal if you ask me. But since it only takes 10 lines of C code to solve using a FIFO to move missed key presses onto the next frame, its probably worth adding it in. I can make a pull request with such a fix if wanted. |
@egonelbre @Berni8k I agree it's not a big deal to improve it so we can add that fix. Just one concern, would it be possible to implement it without any additional |
@Berni8k did you check this improvement? |
Copied information from gen2brain/raylib-go#36 (comment), because the original issue was closed and probably warrants a clear decision.
The example code in raylib uses IsKeyPressed https://github.com/raysan5/raylib/blob/master/examples/text/text_input_box.c#L55.
However looking at raylib API it looks like something that could be improved. The GetKeyPressed has a problem that it might lose characters with quick typing and you might end up with a buggy text-box.
For text input you want to separate "characters" and other keys, because you have case-sensitivity and utf-8... I suspect here the
GetKeyPressed
is meant asGetLastCharacterPressed
. You never want to mix these two, otherwise there will be pain.For raylib the right approach would be probably to have a buffer that gets cleared each frame so that you can consume all the keys in order that happened during the frame. e.g.
However this has the problem that it wouldn't be able to distinguish between characters and modifiers. There's a possibility to using something like:
Other APIs:
The text was updated successfully, but these errors were encountered: