Merge pull request #2176 from kacpherek/fix/2161-shared-key-bindings

🤖 Agent: Gate check by Ludovic's AI agent (Claude). Checked against 26.3: `KeyMapping.click` and `set` act on every mapping bound to the key, as here, and your second commit stops a key from replaying a press when two mappings share it by default. #2207 fixes the same issue; I kept this one because it came first and changes less. Gate: this wave's 55 PRs merged on main in one tree: clippy `-D warnings` clean, 1186 horizon-client and 163 horizon-world tests pass, and of the 101 harnesses `parity.py --changed-files` picked, 100 pass (`block_removal` failed on one spawner case from #2199's orb merging, fixed there). `lan_e2e.py` passes 24 checks with the host on its server thread and in-frame (chest, xp, pvp, doors, villagers, beds, frames, projectiles, arrows, fishing, damage, mobs, blast, drops, elytra, breath, predict, cracks, barter, kills, hurt, contacts, underwater, plates), plus forced-levels on the server thread. Thanks @kacpherek!
This commit is contained in:
Ludovic Marie
2026-10-26 18:46:14 +01:00
committed by GitHub
2 changed files with 90 additions and 38 deletions
+26 -19
View File
@@ -11626,16 +11626,17 @@ impl ApplicationHandler for Game<'_> {
}
self.extras_mouse(button, pressed);
self.mod_mouse_key(button, pressed);
match self.settings.keys.translate_mouse(button) {
Some(Err(b)) => self.play_mouse(b, pressed)?,
Some(Ok(k)) if pressed => {
self.keys.insert(k);
self.key_pressed(k)?;
for mapped in self.settings.keys.translate_mouse_all(button) {
match mapped {
Err(b) => self.play_mouse(b, pressed)?,
Ok(k) if pressed => {
self.keys.insert(k);
self.key_pressed(k)?;
}
Ok(k) => {
self.keys.remove(&k);
}
}
Some(Ok(k)) => {
self.keys.remove(&k);
}
None => {}
}
}
Screen::Inventory | Screen::Creative
@@ -11883,18 +11884,24 @@ impl ApplicationHandler for Game<'_> {
{
self.smooth_camera = !self.smooth_camera;
}
// Key binds: act as the default key of the bound mapping.
let key = match self.settings.keys.translate(key) {
Some(Ok(k)) => k,
Some(Err(button)) => {
if self.screen == Screen::Playing && !event.repeat {
self.play_mouse(button, event.state == ElementState::Pressed)?;
// Key binds: act as the default key of every mapping bound
// to it (KeyMapping.set drives them all).
for mapped in self.settings.keys.translate_all(key) {
match mapped {
Ok(k) => {
self.mapped_key(
k,
event.state == ElementState::Pressed,
event.repeat,
)?;
}
Err(button) => {
if self.screen == Screen::Playing && !event.repeat {
self.play_mouse(button, event.state == ElementState::Pressed)?;
}
}
return Ok(());
}
None => return Ok(()),
};
self.mapped_key(key, event.state == ElementState::Pressed, event.repeat)?;
}
}
WindowEvent::RedrawRequested => {
self.observe_fullscreen();
+64 -19
View File
@@ -275,6 +275,20 @@ impl Default for KeyBinds {
}
}
}
/// Each default key once, in order: two mappings that share a default key
/// (Advancements and the debug profiler both on L) are one press of it, not
/// two (the second would close the screen the first opened). Vanilla clicks
/// both mappings, but the profiler one only acts with F3 held.
fn distinct<T: PartialEq>(keys: Vec<T>) -> Vec<T> {
let mut out = Vec::with_capacity(keys.len());
for k in keys {
if !out.contains(&k) {
out.push(k);
}
}
out
}
impl KeyBinds {
pub fn get(&self, name: &str) -> &str {
self.keys
@@ -320,49 +334,80 @@ impl KeyBinds {
}
/// The mapping a physical input triggers (first in list order).
pub fn mapping_for(&self, key: &str) -> Option<&'static str> {
MAPPINGS.iter().map(|m| m.0).find(|n| self.get(n) == key)
self.mappings_for(key).next()
}
/// Every mapping a physical input triggers, in list order: like
/// `KeyMapping.set`/`click`, one key drives all the mappings bound to it.
pub fn mappings_for<'a>(&'a self, key: &'a str) -> impl Iterator<Item = &'static str> + 'a {
MAPPINGS
.iter()
.map(|m| m.0)
.filter(move |n| self.get(n) == key)
}
/// Translate a physical key into the default key of the mapping it is
/// bound to, so game code written against vanilla defaults follows the
/// player's bindings. `None` means the key now does nothing; mouse
/// mappings bound to keys come back as `Err(button)`.
/// mappings bound to keys come back as `Err(button)`. The first of
/// [`KeyBinds::translate_all`].
pub fn translate(&self, key: KeyCode) -> Option<Result<KeyCode, MouseButton>> {
self.translate_all(key).into_iter().next()
}
/// The default keys of every mapping `key` is bound to (empty when it
/// now does nothing), so a key bound to two mappings (forward and
/// sprint) drives both, as `KeyMapping.set` does.
pub fn translate_all(&self, key: KeyCode) -> Vec<Result<KeyCode, MouseButton>> {
let Some(name) = key_name(key) else {
return Some(Ok(key));
return vec![Ok(key)];
};
if let Some(mapping) = self.mapping_for(name) {
let default = Self::default_of(mapping);
if let Some(code) = key_code(default) {
return Some(Ok(code));
}
return mouse_button(default).map(Err);
let bound: Vec<_> = self
.mappings_for(name)
.filter_map(|mapping| {
let default = Self::default_of(mapping);
match key_code(default) {
Some(code) => Some(Ok(code)),
None => mouse_button(default).map(Err),
}
})
.collect();
if !bound.is_empty() {
return distinct(bound);
}
// An unbound key whose default meaning was moved elsewhere is inert.
if MAPPINGS
.iter()
.any(|(n, d, _)| *d == name && !self.is_default(n))
{
return None;
return Vec::new();
}
Some(Ok(key))
vec![Ok(key)]
}
/// Translate a mouse button the same way (keys bound to mouse buttons).
pub fn translate_mouse(&self, button: MouseButton) -> Option<Result<KeyCode, MouseButton>> {
self.translate_mouse_all(button).into_iter().next()
}
/// [`KeyBinds::translate_all`] for a mouse button.
pub fn translate_mouse_all(&self, button: MouseButton) -> Vec<Result<KeyCode, MouseButton>> {
let name = mouse_name(button);
if let Some(mapping) = self.mapping_for(&name) {
let default = Self::default_of(mapping);
if let Some(b) = mouse_button(default) {
return Some(Err(b));
}
return key_code(default).map(Ok);
let bound: Vec<_> = self
.mappings_for(&name)
.filter_map(|mapping| {
let default = Self::default_of(mapping);
match mouse_button(default) {
Some(b) => Some(Err(b)),
None => key_code(default).map(Ok),
}
})
.collect();
if !bound.is_empty() {
return distinct(bound);
}
if MAPPINGS
.iter()
.any(|(n, d, _)| *d == name && !self.is_default(n))
{
return None;
return Vec::new();
}
Some(Err(button))
vec![Err(button)]
}
/// `AbstractContainerScreen.checkHotbarMouseClicked`: off hand, then hotbar.
pub fn container_mouse_key(&self, button: MouseButton, creative: bool) -> Option<KeyCode> {