Skip to content

Commit 85a3734

Browse files
TmpodxCollateral
authored andcommitted
Fix Ctrl+key combos inserting characters in text fields on Wayland
On native Wayland, GLFW fires a separate char event after the "main" key event, leading to quirky behaviour (eg. with `Ctrl+a`). This new mixin fixes it by cancelling the secondary key event if the Ctrl modifier is set.
1 parent b51ca0c commit 85a3734

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.vulkanmod.mixin.wayland;
2+
3+
import net.minecraft.client.KeyboardHandler;
4+
import net.minecraft.client.input.CharacterEvent;
5+
import net.vulkanmod.config.Platform;
6+
import org.lwjgl.glfw.GLFW;
7+
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Inject;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
11+
12+
@Mixin(KeyboardHandler.class)
13+
public class KeyboardHandlerM {
14+
15+
/**
16+
* Ensures Ctrl-based shortcuts work correctly.
17+
*
18+
* On native Wayland (i.e. not XWayland), GLFW fires both a key event and a char callback event
19+
* when a Ctrl+key combination is pressed, because the compositor delivers the unmodified
20+
* character as a separate char event regardless of held modifiers. This causes both the
21+
* shortcut action and a character insertion to happen simultaneously (e.g. Ctrl+v pastes and appends 'v').
22+
*
23+
* This mixin cancels that char event if the Ctrl modifier is set, thus fixing the issue.
24+
*/
25+
@Inject(method = "charTyped", at = @At("HEAD"), cancellable = true)
26+
private void cancelCtrlCharTyped(long window, CharacterEvent event, CallbackInfo ci) {
27+
if (Platform.isWayLand() && (event.modifiers() & GLFW.GLFW_MOD_CONTROL) != 0) {
28+
ci.cancel();
29+
}
30+
}
31+
}

src/main/resources/vulkanmod.mixins.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"vertex.VertexMultiConsumersM$SheetDecalM",
9999

100100
"wayland.InputConstantsM",
101+
"wayland.KeyboardHandlerM",
101102
"wayland.MinecraftMixin",
102103

103104
"texture.mip.MipmapGeneratorM",

0 commit comments

Comments
 (0)