floooh / sokol

minimal cross-platform standalone C headers
https://floooh.github.io/sokol-html5
zlib License
7.12k stars 501 forks source link

Difference between sokol_app events `KEY_DOWN` and `CHAR` #977

Closed elvodqa closed 10 months ago

elvodqa commented 10 months ago

Hi,

I'm pretty new to Sokol and I've been trying to learn it with the Odin bindings. My issue is with how input is checked. So there are events for it but as far as what I noticed, even in the https://floooh.github.io/sokol-html5/events-sapp.html example, there is no difference between KEY_DOWN and CHAR. They both behave like text inputs rather than an input similar to the ones in games. Is there another way to do it?

Thanks, Emir

floooh commented 10 months ago

The main difference is that CHAR gives you the UNICODE code point of what's printed on the keyboard key (assuming correct keyboard layout settings in the OS), while the KEY events are "virtual key codes" which are independent from any international keyboard layout.

E.g. for text input you would use CHAR events, and for game controls you would use KEY events.

What do you mean with "behaves like text inputs" for KEY events, the key repeat?

If you're just interested in whether a key is currently held down, you need to track that yourself. For example see here in pacman.c:

https://github.com/floooh/pacman.c/blob/090c18849c49fb162c27afb0b959bd9fda885f0c/pacman.c#L770-L800

...eventually there might be a separate header which provides such a polling API on top of input events, but that's quite low on the priority stack.

elvodqa commented 10 months ago

Thank you for the response. Following the pacman.c example, It worked smoothly. What I meant by behave like text inputs is a delay after the first key tick If you are holding down the key. Like: a__aaaaaaaaa.

Too bad I have to keep the state of every key I wanted but It is what it is.

Other than that, are there possible SDL/Sokol usage examples out there, If you are aware? I found a lot of examples for OpenGL but the main reason I'm using Sokol is DXD11 and Metal support.

floooh commented 10 months ago

is a delay after the first key tick If you are holding down the key. Like: a__aaaaaaaaa.

Ah right, yeah that might be a bit confusing for "raw" key input, but that's how the underlying operating system events behave. There's a bool key_repeat field in the event struct which you can use to detect and ignore such followup events if desired:

https://github.com/floooh/sokol/blob/17b399b8eeb6d1db4fe0e78fbea3b1a4184470be/sokol_app.h#L1413

Closing this ticket if that's ok :)

floooh commented 10 months ago

PS:

Other than that, are there possible SDL/Sokol usage examples out there,

Not that I know of unfortunately. The sokol-samples have examples for GLFW+GL (https://github.com/floooh/sokol-samples/tree/master/glfw) plus a very simple example for GLFW+Metal (https://github.com/floooh/sokol-samples/blob/master/glfw/metal-glfw.m).

I haven't attempted to use SDL with sokol_gfx.h so far though.