KeyMapping.java

net.minecraft.client.KeyMapping

信息

  • 全限定名:net.minecraft.client.KeyMapping
  • 类型:public class
  • 包:net.minecraft.client
  • 源码路径:src/main/java/net/minecraft/client/KeyMapping.java
  • 起始行号:L22
  • 实现:Comparable
  • 职责:

    TODO

字段/常量

  • ALL

    • 类型: Map<String,KeyMapping>
    • 修饰符: private static final
    • 源码定位: L23
    • 说明:

      TODO

  • MAP

    • 类型: Map<InputConstants.Key,List<KeyMapping>>
    • 修饰符: private static final
    • 源码定位: L24
    • 说明:

      TODO

  • name

    • 类型: String
    • 修饰符: private final
    • 源码定位: L25
    • 说明:

      TODO

  • defaultKey

    • 类型: InputConstants.Key
    • 修饰符: private final
    • 源码定位: L26
    • 说明:

      TODO

  • category

    • 类型: KeyMapping.Category
    • 修饰符: private final
    • 源码定位: L27
    • 说明:

      TODO

  • key

    • 类型: InputConstants.Key
    • 修饰符: protected
    • 源码定位: L28
    • 说明:

      TODO

  • isDown

    • 类型: boolean
    • 修饰符: private
    • 源码定位: L29
    • 说明:

      TODO

  • clickCount

    • 类型: int
    • 修饰符: private
    • 源码定位: L30
    • 说明:

      TODO

  • order

    • 类型: int
    • 修饰符: private final
    • 源码定位: L31
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.KeyMapping.Category
    • 类型: record
    • 修饰符: public
    • 源码定位: L202
    • 说明:

      TODO

构造器

public KeyMapping(String name, int keysym, KeyMapping.Category category) @ L90

  • 构造器名:KeyMapping
  • 源码定位:L90
  • 修饰符:public

参数:

  • name: String
  • keysym: int
  • category: KeyMapping.Category

说明:

TODO

public KeyMapping(String name, InputConstants.Type type, int value, KeyMapping.Category category) @ L94

  • 构造器名:KeyMapping
  • 源码定位:L94
  • 修饰符:public

参数:

  • name: String
  • type: InputConstants.Type
  • value: int
  • category: KeyMapping.Category

说明:

TODO

public KeyMapping(String name, InputConstants.Type type, int value, KeyMapping.Category category, int order) @ L98

  • 构造器名:KeyMapping
  • 源码定位:L98
  • 修饰符:public

参数:

  • name: String
  • type: InputConstants.Type
  • value: int
  • category: KeyMapping.Category
  • order: int

说明:

TODO

方法

下面的方法块按源码顺序生成。

public static void click(InputConstants.Key key) @ L33

  • 方法名:click
  • 源码定位:L33
  • 返回类型:void
  • 修饰符:public static

参数:

  • key: InputConstants.Key

说明:

TODO

public static void set(InputConstants.Key key, boolean state) @ L37

  • 方法名:set
  • 源码定位:L37
  • 返回类型:void
  • 修饰符:public static

参数:

  • key: InputConstants.Key
  • state: boolean

说明:

TODO

private static void forAllKeyMappings(InputConstants.Key key, Consumer<KeyMapping> operation) @ L41

  • 方法名:forAllKeyMappings
  • 源码定位:L41
  • 返回类型:void
  • 修饰符:private static

参数:

  • key: InputConstants.Key
  • operation: Consumer

说明:

TODO

public static void setAll() @ L50

  • 方法名:setAll
  • 源码定位:L50
  • 返回类型:void
  • 修饰符:public static

参数:

说明:

TODO

public static void releaseAll() @ L60

  • 方法名:releaseAll
  • 源码定位:L60
  • 返回类型:void
  • 修饰符:public static

参数:

说明:

TODO

public static void restoreToggleStatesOnScreenClosed() @ L66

  • 方法名:restoreToggleStatesOnScreenClosed
  • 源码定位:L66
  • 返回类型:void
  • 修饰符:public static

