Class Key

java.lang.Object
dev.xichil.mvui.Key

public final class Key extends Object
A keyboard key or mouse button that a Keybind can be set to, the same in every Minecraft version. Immutable; compare with equals(Object).
  • A keyboard key is identified by its GLFW key code (what 1.13+ uses), e.g. 82 for R, 340 for Left Shift. Create one with keyboard(int).
  • A mouse button is identified by its number: 0 left, 1 right, 2 middle, 3 and up the side buttons. Create one with mouse(int).
  • UNBOUND is "no key".

Storing keys

name() is what 1.13+ writes to options.txt (key.keyboard.r, key.mouse.left), and fromName(String) reads it back, so a mod can store a key as text in its config:
String text = key.name();          // "key.keyboard.left.shift"
Key back = Key.fromName(text);     // equals key; null for text that is no key name

Old versions

1.7.10 – 1.12.2 use LWJGL 2 key codes, and mouse buttons as -100 + button; their backend (and a mod that compares keys there) converts with fromLwjgl2(int) and toLwjgl2(), the same table vanilla's 1.13 options upgrade uses. Keys LWJGL 2 doesn't know (F20 – F25, World 1/2) can't be bound there; LWJGL 2 keys GLFW doesn't know become UNBOUND.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Whether a Key is a keyboard key or a mouse button.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Key
    The Escape key (GLFW 256).
    static final Key
    No key: GLFW_KEY_UNKNOWN (-1), named key.keyboard.unknown, shown as "Not Bound".
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    The GLFW key code for a keyboard key (-1 for UNBOUND), the button number for a mouse button.
    The English name, as vanilla's en_us shows it: "R", "Left Shift", "Button 4", "Not Bound".
    boolean
    Equal if both are the same type with the same code.
    static Key
    fromLwjgl2(int code)
    The key for a 1.7.10 – 1.12.2 key code, as vanilla's KeyBinding stores them there: an LWJGL 2 Keyboard.KEY_* code, or -100 + button for a mouse button.
    static Key
    The key named name as in 1.13+'s options.txt: the inverse of name().
    int
     
    boolean
    Whether this is UNBOUND, "no key".
    static Key
    keyboard(int glfwCode)
    The keyboard key with GLFW key code glfwCode, e.g.
    static Key
    mouse(int button)
    A mouse button.
    The key's name as in 1.13+'s options.txt: key.keyboard.r, key.mouse.left, key.keyboard.unknown.
    int
    The 1.7.10 – 1.12.2 key code: the inverse of fromLwjgl2(int).
    The name().
    Whether this is a keyboard key (including UNBOUND) or a mouse button.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • UNBOUND

      public static final Key UNBOUND
      No key: GLFW_KEY_UNKNOWN (-1), named key.keyboard.unknown, shown as "Not Bound".
    • ESCAPE

      public static final Key ESCAPE
      The Escape key (GLFW 256). Pressed while a Keybind waits for a key, it unbinds instead of binding itself, as vanilla's Controls screen does.
  • Method Details

    • keyboard

      public static Key keyboard(int glfwCode)
      The keyboard key with GLFW key code glfwCode, e.g. Key.keyboard(82) for R (GLFW_KEY_R). Codes the library has no name for are accepted too.
      Parameters:
      glfwCode - a GLFW key code (32 and up), or -1 (GLFW_KEY_UNKNOWN) for UNBOUND
      Returns:
      the key
      Throws:
      IllegalArgumentException - if glfwCode is below 32 and not -1
    • mouse

      public static Key mouse(int button)
      A mouse button.
      Parameters:
      button - 0 left, 1 right, 2 middle, 3 and up the side buttons
      Returns:
      the key
      Throws:
      IllegalArgumentException - if button is negative
    • fromName

      public static Key fromName(String name)
      The key named name as in 1.13+'s options.txt: the inverse of name(). Examples: key.keyboard.r, key.keyboard.left.shift, key.mouse.left, key.mouse.4 (the first side button, number 3), key.keyboard.unknown (UNBOUND). Keys without a name are written key.keyboard.<GLFW code>.
      Parameters:
      name - a key name, may be null
      Returns:
      the key, or null if name is null or no key name
    • fromLwjgl2

      public static Key fromLwjgl2(int code)
      The key for a 1.7.10 – 1.12.2 key code, as vanilla's KeyBinding stores them there: an LWJGL 2 Keyboard.KEY_* code, or -100 + button for a mouse button.
      Parameters:
      code - an LWJGL 2 key code or -100 + button
      Returns:
      the key; UNBOUND for 0 (KEY_NONE) and codes without a GLFW equivalent
    • type

      public Key.Type type()
      Whether this is a keyboard key (including UNBOUND) or a mouse button.
      Returns:
      Key.Type.KEYBOARD or Key.Type.MOUSE
    • code

      public int code()
      The GLFW key code for a keyboard key (-1 for UNBOUND), the button number for a mouse button.
      Returns:
      the GLFW key code, -1 for UNBOUND, or the button number for a mouse button
    • isUnbound

      public boolean isUnbound()
      Whether this is UNBOUND, "no key".
      Returns:
      true for a keyboard key with no code, that is UNBOUND
    • name

      public String name()
      The key's name as in 1.13+'s options.txt: key.keyboard.r, key.mouse.left, key.keyboard.unknown. A stable text form for storing keys; fromName(String) reads it back.
      Returns:
      the name as in options.txt; key.keyboard.unknown for UNBOUND
    • toLwjgl2

      public int toLwjgl2()
      The 1.7.10 – 1.12.2 key code: the inverse of fromLwjgl2(int).
      Returns:
      an LWJGL 2 key code, -100 + button for a mouse button, or 0 (KEY_NONE) for UNBOUND and keys LWJGL 2 doesn't have
    • englishName

      public String englishName()
      The English name, as vanilla's en_us shows it: "R", "Left Shift", "Button 4", "Not Bound". For tests, logs and as a fallback only: backends show vanilla's own name, which is translated and follows the keyboard layout.
      Returns:
      the English name, such as "R", "Button 4" or "Not Bound" for UNBOUND
    • equals

      public boolean equals(Object o)
      Equal if both are the same type with the same code.
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      The name().
      Overrides:
      toString in class Object