KeyMapping .java

net.minecraft.client.KeyMapping (class): members: restoreToggleStatesOnScreenClosed, getTranslatedKeyMessage, createNameSupplier, resetToggleKeys, setKey.

net.minecraft.clientclass234 linesclient7.0 KB

Implements

API Surface (29)

void click(final InputConstants.Key key)
void set(final InputConstants.Key key, final boolean state)
void setAll()
void releaseAll()
void restoreToggleStatesOnScreenClosed()
void resetToggleKeys()
void resetMapping()
boolean isDown()
KeyMapping.Category getCategory()
boolean consumeClick()
void release()
boolean shouldSetOnIngameFocus()
String getName()
InputConstants.Key getDefaultKey()
void setKey(final InputConstants.Key key)
int compareTo(final KeyMapping o)
Supplier<Component> createNameSupplier(final String key)
boolean same(final KeyMapping that)
boolean isUnbound()
boolean matches(final KeyEvent event)
boolean matchesMouse(final MouseButtonEvent event)
boolean matches(final InputConstants.Key key)
Component getTranslatedKeyMessage()
boolean isDefault()
String saveString()
void setDown(final boolean down)
record Category(Identifier id)
KeyMapping.Category register(final Identifier id)
Component label()

Fields & Constants (9)

ALL
MAP
name
defaultKey
category
key
isDown
clickCount
order

String literals (8)

"movement"
"misc"
"multiplayer"
"gameplay"
"inventory"
"creative"
"spectator"
"key.category"

Full Source