参数:

说明:

TODO

public static void resetToggleKeys() @ L74

  • 方法名:resetToggleKeys
  • 源码定位:L74
  • 返回类型:void
  • 修饰符:public static

参数:

说明:

TODO

public static void resetMapping() @ L82

  • 方法名:resetMapping
  • 源码定位:L82
  • 返回类型:void
  • 修饰符:public static

参数:

说明:

TODO

public boolean isDown() @ L108

  • 方法名:isDown
  • 源码定位:L108
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public KeyMapping.Category getCategory() @ L112

  • 方法名:getCategory
  • 源码定位:L112
  • 返回类型:KeyMapping.Category
  • 修饰符:public

参数:

说明:

TODO

public boolean consumeClick() @ L116

  • 方法名:consumeClick
  • 源码定位:L116
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

protected void release() @ L125

  • 方法名:release
  • 源码定位:L125
  • 返回类型:void
  • 修饰符:protected

参数:

说明:

TODO

protected boolean shouldSetOnIngameFocus() @ L130

  • 方法名:shouldSetOnIngameFocus
  • 源码定位:L130
  • 返回类型:boolean
  • 修饰符:protected

参数:

说明:

TODO

public String getName() @ L134

  • 方法名:getName
  • 源码定位:L134
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public InputConstants.Key getDefaultKey() @ L138

  • 方法名:getDefaultKey
  • 源码定位:L138
  • 返回类型:InputConstants.Key
  • 修饰符:public

参数:

说明:

TODO

public void setKey(InputConstants.Key key) @ L142

  • 方法名:setKey
  • 源码定位:L142
  • 返回类型:void
  • 修饰符:public

参数:

  • key: InputConstants.Key

说明:

TODO

public int compareTo(KeyMapping o) @ L146

  • 方法名:compareTo
  • 源码定位:L146
  • 返回类型:int
  • 修饰符:public

参数:

  • o: KeyMapping

说明:

TODO

public static Supplier<Component> createNameSupplier(String key) @ L154

  • 方法名:createNameSupplier
  • 源码定位:L154
  • 返回类型:Supplier
  • 修饰符:public static

参数:

  • key: String

说明:

TODO

public boolean same(KeyMapping that) @ L159

  • 方法名:same
  • 源码定位:L159
  • 返回类型:boolean
  • 修饰符:public

参数:

  • that: KeyMapping

说明:

TODO

public boolean isUnbound() @ L163

  • 方法名:isUnbound
  • 源码定位:L163
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public boolean matches(KeyEvent event) @ L167

  • 方法名:matches
  • 源码定位:L167
  • 返回类型:boolean
  • 修饰符:public

参数:

  • event: KeyEvent

说明:

TODO

public boolean matchesMouse(MouseButtonEvent event) @ L173

  • 方法名:matchesMouse
  • 源码定位:L173
  • 返回类型:boolean
  • 修饰符:public

参数:

  • event: MouseButtonEvent

说明:

TODO

public Component getTranslatedKeyMessage() @ L177

  • 方法名:getTranslatedKeyMessage
  • 源码定位:L177
  • 返回类型:Component
  • 修饰符:public

参数:

说明:

TODO

public boolean isDefault() @ L181

  • 方法名:isDefault
  • 源码定位:L181
  • 返回类型:boolean
  • 修饰符:public

参数:

说明:

TODO

public String saveString() @ L185

  • 方法名:saveString
  • 源码定位:L185
  • 返回类型:String
  • 修饰符:public

参数:

说明:

TODO

public void setDown(boolean down) @ L189

  • 方法名:setDown
  • 源码定位:L189
  • 返回类型:void
  • 修饰符:public

参数:

  • down: boolean

说明:

TODO

private void registerMapping(InputConstants.Key key) @ L193

  • 方法名:registerMapping
  • 源码定位:L193
  • 返回类型:void
  • 修饰符:private

