bevyengine / bevyengine/bevy

Expose winit's DeviceEvents (distinguish different keyboards/mice).

Open
#2,752 4 comments 0 reactions 0 assignees View on GitHub
A-Input C-Feature
Dominant language
Rust
Stars
48.2k
Forks
4.8k
Avg merge
3d 22h
Merged PRs (30d)
161

Description

It would be interesting to expose the `DeviceEvent`s from `winit` to the application somehow.

The specific motivation for this is that this exposes not only device events themselves, but also allows you to distinguish the device that produces them (for example: key presses are annotated with the keyboard that produces them).

## What problem does this solve or what need does it fill?

This can be useful for local multiplayer; each player can be allocated a separate keyboard (and mouse?).

(N.B. it seems like `GamepadEvent` already identifies the specific gamepad in use, so this is not required for multi-gamepad games.)

## What solution would you like?

Unsure what's best, but the relevant event is currently ignored by the event loop in `bevy_winit`, so we could expose it to the application as events (in a similar way to `KeyboardInput` and friends).

I had a play around to expose these and it's not difficult (at least on Linux/X), though I notice that `DeviceEvent`s ignore the focus of the window, so key events are captured even if another application is in use — likely need to work around this by ignoring the event if the window isn't focussed.

(As an opaque identifier, I hashed the device ID and fed the hash (as a `u32`) to a new custom event type. This is probably not the best, but means that `bevy_input` doesn't need to know about the specific type of `device_id` (which is platform-dependent))

## What alternative(s) have you considered?

It might be possible to write a custom crate that expands on `bevy_winit` but uses a custom event loop. I may well have a go at that, but thought I'd leave this ticket here.

Contributor guide

Open the contributing guide

Research direction

Start in the bevy_winit event loop where winit DeviceEvents are currently ignored, then compare how KeyboardInput and GamepadEvent are exposed through bevy_input. The issue's proposed behavior is complete when applications can receive device events with an opaque device identifier, with the window-focus behavior accounted for.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
game-dev
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.