Rework KeyModifiers system to properly allow keybinds to be triggered when multiple modifiers are pressed.

Fix setting keybinds whel using keyboard inputs to select the menu. Closes #9793
This commit is contained in:
LexManos committed 2023-11-28 19:10:07 -08:00
1 parent 4ac1a3dedf
commit c1557ec3c2
6 files changed
+68 -40

No files matched your search

@@ -90,10 +90,11 @@
}
public String saveString() {
@@ -184,4 +_,84 @@
@@ -183,5 +_,89 @@
public void setDown(boolean p_90846_) {
this.isDown = p_90846_;
}
+ }
+
+ /****************** Forge Start *****************************/
+ private net.minecraftforge.client.settings.KeyModifier keyModifierDefault = net.minecraftforge.client.settings.KeyModifier.NONE;
@@ -165,13 +166,17 @@
+ }
+
+ @Override
+ public void setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier keyModifier, InputConstants.Key keyCode) {
+ this.key = keyCode;
+ if (keyModifier.matches(keyCode))
+ keyModifier = net.minecraftforge.client.settings.KeyModifier.NONE;
+ public void setKeyModifierAndCode(@org.jetbrains.annotations.Nullable net.minecraftforge.client.settings.KeyModifier keyModifier, InputConstants.Key keyCode) {
+ MAP.remove(this);
+
+ if (keyModifier == null)
+ keyModifier = net.minecraftforge.client.settings.KeyModifier.getModifier(this.key);
+ if (keyModifier == null || keyCode == InputConstants.UNKNOWN || net.minecraftforge.client.settings.KeyModifier.isKeyCodeModifier(keyCode))
+ keyModifier = net.minecraftforge.client.settings.KeyModifier.NONE;
+
+ this.key = keyCode;
+ this.keyModifier = keyModifier;
+
+ MAP.put(keyCode, this);
+ }
+ /****************** Forge End *****************************/
}
}
@@ -1,25 +1,5 @@
--- a/net/minecraft/client/KeyboardHandler.java
+++ b/net/minecraft/client/KeyboardHandler.java
@@ -340,7 +_,9 @@
}
}
- if (p_90897_ == 1 && (!(this.minecraft.screen instanceof KeyBindsScreen) || ((KeyBindsScreen)screen).lastKeySelection <= Util.getMillis() - 20L)) {
+
+ if ((!(this.minecraft.screen instanceof KeyBindsScreen) || ((KeyBindsScreen)screen).lastKeySelection <= Util.getMillis() - 20L)) {
+ if (p_90897_ == 1) {
if (this.minecraft.options.keyFullscreen.matches(p_90895_, p_90896_)) {
this.minecraft.getWindow().toggleFullScreen();
this.minecraft.options.fullscreen().set(this.minecraft.getWindow().isFullscreen());
@@ -358,6 +_,8 @@
});
return;
}
+ } else if (p_90897_ == 0 /*GLFW_RELEASE*/ && this.minecraft.screen instanceof KeyBindsScreen)
+ ((KeyBindsScreen)this.minecraft.screen).selectedKey = null; //Forge: Unset pure modifiers.
}
if (this.minecraft.getNarrator().isActive() && this.minecraft.options.narratorHotkey().get()) {
@@ -380,11 +_,15 @@
Screen.wrapScreenError(() -> {
if (p_90897_ != 1 && p_90897_ != 2) {
@@ -9,9 +9,11 @@
}
public int getRowWidth() {
@@ -129,10 +_,11 @@
@@ -128,11 +_,13 @@
this.name = p_193917_;
this.changeButton = Button.builder(p_193917_, (p_269618_) -> {
KeyBindsList.this.keyBindsScreen.selectedKey = p_193916_;
+ p_193916_.setKeyModifierAndCode(null, com.mojang.blaze3d.platform.InputConstants.UNKNOWN);
KeyBindsList.this.resetMappingAndUpdateButtons();
- }).bounds(0, 0, 75, 20).createNarration((p_253311_) -> {
+ }).bounds(0, 0, 75 + 20 /* Forge: Add space */, 20).createNarration((p_253311_) -> {
@@ -9,18 +9,38 @@
}
this.keyBindsList.resetMappingAndUpdateButtons();
@@ -55,11 +_,14 @@
@@ -55,11 +_,13 @@
public boolean keyPressed(int p_193987_, int p_193988_, int p_193989_) {
if (this.selectedKey != null) {
if (p_193987_ == 256) {
+ this.selectedKey.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.getActiveModifier(), InputConstants.UNKNOWN);
+ this.selectedKey.setKeyModifierAndCode(null, InputConstants.UNKNOWN);
this.options.setKey(this.selectedKey, InputConstants.UNKNOWN);
} else {
+ this.selectedKey.setKeyModifierAndCode(net.minecraftforge.client.settings.KeyModifier.getActiveModifier(), InputConstants.getKey(p_193987_, p_193988_));
+ this.selectedKey.setKeyModifierAndCode(null, InputConstants.getKey(p_193987_, p_193988_));
this.options.setKey(this.selectedKey, InputConstants.getKey(p_193987_, p_193988_));
}
+ if(!net.minecraftforge.client.settings.KeyModifier.isKeyCodeModifier(this.selectedKey.getKey()))
-
+ if (p_193987_ == 256 || !net.minecraftforge.client.settings.KeyModifier.isKeyCodeModifier(this.selectedKey.getKey()))
this.selectedKey = null;
this.lastKeySelection = Util.getMillis();
this.keyBindsList.resetMappingAndUpdateButtons();
@@ -67,6 +_,19 @@
} else {
return super.keyPressed(p_193987_, p_193988_, p_193989_);
}
+ }
+
+ @Override
+ public boolean keyReleased(int keyCode, int scanCode, int modifiers) {
+ // Forge: We wait for a second key above if the first press is a modifier
+ // but if they release the modifier then set it explicitly.
+ var key = InputConstants.getKey(keyCode, scanCode);
+ if (this.selectedKey != null && this.selectedKey.getKey() == key) {
+ this.selectedKey = null;
+ this.lastKeySelection = Util.getMillis();
+ this.keyBindsList.resetMappingAndUpdateButtons();
+ }
+ return super.keyReleased(keyCode, scanCode, modifiers);
}
public void render(GuiGraphics p_282556_, int p_193992_, int p_193993_, float p_193994_) {
@@ -58,15 +58,17 @@ public class KeyMappingLookup {
*/
public List<KeyMapping> getAll(InputConstants.Key keyCode) {
var ret = new ArrayList<KeyMapping>();
var modifier = KeyModifier.getActiveModifier();
if (!modifier.matches(keyCode)) {
for (var modifier : KeyModifier.getValues(false)) {
if (!modifier.isActive(null) || modifier.matches(keyCode))
continue;
for (var binding : get(modifier, keyCode)) {
if (binding.isActiveAndMatches(keyCode))
ret.add(binding);
}
}
if (!ret.isEmpty() || modifier == KeyModifier.NONE)
if (!ret.isEmpty())
return ret;
for (var binding : get(KeyModifier.NONE, keyCode)) {
@@ -5,6 +5,7 @@
package net.minecraftforge.client.settings;
import java.util.List;
import java.util.function.Supplier;
import net.minecraft.client.Minecraft;
@@ -78,7 +79,7 @@ public enum KeyModifier {
@Override
public boolean isActive(@Nullable IKeyConflictContext conflictContext) {
if (conflictContext != null && !conflictContext.conflicts(KeyConflictContext.IN_GAME)) {
for (KeyModifier keyModifier : MODIFIER_VALUES) {
for (KeyModifier keyModifier : VALUES) {
if (keyModifier.isActive(conflictContext))
return false;
}
@@ -92,18 +93,36 @@ public enum KeyModifier {
}
};
@Deprecated(forRemoval = true, since = "1.20.2")
public static final KeyModifier[] MODIFIER_VALUES = {SHIFT, CONTROL, ALT};
@Deprecated(forRemoval = true, since = "1.20.2")
public static KeyModifier getActiveModifier() {
for (KeyModifier keyModifier : MODIFIER_VALUES) {
for (var keyModifier : VALUES) {
if (keyModifier.isActive(null))
return keyModifier;
}
return NONE;
}
private static final KeyModifier[] VALUES = {SHIFT, CONTROL, ALT};
private static final List<KeyModifier> VALUES_LIST = List.of(SHIFT, CONTROL, ALT);
private static final List<KeyModifier> ALL = List.of(SHIFT, CONTROL, ALT, NONE);
public static final List<KeyModifier> getValues(boolean includeNone) {
return includeNone ? ALL : VALUES_LIST;
}
@Nullable
public static KeyModifier getModifier(InputConstants.Key key) {
for (var modifier : VALUES) {
if (modifier.matches(key))
return modifier;
}
return null;
}
public static boolean isKeyCodeModifier(InputConstants.Key key) {
for (KeyModifier keyModifier : MODIFIER_VALUES) {
for (KeyModifier keyModifier : VALUES) {
if (keyModifier.matches(key))
return true;
}