参数:

  • key: InputConstants.Key

说明:

TODO

public static KeyMapping get(String name) @ L197

  • 方法名:get
  • 源码定位:L197
  • 返回类型:KeyMapping
  • 修饰符:public static

参数:

  • name: String

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class KeyMapping implements Comparable<KeyMapping> {
    private static final Map<String, KeyMapping> ALL = Maps.newHashMap();
    private static final Map<InputConstants.Key, List<KeyMapping>> MAP = Maps.newHashMap();
    private final String name;
    private final InputConstants.Key defaultKey;
    private final KeyMapping.Category category;
    protected InputConstants.Key key;
    private boolean isDown;
    private int clickCount;
    private final int order;
 
    public static void click(InputConstants.Key key) {
        forAllKeyMappings(key, keyMapping -> keyMapping.clickCount++);
    }
 
    public static void set(InputConstants.Key key, boolean state) {
        forAllKeyMappings(key, keyMapping -> keyMapping.setDown(state));
    }
 
    private static void forAllKeyMappings(InputConstants.Key key, Consumer<KeyMapping> operation) {
        List<KeyMapping> keyMappings = MAP.get(key);
        if (keyMappings != null && !keyMappings.isEmpty()) {
            for (KeyMapping keyMapping : keyMappings) {
                operation.accept(keyMapping);
            }
        }
    }
 
    public static void setAll() {
        Window window = Minecraft.getInstance().getWindow();
 
        for (KeyMapping keyMapping : ALL.values()) {
            if (keyMapping.shouldSetOnIngameFocus()) {
                keyMapping.setDown(InputConstants.isKeyDown(window, keyMapping.key.getValue()));
            }
        }
    }
 
    public static void releaseAll() {
        for (KeyMapping keyMapping : ALL.values()) {
            keyMapping.release();
        }
    }
 
    public static void restoreToggleStatesOnScreenClosed() {
        for (KeyMapping keyMapping : ALL.values()) {
            if (keyMapping instanceof ToggleKeyMapping toggleKeyMapping && toggleKeyMapping.shouldRestoreStateOnScreenClosed()) {
                toggleKeyMapping.setDown(true);
            }
        }
    }
 
    public static void resetToggleKeys() {
        for (KeyMapping keyMapping : ALL.values()) {
            if (keyMapping instanceof ToggleKeyMapping toggleKeyMapping) {
                toggleKeyMapping.reset();
            }
        }
    }
 
    public static void resetMapping() {
        MAP.clear();
 
        for (KeyMapping keyMapping : ALL.values()) {
            keyMapping.registerMapping(keyMapping.key);
        }
    }
 
    public KeyMapping(String name, int keysym, KeyMapping.Category category) {
        this(name, InputConstants.Type.KEYSYM, keysym, category);
    }
 
    public KeyMapping(String name, InputConstants.Type type, int value, KeyMapping.Category category) {
        this(name, type, value, category, 0);
    }
 
    public KeyMapping(String name, InputConstants.Type type, int value, KeyMapping.Category category, int order) {
        this.name = name;
        this.key = type.getOrCreate(value);
        this.defaultKey = this.key;
        this.category = category;
        this.order = order;
        ALL.put(name, this);
        this.registerMapping(this.key);
    }
 
    public boolean isDown() {
        return this.isDown;
    }
 
    public KeyMapping.Category getCategory() {
        return this.category;
    }
 
    public boolean consumeClick() {
        if (this.clickCount == 0) {
            return false;
        } else {
            this.clickCount--;
            return true;
        }
    }
 
    protected void release() {
        this.clickCount = 0;
        this.setDown(false);
    }
 
    protected boolean shouldSetOnIngameFocus() {
        return this.key.getType() == InputConstants.Type.KEYSYM && this.key.getValue() != InputConstants.UNKNOWN.getValue();
    }
 
    public String getName() {
        return this.name;
    }
 
    public InputConstants.Key getDefaultKey() {
        return this.defaultKey;
    }
 
    public void setKey(InputConstants.Key key) {
        this.key = key;
    }
 
    public int compareTo(KeyMapping o) {
        if (this.category == o.category) {
            return this.order == o.order ? I18n.get(this.name).compareTo(I18n.get(o.name)) : Integer.compare(this.order, o.order);
        } else {
            return Integer.compare(KeyMapping.Category.SORT_ORDER.indexOf(this.category), KeyMapping.Category.SORT_ORDER.indexOf(o.category));
        }
    }
 
    public static Supplier<Component> createNameSupplier(String key) {
        KeyMapping map = ALL.get(key);
        return map == null ? () -> Component.translatable(key) : map::getTranslatedKeyMessage;
    }
 
    public boolean same(KeyMapping that) {
        return this.key.equals(that.key);
    }
 
    public boolean isUnbound() {
        return this.key.equals(InputConstants.UNKNOWN);
    }
 
    public boolean matches(KeyEvent event) {
        return event.key() == InputConstants.UNKNOWN.getValue()
            ? this.key.getType() == InputConstants.Type.SCANCODE && this.key.getValue() == event.scancode()
            : this.key.getType() == InputConstants.Type.KEYSYM && this.key.getValue() == event.key();
    }
 
    public boolean matchesMouse(MouseButtonEvent event) {
        return this.key.getType() == InputConstants.Type.MOUSE && this.key.getValue() == event.button();
    }
 
    public Component getTranslatedKeyMessage() {
        return this.key.getDisplayName();
    }
 
    public boolean isDefault() {
        return this.key.equals(this.defaultKey);
    }
 
    public String saveString() {
        return this.key.getName();
    }
 
    public void setDown(boolean down) {
        this.isDown = down;
    }
 
    private void registerMapping(InputConstants.Key key) {
        MAP.computeIfAbsent(key, k -> new ArrayList<>()).add(this);
    }
 
    public static @Nullable KeyMapping get(String name) {
        return ALL.get(name);
    }
 
    @OnlyIn(Dist.CLIENT)
    public record Category(Identifier id) {
        private static final List<KeyMapping.Category> SORT_ORDER = new ArrayList<>();
        public static final KeyMapping.Category MOVEMENT = register("movement");
        public static final KeyMapping.Category MISC = register("misc");
        public static final KeyMapping.Category MULTIPLAYER = register("multiplayer");
        public static final KeyMapping.Category GAMEPLAY = register("gameplay");
        public static final KeyMapping.Category INVENTORY = register("inventory");
        public static final KeyMapping.Category CREATIVE = register("creative");
        public static final KeyMapping.Category SPECTATOR = register("spectator");
        public static final KeyMapping.Category DEBUG = register("debug");
 
        private static KeyMapping.Category register(String name) {
            return register(Identifier.withDefaultNamespace(name));
        }
 
        public static KeyMapping.Category register(Identifier id) {
            KeyMapping.Category category = new KeyMapping.Category(id);
            if (SORT_ORDER.contains(category)) {
                throw new IllegalArgumentException(String.format(Locale.ROOT, "Category '%s' is already registered.", id));
            } else {
                SORT_ORDER.add(category);
                return category;
            }
        }
 
        public Component label() {
            return Component.translatable(this.id.toLanguageKey("key.category"));
        }
    }
}

引用的其他类

  • InputConstants

    • 引用位置: 参数/字段/方法调用/返回值
    • 关联成员: InputConstants.isKeyDown()
  • Minecraft

    • 引用位置: 方法调用
    • 关联成员: Minecraft.getInstance()
  • KeyEvent

    • 引用位置: 参数
  • MouseButtonEvent

    • 引用位置: 参数
  • I18n

    • 引用位置: 方法调用
    • 关联成员: I18n.get()
  • Component

    • 引用位置: 方法调用/返回值
    • 关联成员: Component.translatable()
  • Identifier

    • 引用位置: 方法调用
    • 关联成员: Identifier.withDefaultNamespace()