MudEngine Part 8: Inventory & Chat
Typing vs. moving: keys get swallowed
Try typing say hello into the command input. The h never appears — and your character walks west instead. The same happens for w, a, s, d, and the vi-style h/j/k/l. Why?
The movement handler listens on the whole .game-layout div for onkeydown. A key pressed inside the command input still bubbles up to that div, so the movement handler claims it: it calls prevent_default() (blocking the character from being inserted) and moves the player. You can type, but never the letters that move you.
The fix is to stop the keystroke at the input so it never reaches the movement handler. Dioxus events allow this with stop_propagation() — add it at the top of the input's own onkeydown.
input { class: "command-input", value: "{command_input}", oninput: move |e| command_input.set(e.value()), onkeydown: move |e| { // Don't let this keystroke bubble up to the movement handler: // w/a/s/d/h/j/k/l need to be *typed*, not move the player. e.stop_propagation(); if e.key() == Key::Enter { let cmd = command_input.read().trim().to_string(); if !cmd.is_empty() { if let Some((verb, rest)) = cmd.split_once(char::is_whitespace) .map(|(v, r)| (v.to_lowercase(), r.trim().to_string())) { match verb.as_str() { "say" | "'" => { send(ClientMessage::Say { message: rest }); } "take" | "get" => { send(ClientMessage::Take { item: rest }); } "drop" => { send(ClientMessage::Drop { item: rest }); } _ => {} } } command_input.set(String::new()); } } }, placeholder: "Type 'say hello', 'take sword', 'drop map'...", }
Dioxus events fire on the element where they happen, then bubble up through the parent tree. The keydown you type in the <input> climbs: input → .controls-area → .bottom-area → .game-layout, where the movement handler grabs it. stop_propagation() cuts that climb at the input, so the movement handler never sees typing keys. Arrow keys behave the same way: while the input is focused they move the text caret, and once you click the map they move the player again.
- Click the command input and type
say hello wasd— every character appears. - Press Enter — the message shows in chat and your player didn't move.
- Click the map, then press
w/s/a/d(or the arrow keys) — the player moves again. - Click back into the input and press
h,j,k,l— they type normally instead of walking.