[go: up one dir, main page]

Version: Unity 6 (6000.0)
LanguageEnglish
  • C#

KeyCode

enumeration

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

Key codes returned by Event.keyCode. These map directly to a physical key on the keyboard. If "Use Physical Keys" is enabled in Input Manager settings, these 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

Key codes can be used to detect key down and key up events, using Input.GetKeyDown and Input.GetKeyUp:

using UnityEngine;

public class KeyCodeExample : MonoBehaviour { void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Debug.Log("Space key was pressed."); }

if (Input.GetKeyUp(KeyCode.Space)) { Debug.Log("Space key was released."); } } }

Keyboard events can also be captured within OnGUI:

using UnityEngine;

public class KeyCodeOnGUIExample : MonoBehaviour { void OnGUI() { if (Event.current.Equals(Event.KeyboardEvent(KeyCode.Space.ToString()))) { Debug.Log("Space key is pressed."); } } }

For joystick and gamepad button presses, consider using Input.GetButtonDown and Input.GetButtonUp instead of the KeyCode. These methods allow you to check input state using a descriptive action string, e.g. "fire" or "jump", instead of the hardware button number.

The Input pages provide details about accessing keyboard, mouse and joystick input.

Properties

NoneNot assigned (never returned as the result of a keystroke).
BackspaceThe backspace key.
DeleteThe forward delete key.
TabThe tab key.
ClearThe Clear key.
ReturnReturn key.
PausePause on PC machines.
EscapeEscape key.
SpaceSpace key.
Keypad0Numeric keypad 0.
Keypad1Numeric keypad 1.
Keypad2Numeric keypad 2.
Keypad3Numeric keypad 3.
Keypad4Numeric keypad 4.
Keypad5Numeric keypad 5.
Keypad6Numeric keypad 6.
Keypad7Numeric keypad 7.
Keypad8Numeric keypad 8.
Keypad9Numeric keypad 9.
KeypadPeriodNumeric keypad '.'.
KeypadDivideNumeric keypad '/'.
KeypadMultiplyNumeric keypad '*'.
KeypadMinusNumeric keypad '-'.
KeypadPlusNumeric keypad '+'.
KeypadEnterNumeric keypad Enter.
KeypadEqualsNumeric keypad '='.
UpArrowUp arrow key.
DownArrowDown arrow key.
RightArrowRight arrow key.
LeftArrowLeft arrow key.
InsertInsert key key.
HomeHome key.
EndEnd key.
PageUpPage up.
PageDownPage down.
F1F1 function key.
F2F2 function key.
F3F3 function key.
F4F4 function key.
F5F5 function key.
F6F6 function key.
F7F7 function key.
F8F8 function key.
F9F9 function key.
F10F10 function key.
F11F11 function key.
F12F12 function key.
F13F13 function key.
F14F14 function key.
F15F15 function key.
Alpha0The '0' key on the top of the alphanumeric keyboard.
Alpha1The '1' key on the top of the alphanumeric keyboard.
Alpha2The '2' key on the top of the alphanumeric keyboard.
Alpha3The '3' key on the top of the alphanumeric keyboard.
Alpha4The '4' key on the top of the alphanumeric keyboard.
Alpha5The '5' key on the top of the alphanumeric keyboard.
Alpha6The '6' key on the top of the alphanumeric keyboard.
Alpha7The '7' key on the top of the alphanumeric keyboard.
Alpha8The '8' key on the top of the alphanumeric keyboard.
Alpha9The '9' key on the top of the alphanumeric keyboard.
ExclaimExclamation mark key '!'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha1 instead.
DoubleQuoteDouble quote key '"'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Quote instead.
HashHash key '#'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha3 instead.
DollarDollar sign key '$'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha4 instead.
PercentPercent '%' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha5 instead.
AmpersandAmpersand key '&'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha7 instead.
QuoteQuote key '.
LeftParenLeft Parenthesis key '('. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha9 instead.
RightParenRight Parenthesis key ')'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha0 instead.
AsteriskAsterisk key '*'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha8 instead.
PlusPlus key '+'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Equals instead.
CommaComma ',' key.
MinusMinus '-' key.
PeriodPeriod '.' key.
SlashSlash '/' key.
ColonColon ':' key.Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Semicolon instead.
SemicolonSemicolon ';' key.
LessLess than '<' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Comma instead.
EqualsEquals '=' key.
GreaterGreater than '>' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Period instead.
QuestionQuestion mark '?' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Slash instead.
AtAt key '@'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha2 instead.
LeftBracketLeft square bracket key '['.
BackslashBackslash key '\'.
RightBracketRight square bracket key ']'.
CaretCaret key '^'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Alpha6 instead.
UnderscoreUnderscore '_' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Minus instead.
BackQuoteBack quote key '`'.
A'a' key.
B'b' key.
C'c' key.
D'd' key.
E'e' key.
F'f' key.
G'g' key.
H'h' key.
I'i' key.
J'j' key.
K'k' key.
L'l' key.
M'm' key.
N'n' key.
O'o' key.
P'p' key.
Q'q' key.
R'r' key.
S's' key.
T't' key.
U'u' key.
V'v' key.
W'w' key.
X'x' key.
Y'y' key.
Z'z' key.
LeftCurlyBracketLeft curly bracket key '{'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.LeftBracket instead.
PipePipe '|' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.Backslash instead.
RightCurlyBracketRight curly bracket key '}'. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.RightBracket instead.
TildeTilde '~' key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.BackQuote instead.
NumlockNumlock key.
CapsLockCapslock key.
ScrollLockScroll lock key.
RightShiftRight shift key.
LeftShiftLeft shift key.
RightControlRight Control key.
LeftControlLeft Control key.
RightAltRight Alt key.
LeftAltLeft Alt key.
LeftMetaMaps to left Windows key or left Command key if physical keys are enabled in Input Manager settings, otherwise maps to left Command key only.
LeftCommandLeft Command key.
LeftAppleLeft Command key.
LeftWindowsLeft Windows key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.LeftMeta instead.
RightMetaMaps to right Windows key or right Command key if physical keys are enabled in Input Manager settings, otherwise maps to right Command key only.
RightCommandRight Command key.
RightAppleRight Command key.
RightWindowsRight Windows key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.RightMeta instead.
AltGrAlt Gr key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, use KeyCode.RightAlt instead.
HelpHelp key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, doesn't map to any physical key.
PrintPrint key.
SysReqSys Req key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, doesn't map to any physical key.
BreakBreak key. Deprecated if "Use Physical Keys" is enabled in Input Manager settings, doesn't map to any physical key.
MenuMenu key.
WheelUpMouse wheel up.
WheelDownMouse wheel down.
Mouse0The Left (or primary) mouse button.
Mouse1Right mouse button (or secondary mouse button).
Mouse2Middle mouse button (or third button).
Mouse3Additional (fourth) mouse button.
Mouse4Additional (fifth) mouse button.
Mouse5Additional (or sixth) mouse button.
Mouse6Additional (or seventh) mouse button.
JoystickButton0Button 0 on any joystick.
JoystickButton1Button 1 on any joystick.
JoystickButton2Button 2 on any joystick.
JoystickButton3Button 3 on any joystick.
JoystickButton4Button 4 on any joystick.
JoystickButton5Button 5 on any joystick.
JoystickButton6Button 6 on any joystick.
JoystickButton7Button 7 on any joystick.
JoystickButton8Button 8 on any joystick.
JoystickButton9Button 9 on any joystick.
JoystickButton10Button 10 on any joystick.
JoystickButton11Button 11 on any joystick.
JoystickButton12Button 12 on any joystick.
JoystickButton13Button 13 on any joystick.
JoystickButton14Button 14 on any joystick.
JoystickButton15Button 15 on any joystick.
JoystickButton16Button 16 on any joystick.
JoystickButton17Button 17 on any joystick.
JoystickButton18Button 18 on any joystick.
JoystickButton19Button 19 on any joystick.
Joystick1Button0Button 0 on first joystick.
Joystick1Button1Button 1 on first joystick.
Joystick1Button2Button 2 on first joystick.
Joystick1Button3Button 3 on first joystick.
Joystick1Button4Button 4 on first joystick.
Joystick1Button5Button 5 on first joystick.
Joystick1Button6Button 6 on first joystick.
Joystick1Button7Button 7 on first joystick.
Joystick1Button8Button 8 on first joystick.
Joystick1Button9Button 9 on first joystick.
Joystick1Button10Button 10 on first joystick.
Joystick1Button11Button 11 on first joystick.
Joystick1Button12Button 12 on first joystick.
Joystick1Button13Button 13 on first joystick.
Joystick1Button14Button 14 on first joystick.
Joystick1Button15Button 15 on first joystick.
Joystick1Button16Button 16 on first joystick.
Joystick1Button17Button 17 on first joystick.
Joystick1Button18Button 18 on first joystick.
Joystick1Button19Button 19 on first joystick.
Joystick2Button0Button 0 on second joystick.
Joystick2Button1Button 1 on second joystick.
Joystick2Button2Button 2 on second joystick.
Joystick2Button3Button 3 on second joystick.
Joystick2Button4Button 4 on second joystick.
Joystick2Button5Button 5 on second joystick.
Joystick2Button6Button 6 on second joystick.
Joystick2Button7Button 7 on second joystick.
Joystick2Button8Button 8 on second joystick.
Joystick2Button9Button 9 on second joystick.
Joystick2Button10Button 10 on second joystick.
Joystick2Button11Button 11 on second joystick.
Joystick2Button12Button 12 on second joystick.
Joystick2Button13Button 13 on second joystick.
Joystick2Button14Button 14 on second joystick.
Joystick2Button15Button 15 on second joystick.
Joystick2Button16Button 16 on second joystick.
Joystick2Button17Button 17 on second joystick.
Joystick2Button18Button 18 on second joystick.
Joystick2Button19Button 19 on second joystick.
Joystick3Button0Button 0 on third joystick.
Joystick3Button1Button 1 on third joystick.
Joystick3Button2Button 2 on third joystick.
Joystick3Button3Button 3 on third joystick.
Joystick3Button4Button 4 on third joystick.
Joystick3Button5Button 5 on third joystick.
Joystick3Button6Button 6 on third joystick.
Joystick3Button7Button 7 on third joystick.
Joystick3Button8Button 8 on third joystick.
Joystick3Button9Button 9 on third joystick.
Joystick3Button10Button 10 on third joystick.
Joystick3Button11Button 11 on third joystick.
Joystick3Button12Button 12 on third joystick.
Joystick3Button13Button 13 on third joystick.
Joystick3Button14Button 14 on third joystick.
Joystick3Button15Button 15 on third joystick.
Joystick3Button16Button 16 on third joystick.
Joystick3Button17Button 17 on third joystick.
Joystick3Button18Button 18 on third joystick.
Joystick3Button19Button 19 on third joystick.
Joystick4Button0Button 0 on forth joystick.
Joystick4Button1Button 1 on forth joystick.
Joystick4Button2Button 2 on forth joystick.
Joystick4Button3Button 3 on forth joystick.
Joystick4Button4Button 4 on forth joystick.
Joystick4Button5Button 5 on forth joystick.
Joystick4Button6Button 6 on forth joystick.
Joystick4Button7Button 7 on forth joystick.
Joystick4Button8Button 8 on forth joystick.
Joystick4Button9Button 9 on forth joystick.
Joystick4Button10Button 10 on forth joystick.
Joystick4Button11Button 11 on forth joystick.
Joystick4Button12Button 12 on forth joystick.
Joystick4Button13Button 13 on forth joystick.
Joystick4Button14Button 14 on forth joystick.
Joystick4Button15Button 15 on forth joystick.
Joystick4Button16Button 16 on forth joystick.
Joystick4Button17Button 17 on forth joystick.
Joystick4Button18Button 18 on forth joystick.
Joystick4Button19Button 19 on forth joystick.
Joystick5Button0Button 0 on fifth joystick.
Joystick5Button1Button 1 on fifth joystick.
Joystick5Button2Button 2 on fifth joystick.
Joystick5Button3Button 3 on fifth joystick.
Joystick5Button4Button 4 on fifth joystick.
Joystick5Button5Button 5 on fifth joystick.
Joystick5Button6Button 6 on fifth joystick.
Joystick5Button7Button 7 on fifth joystick.
Joystick5Button8Button 8 on fifth joystick.
Joystick5Button9Button 9 on fifth joystick.
Joystick5Button10Button 10 on fifth joystick.
Joystick5Button11Button 11 on fifth joystick.
Joystick5Button12Button 12 on fifth joystick.
Joystick5Button13Button 13 on fifth joystick.
Joystick5Button14Button 14 on fifth joystick.
Joystick5Button15Button 15 on fifth joystick.
Joystick5Button16Button 16 on fifth joystick.
Joystick5Button17Button 17 on fifth joystick.
Joystick5Button18Button 18 on fifth joystick.
Joystick5Button19Button 19 on fifth joystick.
Joystick6Button0Button 0 on sixth joystick.
Joystick6Button1Button 1 on sixth joystick.
Joystick6Button2Button 2 on sixth joystick.
Joystick6Button3Button 3 on sixth joystick.
Joystick6Button4Button 4 on sixth joystick.
Joystick6Button5Button 5 on sixth joystick.
Joystick6Button6Button 6 on sixth joystick.
Joystick6Button7Button 7 on sixth joystick.
Joystick6Button8Button 8 on sixth joystick.
Joystick6Button9Button 9 on sixth joystick.
Joystick6Button10Button 10 on sixth joystick.
Joystick6Button11Button 11 on sixth joystick.
Joystick6Button12Button 12 on sixth joystick.
Joystick6Button13Button 13 on sixth joystick.
Joystick6Button14Button 14 on sixth joystick.
Joystick6Button15Button 15 on sixth joystick.
Joystick6Button16Button 16 on sixth joystick.
Joystick6Button17Button 17 on sixth joystick.
Joystick6Button18Button 18 on sixth joystick.
Joystick6Button19Button 19 on sixth joystick.
Joystick7Button0Button 0 on seventh joystick.
Joystick7Button1Button 1 on seventh joystick.
Joystick7Button2Button 2 on seventh joystick.
Joystick7Button3Button 3 on seventh joystick.
Joystick7Button4Button 4 on seventh joystick.
Joystick7Button5Button 5 on seventh joystick.
Joystick7Button6Button 6 on seventh joystick.
Joystick7Button7Button 7 on seventh joystick.
Joystick7Button8Button 8 on seventh joystick.
Joystick7Button9Button 9 on seventh joystick.
Joystick7Button10Button 10 on seventh joystick.
Joystick7Button11Button 11 on seventh joystick.
Joystick7Button12Button 12 on seventh joystick.
Joystick7Button13Button 13 on seventh joystick.
Joystick7Button14Button 14 on seventh joystick.
Joystick7Button15Button 15 on seventh joystick.
Joystick7Button16Button 16 on seventh joystick.
Joystick7Button17Button 17 on seventh joystick.
Joystick7Button18Button 18 on seventh joystick.
Joystick7Button19Button 19 on seventh joystick.
Joystick8Button0Button 0 on eighth joystick.
Joystick8Button1Button 1 on eighth joystick.
Joystick8Button2Button 2 on eighth joystick.
Joystick8Button3Button 3 on eighth joystick.
Joystick8Button4Button 4 on eighth joystick.
Joystick8Button5Button 5 on eighth joystick.
Joystick8Button6Button 6 on eighth joystick.
Joystick8Button7Button 7 on eighth joystick.
Joystick8Button8Button 8 on eighth joystick.
Joystick8Button9Button 9 on eighth joystick.
Joystick8Button10Button 10 on eighth joystick.
Joystick8Button11Button 11 on eighth joystick.
Joystick8Button12Button 12 on eighth joystick.
Joystick8Button13Button 13 on eighth joystick.
Joystick8Button14Button 14 on eighth joystick.
Joystick8Button15Button 15 on eighth joystick.
Joystick8Button16Button 16 on eighth joystick.
Joystick8Button17Button 17 on eighth joystick.
Joystick8Button18Button 18 on eighth joystick.
Joystick8Button19Button 19 on eighth joystick.