KeyMapping.java234 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client;
2
3
import com.google.common.collect.Maps;
4
import com.mojang.blaze3d.platform.InputConstants;
5
import com.mojang.blaze3d.platform.Window;
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.Locale;
9
import java.util.Map;
10
import java.util.function.Consumer;
11
import java.util.function.Supplier;
12
import net.fabricmc.api.EnvType;
13
import net.fabricmc.api.Environment;
14
import net.minecraft.client.input.KeyEvent;
15
import net.minecraft.client.input.MouseButtonEvent;
16
import net.minecraft.client.resources.language.I18n;
17
import net.minecraft.network.chat.Component;
18
import net.minecraft.resources.Identifier;
19
import org.jspecify.annotations.Nullable;
20
21
@Environment(EnvType.CLIENT)
22
public class KeyMapping implements Comparable<KeyMapping> {
23
	private static final Map<String, KeyMapping> ALL = Maps.newHashMap();
24
	private static final Map<InputConstants.Key, List<KeyMapping>> MAP = Maps.newHashMap();
25
	private final String name;
26
	private final InputConstants.Key defaultKey;
27
	private final KeyMapping.Category category;
28
	protected InputConstants.Key key;
29
	private boolean isDown;
30
	private int clickCount;
31
	private final int order;
32
33
	public static void click(final InputConstants.Key key) {
34
		forAllKeyMappings(key, keyMapping -> keyMapping.clickCount++);
35
	}
36
37
	public static void set(final InputConstants.Key key, final boolean state) {
38
		forAllKeyMappings(key, keyMapping -> keyMapping.setDown(state));
39
	}
40
41
	private static void forAllKeyMappings(final InputConstants.Key key, final Consumer<KeyMapping> operation) {
42
		List<KeyMapping> keyMappings = MAP.get(key);
43
		if (keyMappings != null && !keyMappings.isEmpty()) {
44
			for (KeyMapping keyMapping : keyMappings) {
45
				operation.accept(keyMapping);
46
			}
47
		}
48
	}
49
50
	public static void setAll() {
51
		Window window = Minecraft.getInstance().getWindow();
52
53
		for (KeyMapping keyMapping : ALL.values()) {
54
			if (keyMapping.shouldSetOnIngameFocus()) {
55
				keyMapping.setDown(InputConstants.isKeyDown(keyMapping.key.getValue()));
56
			}
57
		}
58
	}
59
60
	public static void releaseAll() {
61
		for (KeyMapping keyMapping : ALL.values()) {
62
			keyMapping.release();
63
		}
64
	}
65
66
	public static void restoreToggleStatesOnScreenClosed() {
67
		for (KeyMapping keyMapping : ALL.values()) {
68
			if (keyMapping instanceof ToggleKeyMapping toggleKeyMapping && toggleKeyMapping.shouldRestoreStateOnScreenClosed()) {
69
				toggleKeyMapping.setDown(true);
70
			}
71
		}
72
	}
73
74
	public static void resetToggleKeys() {
75
		for (KeyMapping keyMapping : ALL.values()) {
76
			if (keyMapping instanceof ToggleKeyMapping toggleKeyMapping) {
77
				toggleKeyMapping.reset();
78
			}
79
		}
80
	}
81
82
	public static void resetMapping() {
83
		MAP.clear();
84
85
		for (KeyMapping keyMapping : ALL.values()) {
86
			keyMapping.registerMapping(keyMapping.key);
87
		}
88
	}
89
90
	public KeyMapping(final String name, final int keysym, final KeyMapping.Category category) {
91
		this(name, InputConstants.Type.KEYBOARD, keysym, category);
92
	}
93
94
	public KeyMapping(final String name, final InputConstants.Type type, final int value, final KeyMapping.Category category) {
95
		this(name, type, value, category, 0);
96
	}
97
98
	public KeyMapping(final String name, final InputConstants.Type type, final int value, final KeyMapping.Category category, final int order) {
99
		this.name = name;
100
		this.key = type.getOrCreate(value);
101
		this.defaultKey = this.key;
102
		this.category = category;
103
		this.order = order;
104
		ALL.put(name, this);
105
		this.registerMapping(this.key);
106
	}
107
108
	public boolean isDown() {
109
		return this.isDown;
110
	}
111
112
	public KeyMapping.Category getCategory() {
113
		return this.category;
114
	}
115
116
	public boolean consumeClick() {
117
		if (this.clickCount == 0) {
118
			return false;
119
		}
120
121
		this.clickCount--;
122
		return true;
123
	}
124
125
	protected void release() {
126
		this.clickCount = 0;
127
		this.setDown(false);
128
	}
129
130
	protected boolean shouldSetOnIngameFocus() {
131
		return this.key.getType() == InputConstants.Type.KEYBOARD && this.key.getValue() != InputConstants.UNKNOWN.getValue();
132
	}
133
134
	public String getName() {
135
		return this.name;
136
	}
137
138
	public InputConstants.Key getDefaultKey() {
139
		return this.defaultKey;
140
	}
141
142
	public void setKey(final InputConstants.Key key) {
143
		this.key = key;
144
	}
145
146
	public int compareTo(final KeyMapping o) {
147
		if (this.category == o.category) {
148
			return this.order == o.order ? I18n.get(this.name).compareTo(I18n.get(o.name)) : Integer.compare(this.order, o.order);
149
		} else {
150
			return Integer.compare(KeyMapping.Category.SORT_ORDER.indexOf(this.category), KeyMapping.Category.SORT_ORDER.indexOf(o.category));
151
		}
152
	}
153
154
	public static Supplier<Component> createNameSupplier(final String key) {
155
		KeyMapping map = ALL.get(key);
156
		return map == null ? () -> Component.translatable(key) : map::getTranslatedKeyMessage;
157
	}
158
159
	public boolean same(final KeyMapping that) {
160
		return this.key.equals(that.key);
161
	}
162
163
	public boolean isUnbound() {
164
		return this.key.equals(InputConstants.UNKNOWN);
165
	}
166
167
	public boolean matches(final KeyEvent event) {
168
		return this.key.getType() == InputConstants.Type.KEYBOARD && this.key.getValue() == event.key() && !this.isUnbound();
169
	}
170
171
	public boolean matchesMouse(final MouseButtonEvent event) {
172
		return this.key.getType() == InputConstants.Type.MOUSE && this.key.getValue() == event.button() && !this.isUnbound();
173
	}
174
175
	public boolean matches(final InputConstants.Key key) {
176
		return this.key.equals(key) && !this.isUnbound();
177
	}
178
179
	public Component getTranslatedKeyMessage() {
180
		return this.key.getDisplayName();
181
	}
182
183
	public boolean isDefault() {
184
		return this.key.equals(this.defaultKey);
185
	}
186
187
	public String saveString() {
188
		return this.key.getName();
189
	}
190
191
	public void setDown(final boolean down) {
192
		this.isDown = down;
193
	}
194
195
	private void registerMapping(final InputConstants.Key key) {
196
		MAP.computeIfAbsent(key, k -> new ArrayList<>()).add(this);
197
	}
198
199
	public static @Nullable KeyMapping get(final String name) {
200
		return ALL.get(name);
201
	}
202
203
	@Environment(EnvType.CLIENT)
204
	public record Category(Identifier id) {
205
		private static final List<KeyMapping.Category> SORT_ORDER = new ArrayList<>();
206
		public static final KeyMapping.Category MOVEMENT = register("movement");
207
		public static final KeyMapping.Category MISC = register("misc");
208
		public static final KeyMapping.Category MULTIPLAYER = register("multiplayer");
209
		public static final KeyMapping.Category GAMEPLAY = register("gameplay");
210
		public static final KeyMapping.Category INVENTORY = register("inventory");
211
		public static final KeyMapping.Category CREATIVE = register("creative");
212
		public static final KeyMapping.Category SPECTATOR = register("spectator");
213
		public static final KeyMapping.Category DEBUG = register("debug");
214
215
		private static KeyMapping.Category register(final String name) {
216
			return register(Identifier.withDefaultNamespace(name));
217
		}
218
219
		public static KeyMapping.Category register(final Identifier id) {
220
			KeyMapping.Category category = new KeyMapping.Category(id);
221
			if (SORT_ORDER.contains(category)) {
222
				throw new IllegalArgumentException(String.format(Locale.ROOT, "Category '%s' is already registered.", id));
223
			}
224
225
			SORT_ORDER.add(category);
226
			return category;
227
		}
228
229
		public Component label() {
230
			return Component.translatable(this.id.toLanguageKey("key.category"));
231
		}
232
	}
233
}
234

Decompiled Minecraft 26.3 source of KeyMapping (net/minecraft/client/KeyMapping.java) with Mojang official mappings, stored verbatim on the site.