KeyCode
The code to use for identifying input. These codes map directly to a physical key on the keyboard or language-dependent keys.
Read time 13 minutesLast updated 5 days ago
Definition
- Type: Enum
- Namespace: UnityEngine
- Assembly: UnityEngine.CoreModule
public enum KeyCode
Remarks
If Use Physical Keys is enabled in Input Manager settings, these codes map directly to a physical key on the keyboard. If Use Physical Keys is disabled these map to language dependent mapping, different for every platform and cannot be guaranteed to work. Use Physical Keys is enabled by default from 2022.1.
You can use key codes to detect key down and key up events, using Input.GetKeyDown and Input.GetKeyUp at runtime for gameplay. For joystick and gamepad button presses, consider using Input.GetButtonDown and Input.GetButtonUp instead of the . You can use these methods to check an input state with a descriptive action string, such as "fire" or "jump", instead of the hardware button number.
KeyCodeRefer to Input to learn more about accessing keyboard, mouse, and joystick input. Consider using the new Input System package instead of KeyCode-based input.
using UnityEngine;public class PlayerInput : MonoBehaviour{ void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("Jump!"); } float horizontal = 0f; if (Input.GetKey(KeyCode.A)) horizontal -= 1f; if (Input.GetKey(KeyCode.D)) horizontal += 1f; transform.Translate(new Vector3(horizontal * Time.deltaTime, 0, 0)); }}
Keyboard events can also be captured within in the Editor for direct key handling.
OnGUIusing UnityEngine;using UnityEditor;public class Counter : MonoBehaviour{ public int counter;}[CustomEditor(typeof(Counter))]public class CounterEditor : UnityEditor.Editor{ public override void OnInspectorGUI() { DrawDefaultInspector(); Counter myTarget = (Counter)target; Event e = Event.current; if (e.type == EventType.KeyDown) { if (e.keyCode == KeyCode.UpArrow) { myTarget.counter++; GUI.changed = true; } else if (e.keyCode == KeyCode.DownArrow) { myTarget.counter--; GUI.changed = true; } } if (GUI.changed) { EditorUtility.SetDirty(target); } }}
While KeyCode is used to define keys in shortcuts, the ShortcutManager provides a higher-level API for Editor shortcuts.
using UnityEngine;using UnityEditor;using UnityEditor.ShortcutManagement;public static class ShortcutDefinitions{ [Shortcut("Examples/Log Message", KeyCode.L, ShortcutModifiers.Alt)] public static void LogMessage() { Debug.Log("Shortcut activated!"); } [MenuItem("Tools/Minimal Shortcut Example")] private static void ShowShortcutInfo() { Debug.Log("Press Alt+L to activate the shortcut."); }}
Additional Resources: Input, ShortcutManager.
Fields
Value | Description |
|---|---|
| A | 'a' key. |
| Alpha0 | The '0' key on the top of the alphanumeric keyboard. |
| Alpha1 | The '1' key on the top of the alphanumeric keyboard. |
| Alpha2 | The '2' key on the top of the alphanumeric keyboard. |
| Alpha3 | The '3' key on the top of the alphanumeric keyboard. |
| Alpha4 | The '4' key on the top of the alphanumeric keyboard. |
| Alpha5 | The '5' key on the top of the alphanumeric keyboard. |
| Alpha6 | The '6' key on the top of the alphanumeric keyboard. |
| Alpha7 | The '7' key on the top of the alphanumeric keyboard. |
| Alpha8 | The '8' key on the top of the alphanumeric keyboard. |
| Alpha9 | The '9' key on the top of the alphanumeric keyboard. |
| AltGr | Alt Gr key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.RightAlt instead. |
| Ampersand | Ampersand key '&'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha7 instead. |
| Asterisk | Asterisk key '*'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha8 instead. |
| At | At key '@'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha2 instead. |
| B | 'b' key. |
| BackQuote | Back quote key '`'. |
| Backslash | Backslash key ''. |
| Backspace | The backspace key. |
| Break | Break key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, doesn't map to any physical key. |
| C | 'c' key. |
| CapsLock | Capslock key. |
| Caret | Caret key '^'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha6 instead. |
| Clear | The Clear key. |
| Colon | Colon ':' key.Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Semicolon instead. |
| Comma | Comma ',' key. |
| D | 'd' key. |
| Delete | The forward delete key. |
| Dollar | Dollar sign key '$'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha4 instead. |
| DoubleQuote | Double quote key '"'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Quote instead. |
| DownArrow | Down arrow key. |
| E | 'e' key. |
| End | End key. |
| Equals | Equals '=' key. |
| Escape | Escape key. |
| Exclaim | Exclamation mark key '!'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha1 instead. |
| F | 'f' key. |
| F1 | F1 function key. |
| F10 | F10 function key. |
| F11 | F11 function key. |
| F12 | F12 function key. |
| F13 | F13 function key. |
| F14 | F14 function key. |
| F15 | F15 function key. |
| F16 | F16 function key. |
| F17 | F17 function key. |
| F18 | F18 function key. |
| F19 | F19 function key. |
| F2 | F2 function key. |
| F20 | F20 function key. |
| F21 | F21 function key. |
| F22 | F22 function key. |
| F23 | F23 function key. |
| F24 | F24 function key. |
| F3 | F3 function key. |
| F4 | F4 function key. |
| F5 | F5 function key. |
| F6 | F6 function key. |
| F7 | F7 function key. |
| F8 | F8 function key. |
| F9 | F9 function key. |
| G | 'g' key. |
| Greater | Greater than '>' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Period instead. |
| H | 'h' key. |
| Hash | Hash key '#'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha3 instead. |
| Help | Help key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, doesn't map to any physical key. |
| Home | Home key. |
| I | 'i' key. |
| Insert | Insert key key. |
| J | 'j' key. |
| Joystick1Button0 | Button 0 on first joystick. |
| Joystick1Button1 | Button 1 on first joystick. |
| Joystick1Button10 | Button 10 on first joystick. |
| Joystick1Button11 | Button 11 on first joystick. |
| Joystick1Button12 | Button 12 on first joystick. |
| Joystick1Button13 | Button 13 on first joystick. |
| Joystick1Button14 | Button 14 on first joystick. |
| Joystick1Button15 | Button 15 on first joystick. |
| Joystick1Button16 | Button 16 on first joystick. |
| Joystick1Button17 | Button 17 on first joystick. |
| Joystick1Button18 | Button 18 on first joystick. |
| Joystick1Button19 | Button 19 on first joystick. |
| Joystick1Button2 | Button 2 on first joystick. |
| Joystick1Button3 | Button 3 on first joystick. |
| Joystick1Button4 | Button 4 on first joystick. |
| Joystick1Button5 | Button 5 on first joystick. |
| Joystick1Button6 | Button 6 on first joystick. |
| Joystick1Button7 | Button 7 on first joystick. |
| Joystick1Button8 | Button 8 on first joystick. |
| Joystick1Button9 | Button 9 on first joystick. |
| Joystick2Button0 | Button 0 on second joystick. |
| Joystick2Button1 | Button 1 on second joystick. |
| Joystick2Button10 | Button 10 on second joystick. |
| Joystick2Button11 | Button 11 on second joystick. |
| Joystick2Button12 | Button 12 on second joystick. |
| Joystick2Button13 | Button 13 on second joystick. |
| Joystick2Button14 | Button 14 on second joystick. |
| Joystick2Button15 | Button 15 on second joystick. |
| Joystick2Button16 | Button 16 on second joystick. |
| Joystick2Button17 | Button 17 on second joystick. |
| Joystick2Button18 | Button 18 on second joystick. |
| Joystick2Button19 | Button 19 on second joystick. |
| Joystick2Button2 | Button 2 on second joystick. |
| Joystick2Button3 | Button 3 on second joystick. |
| Joystick2Button4 | Button 4 on second joystick. |
| Joystick2Button5 | Button 5 on second joystick. |
| Joystick2Button6 | Button 6 on second joystick. |
| Joystick2Button7 | Button 7 on second joystick. |
| Joystick2Button8 | Button 8 on second joystick. |
| Joystick2Button9 | Button 9 on second joystick. |
| Joystick3Button0 | Button 0 on third joystick. |
| Joystick3Button1 | Button 1 on third joystick. |
| Joystick3Button10 | Button 10 on third joystick. |
| Joystick3Button11 | Button 11 on third joystick. |
| Joystick3Button12 | Button 12 on third joystick. |
| Joystick3Button13 | Button 13 on third joystick. |
| Joystick3Button14 | Button 14 on third joystick. |
| Joystick3Button15 | Button 15 on third joystick. |
| Joystick3Button16 | Button 16 on third joystick. |
| Joystick3Button17 | Button 17 on third joystick. |
| Joystick3Button18 | Button 18 on third joystick. |
| Joystick3Button19 | Button 19 on third joystick. |
| Joystick3Button2 | Button 2 on third joystick. |
| Joystick3Button3 | Button 3 on third joystick. |
| Joystick3Button4 | Button 4 on third joystick. |
| Joystick3Button5 | Button 5 on third joystick. |
| Joystick3Button6 | Button 6 on third joystick. |
| Joystick3Button7 | Button 7 on third joystick. |
| Joystick3Button8 | Button 8 on third joystick. |
| Joystick3Button9 | Button 9 on third joystick. |
| Joystick4Button0 | Button 0 on forth joystick. |
| Joystick4Button1 | Button 1 on forth joystick. |
| Joystick4Button10 | Button 10 on forth joystick. |
| Joystick4Button11 | Button 11 on forth joystick. |
| Joystick4Button12 | Button 12 on forth joystick. |
| Joystick4Button13 | Button 13 on forth joystick. |
| Joystick4Button14 | Button 14 on forth joystick. |
| Joystick4Button15 | Button 15 on forth joystick. |
| Joystick4Button16 | Button 16 on forth joystick. |
| Joystick4Button17 | Button 17 on forth joystick. |
| Joystick4Button18 | Button 18 on forth joystick. |
| Joystick4Button19 | Button 19 on forth joystick. |
| Joystick4Button2 | Button 2 on forth joystick. |
| Joystick4Button3 | Button 3 on forth joystick. |
| Joystick4Button4 | Button 4 on forth joystick. |
| Joystick4Button5 | Button 5 on forth joystick. |
| Joystick4Button6 | Button 6 on forth joystick. |
| Joystick4Button7 | Button 7 on forth joystick. |
| Joystick4Button8 | Button 8 on forth joystick. |
| Joystick4Button9 | Button 9 on forth joystick. |
| Joystick5Button0 | Button 0 on fifth joystick. |
| Joystick5Button1 | Button 1 on fifth joystick. |
| Joystick5Button10 | Button 10 on fifth joystick. |
| Joystick5Button11 | Button 11 on fifth joystick. |
| Joystick5Button12 | Button 12 on fifth joystick. |
| Joystick5Button13 | Button 13 on fifth joystick. |
| Joystick5Button14 | Button 14 on fifth joystick. |
| Joystick5Button15 | Button 15 on fifth joystick. |
| Joystick5Button16 | Button 16 on fifth joystick. |
| Joystick5Button17 | Button 17 on fifth joystick. |
| Joystick5Button18 | Button 18 on fifth joystick. |
| Joystick5Button19 | Button 19 on fifth joystick. |
| Joystick5Button2 | Button 2 on fifth joystick. |
| Joystick5Button3 | Button 3 on fifth joystick. |
| Joystick5Button4 | Button 4 on fifth joystick. |
| Joystick5Button5 | Button 5 on fifth joystick. |
| Joystick5Button6 | Button 6 on fifth joystick. |
| Joystick5Button7 | Button 7 on fifth joystick. |
| Joystick5Button8 | Button 8 on fifth joystick. |
| Joystick5Button9 | Button 9 on fifth joystick. |
| Joystick6Button0 | Button 0 on sixth joystick. |
| Joystick6Button1 | Button 1 on sixth joystick. |
| Joystick6Button10 | Button 10 on sixth joystick. |
| Joystick6Button11 | Button 11 on sixth joystick. |
| Joystick6Button12 | Button 12 on sixth joystick. |
| Joystick6Button13 | Button 13 on sixth joystick. |
| Joystick6Button14 | Button 14 on sixth joystick. |
| Joystick6Button15 | Button 15 on sixth joystick. |
| Joystick6Button16 | Button 16 on sixth joystick. |
| Joystick6Button17 | Button 17 on sixth joystick. |
| Joystick6Button18 | Button 18 on sixth joystick. |
| Joystick6Button19 | Button 19 on sixth joystick. |
| Joystick6Button2 | Button 2 on sixth joystick. |
| Joystick6Button3 | Button 3 on sixth joystick. |
| Joystick6Button4 | Button 4 on sixth joystick. |
| Joystick6Button5 | Button 5 on sixth joystick. |
| Joystick6Button6 | Button 6 on sixth joystick. |
| Joystick6Button7 | Button 7 on sixth joystick. |
| Joystick6Button8 | Button 8 on sixth joystick. |
| Joystick6Button9 | Button 9 on sixth joystick. |
| Joystick7Button0 | Button 0 on seventh joystick. |
| Joystick7Button1 | Button 1 on seventh joystick. |
| Joystick7Button10 | Button 10 on seventh joystick. |
| Joystick7Button11 | Button 11 on seventh joystick. |
| Joystick7Button12 | Button 12 on seventh joystick. |
| Joystick7Button13 | Button 13 on seventh joystick. |
| Joystick7Button14 | Button 14 on seventh joystick. |
| Joystick7Button15 | Button 15 on seventh joystick. |
| Joystick7Button16 | Button 16 on seventh joystick. |
| Joystick7Button17 | Button 17 on seventh joystick. |
| Joystick7Button18 | Button 18 on seventh joystick. |
| Joystick7Button19 | Button 19 on seventh joystick. |
| Joystick7Button2 | Button 2 on seventh joystick. |
| Joystick7Button3 | Button 3 on seventh joystick. |
| Joystick7Button4 | Button 4 on seventh joystick. |
| Joystick7Button5 | Button 5 on seventh joystick. |
| Joystick7Button6 | Button 6 on seventh joystick. |
| Joystick7Button7 | Button 7 on seventh joystick. |
| Joystick7Button8 | Button 8 on seventh joystick. |
| Joystick7Button9 | Button 9 on seventh joystick. |
| Joystick8Button0 | Button 0 on eighth joystick. |
| Joystick8Button1 | Button 1 on eighth joystick. |
| Joystick8Button10 | Button 10 on eighth joystick. |
| Joystick8Button11 | Button 11 on eighth joystick. |
| Joystick8Button12 | Button 12 on eighth joystick. |
| Joystick8Button13 | Button 13 on eighth joystick. |
| Joystick8Button14 | Button 14 on eighth joystick. |
| Joystick8Button15 | Button 15 on eighth joystick. |
| Joystick8Button16 | Button 16 on eighth joystick. |
| Joystick8Button17 | Button 17 on eighth joystick. |
| Joystick8Button18 | Button 18 on eighth joystick. |
| Joystick8Button19 | Button 19 on eighth joystick. |
| Joystick8Button2 | Button 2 on eighth joystick. |
| Joystick8Button3 | Button 3 on eighth joystick. |
| Joystick8Button4 | Button 4 on eighth joystick. |
| Joystick8Button5 | Button 5 on eighth joystick. |
| Joystick8Button6 | Button 6 on eighth joystick. |
| Joystick8Button7 | Button 7 on eighth joystick. |
| Joystick8Button8 | Button 8 on eighth joystick. |
| Joystick8Button9 | Button 9 on eighth joystick. |
| JoystickButton0 | Button 0 on any joystick. |
| JoystickButton1 | Button 1 on any joystick. |
| JoystickButton10 | Button 10 on any joystick. |
| JoystickButton11 | Button 11 on any joystick. |
| JoystickButton12 | Button 12 on any joystick. |
| JoystickButton13 | Button 13 on any joystick. |
| JoystickButton14 | Button 14 on any joystick. |
| JoystickButton15 | Button 15 on any joystick. |
| JoystickButton16 | Button 16 on any joystick. |
| JoystickButton17 | Button 17 on any joystick. |
| JoystickButton18 | Button 18 on any joystick. |
| JoystickButton19 | Button 19 on any joystick. |
| JoystickButton2 | Button 2 on any joystick. |
| JoystickButton3 | Button 3 on any joystick. |
| JoystickButton4 | Button 4 on any joystick. |
| JoystickButton5 | Button 5 on any joystick. |
| JoystickButton6 | Button 6 on any joystick. |
| JoystickButton7 | Button 7 on any joystick. |
| JoystickButton8 | Button 8 on any joystick. |
| JoystickButton9 | Button 9 on any joystick. |
| K | 'k' key. |
| Keypad0 | Numeric keypad 0. |
| Keypad1 | Numeric keypad 1. |
| Keypad2 | Numeric keypad 2. |
| Keypad3 | Numeric keypad 3. |
| Keypad4 | Numeric keypad 4. |
| Keypad5 | Numeric keypad 5. |
| Keypad6 | Numeric keypad 6. |
| Keypad7 | Numeric keypad 7. |
| Keypad8 | Numeric keypad 8. |
| Keypad9 | Numeric keypad 9. |
| KeypadDivide | Numeric keypad '/'. |
| KeypadEnter | Numeric keypad |
| KeypadEquals | Numeric keypad '='. |
| KeypadMinus | Numeric keypad '-'. |
| KeypadMultiply | Numeric keypad '*'. |
| KeypadPeriod | Numeric keypad '.'. |
| KeypadPlus | Numeric keypad '+'. |
| L | 'l' key. |
| LeftAlt | Left Alt key. |
| LeftApple | Left Command key. |
| LeftArrow | Left arrow key. |
| LeftBracket | Left square bracket key '['. |
| LeftCommand | Left Command key. |
| LeftControl | Left Control key. |
| LeftCurlyBracket | Left curly bracket key '{'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.LeftBracket instead. |
| LeftMeta | Maps to left Windows key or left Command key if physical keys are enabled in Input Manager settings, otherwise maps to left Command key only. |
| LeftParen | Left Parenthesis key '('. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha9 instead. |
| LeftShift | Left shift key. |
| LeftWindows | Left Windows key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.LeftMeta instead. |
| Less | Less than '<' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Comma instead. |
| M | 'm' key. |
| Menu | Menu key. |
| Minus | Minus '-' key. |
| Mouse0 | The Left (or primary) mouse button. |
| Mouse1 | Right mouse button (or secondary mouse button). |
| Mouse2 | Middle mouse button (or third button). |
| Mouse3 | Additional (fourth) mouse button. |
| Mouse4 | Additional (fifth) mouse button. |
| Mouse5 | Additional (or sixth) mouse button. |
| Mouse6 | Additional (or seventh) mouse button. |
| N | 'n' key. |
| None | Not assigned (never returned as the result of a keystroke). |
| Numlock | Numlock key. |
| O | 'o' key. |
| P | 'p' key. |
| PageDown | Page down. |
| PageUp | Page up. |
| Pause | Pause on PC machines. |
| Percent | Percent '%' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha5 instead. |
| Period | Period '.' key. |
| Pipe | Pipe '|' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Backslash instead. |
| Plus | Plus key '+'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Equals instead. |
| Print key. | |
| Q | 'q' key. |
| Question | Question mark '?' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Slash instead. |
| Quote | Quote key '. |
| R | 'r' key. |
| Return | Return key. |
| RightAlt | Right Alt key. |
| RightApple | Right Command key. |
| RightArrow | Right arrow key. |
| RightBracket | Right square bracket key ']'. |
| RightCommand | Right Command key. |
| RightControl | Right Control key. |
| RightCurlyBracket | Right curly bracket key '}'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.RightBracket instead. |
| RightMeta | Maps to right Windows key or right Command key if physical keys are enabled in Input Manager settings, otherwise maps to right Command key only. |
| RightParen | Right Parenthesis key ')'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha0 instead. |
| RightShift | Right shift key. |
| RightWindows | Right Windows key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.RightMeta instead. |
| S | 's' key. |
| ScrollLock | Scroll lock key. |
| Semicolon | Semicolon ';' key. |
| Slash | Slash '/' key. |
| Space | Space key. |
| SysReq | Sys Req key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, doesn't map to any physical key. |
| T | 't' key. |
| Tab | The tab key. |
| Tilde | Tilde '~' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.BackQuote instead. |
| U | 'u' key. |
| Underscore | Underscore '_' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Minus instead. |
| UpArrow | Up arrow key. |
| V | 'v' key. |
| W | 'w' key. |
| WheelDown | Mouse wheel down. |
| WheelUp | Mouse wheel up. |
| X | 'x' key. |
| Y | 'y' key. |
| Z | 'z' key. |