Skip to content

Keyboard input support #144

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

Closed
Devon7925 opened this issue May 11, 2025 · 1 comment · Fixed by #145
Closed

Keyboard input support #144

Devon7925 opened this issue May 11, 2025 · 1 comment · Fixed by #145
Assignees

Comments

@Devon7925
Copy link
Contributor

Slang playground should support keyboard input in addition to its mouse input.

Prior Work

Shadertoy

Allows keyboard input in a 256x3 uint8 input sampler. Each x coordinate represents the state of a different key. Y=0 represents the state of the key, Y=1 represents whether the key was pressed this frame, and Y=2 represents a toggle for that button.

Compute.toys

Allows keyboard input via a function keyDown(keycode: u32). Internally this accesses a bit in a vec4<u32>[2] which compactly holds the state of the keys.

Slang Native Playground

Allows keyboard input through a playground::KEY_INPUT user attribute on a float uniform. This takes the name of a key(ex. "space") and sets the uniform to 1.0 if the key is down and 0.0 otherwise. This is the least memory efficient if you are accessing many keys, but the most readable as it doesn't require memorizing key codes. It is also more memory efficient if the user uses less than 8 keys, which is common. However, it makes accessing keys dynamically much harder.

Proposal

I suggest implementing the playground::KEY_INPUT solution for now, with the possibility of a bitfield in the future.

@r0levrai
Copy link

r0levrai commented May 11, 2025

If your backend exposes it, allowing a key to be retrieved by either name ("a" maps to "a" on azerty, usecase: press "n" to toggle normals) or position ("a" maps to "q" on azerty, usecase: wasd movements) would be a huge upgrade for non-qwerty users :)

In js I think it's done by comparing to either event.key or event.code. Although event.code names are a little strange ("KeyA" for "a") – and it could be possible to map them to something more analogous to event.key names ("a") since they are less than 255 of them – exposing them as is is still a good (and probably more sane) start.

Name survey:

  • js: event.key vs event.code
  • sdl2: event.key.keysym.sym vs event.key.keysym.scancode (of type SDL_Keycode vs SDL_Scancode)
  • sdl3: event.key.key vs event.key.scancode (of type SDL_Keycode vs SDL_Scancode)
  • glfw: void key_callback(GLFWwindow* window, int key, int scancode, ...)
  • godot4: InputEventKey.keycode vs InputEventKey.physical_keycode
  • unity: old: GetKey(string name) vs GetKey(KeyCode key), new: ? vs Keyboard.current.<key>
  • etc.

-> we could call that playground::KEY_NAME and playground::KEY_CODE (removed _INPUT since it doesn't appears in mouse either).

Devon7925 added a commit to Devon7925/slang-playground that referenced this issue May 13, 2025
mkeshavaNV pushed a commit that referenced this issue May 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants