Keyboard, mouse and gamepad¶
Level: 201 · working knowledge
One line: Input in Bevy is state you read each frame, not a callback you register — ButtonInput<KeyCode> is a resource that knows which keys are held, which went down this frame and which came up — and a gamepad is an entity with a Gamepad component rather than a device you open (as of Bevy 0.19).
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The bullets below are the questions the finished page has to answer.
What it has to cover¶
Res<ButtonInput<KeyCode>>and its three questions, per theButtonInputdocs ↗ as of Bevy 0.19:pressedis true between a press and a release;just_pressedandjust_releasedare true for one frame after the event.- The same docs' warning: when several systems check
just_pressedbut only one should react, clear the state withclear_just_pressed, orclearorresetafter acting. What goes wrong if you do not? KeyCodeagainstKey(as of Bevy 0.19):KeyCodeis the location of a physical key, right for WASD movement on any layout;Keyis the logical value the key produces, right for typed letters and for+and-, which sit in different places on different layouts.- The mouse:
ButtonInput<MouseButton>for buttons, andWindow::cursor_position(), which returnsOption<Vec2>in logical pixels andNoneoutside the window (as of Bevy 0.19). Where is its origin, and how doesCamera::viewport_to_world_2dturn it into a point in the game world? - Mouse motion rather than position —
AccumulatedMouseMotion(as of Bevy 0.19) — for a camera that turns with the mouse. Why is the cursor position the wrong input for that? - Gamepads as entities: a
Query<&Gamepad>, withpressed(GamepadButton::South),just_pressed, andleft_stick()returning aVec2(as of Bevy 0.19). Why is a resting stick not exactly zero, and where does a dead zone belong? - Input in
FixedUpdate: the fixed schedule can run zero or two times in a frame. Can ajust_pressedread there be missed, or seen twice? - Actions against keys: does Bevy 0.19 ship a layer that maps "jump" to Space, a gamepad button and a touch, or is that a crate — and what does a game lose by reading
KeyCode::Spacein five systems?
The trap it exists for¶
Jumping on pressed. The key is held for several frames, so the player jumps on every one of them — or, in the other direction, a menu that opens on just_pressed in one system and closes on just_pressed in another opens and closes in the same frame.
Where this sits¶
Resources and plugins covers what a resource is; What a game engine is covers frames and the fixed timestep. This page covers only reading the devices.
See also¶
- What a game engine is — the frame that "just" is measured in
- Resources and plugins —
ButtonInput<KeyCode>is a resource like any other - Sprites and meshes — the camera that turns a cursor position into a world position
OptionandResult—cursor_position()returnsNonewhen the cursor is outside- An enum instead of a bool —
pressed,just_pressedandjust_releasedas three states rather than one flag - Raw mode and passwords — reading keys one at a time in a terminal, the non-game version
- Bevy input examples, 0.19.1 ↗
If you are coming from another language¶
- Python. pygame offers both styles: an event queue you drain each frame, and
pygame.key.get_pressed()for the held state. Bevy'sButtonInputis the second style with the edge detection —just_pressed— already done for you. - JavaScript. The DOM delivers
keydownevents to callbacks, andKeyboardEvent.codeagainstKeyboardEvent.keyis exactlyKeyCodeagainstKey; the Bevy docs sayKeyCodemostly conforms to the UI Events specification'scode. What changes is the shape: no callback, one resource read in a system each frame. - C#. Unity's legacy Input Manager has the same held-and-edge pair:
Input.GetKey, andInput.GetKeyDown, which returns true during the frame ↗ the key goes down — polled inUpdatejust aspressedandjust_pressedare read in a system. Unity now recommends its separate Input System package for new projects, an action layer the last bullet above asks about for Bevy.
Po polsku¶
Wejście w Bevy to stan odczytywany w każdej klatce, a nie funkcja zwrotna (callback): zasób ButtonInput<KeyCode> odpowiada na trzy pytania — pressed (klawisz trzymany), just_pressed i just_released (prawdziwe przez jedną klatkę). KeyCode to fizyczne położenie klawisza (dobre do WASD na każdym układzie klawiatury), Key to znak, który klawisz wytwarza. Pad jest encją z komponentem Gamepad, a pozycję kursora daje Window::cursor_position() jako Option<Vec2>.
Szukaj po polsku: obsługa klawiatury w Bevy · pad do gier Rust · bevy buttoninput just_pressed · bevy keycode vs key · bevy gamepad left_stick · bevy cursor_position viewport_to_world_2d