Videogames Development – Unity – Keyboard, Mouse Buttons and Jostick Input Codes
BASIC USE
1. MAIN TOP MENU> Assets> Create Javascript
2. Assets> select NewBehaviourScript Import Setting
3. Inspector> ‘Open’ button, it starts MonoDevelop-Unity editor
4. Write:
#pragma strict function Start () { } function Update () { if (Input.GetKey ("up")) print ("up arrow key is held down"); if (Input.GetKey ("down")) print ("down arrow key is held down"); }
7. ‘Play’ button> press up and down arrow> BOTTOM LEFT you will see ‘up arrow key is held down’ or ‘down arrow key is held down’
UP – DOWN – HELD
#pragma strict function Update () { var down = Input.GetKeyDown(KeyCode.Space); // giu var held = Input.GetKey(KeyCode.Space); // mantenuto premuto a lungo var up = Input.GetKeyUp(KeyCode.Space); // su Debug.Log('variabile down ' + down); Debug.Log('variabile held ' + held); Debug.Log('variabile up ' + up); }
NOTICE:
Input.GetKeyDown()
Input.GetKey()
Input.GetKeyUp()
1. The key is not being pressed:
Input.GetKeyDown() -> FALSE
Input.GetKey() -> FALSE
Input.GetKeyUp() -> FALSE
2. When we first press the key, in the FIRST FRAME:
Input.GetKeyDown() -> TRUE
Input.GetKey() -> TRUE
Input.GetKeyUp() -> FALSE
3. As we PROGRESS THROUGTH FRAMES holding down the button (mantenendo premuto):
Input.GetKeyDown() -> FALSE
Input.GetKey() -> TRUE
Input.GetKeyUp() -> FALSE
4. When we release the button, in the FIRST FRAME::
Input.GetKeyDown() -> FALSE
Input.GetKey() -> FALSE
Input.GetKeyUp() -> TRUE
5. As we continue PROGRESS THROUGTH FRAMES (non premendo il bottone):
Input.GetKeyDown() -> FALSE
Input.GetKey() -> FALSE
Input.GetKeyUp() -> FALSE
FIRST TECNIQUE – it is based on Input Manager
Returns true while the user holds down the key.
Virtual Axes and buttons can be created in: MAIN TOP MENU> Edit> Project Settings> Input.
End users can configure Keyboard input in a nice screen configuration dialog.
Write:
#pragma strict function Start () { } function Update () { if (Input.GetKey ("up")) print ("up arrow key is held down"); if (Input.GetKey ("down")) print ("down arrow key is held down"); }
Statement: static function GetKey(name: string): bool;
‘name’ complete list:
Normal keys: “a”, “b”, “c” …
Number keys: “1”, “2”, “3”, …
Arrow keys: “up”, “down”, “left”, “right”
Keypad keys: “[1]”, “[2]”, “[3]”, “[+]”, “[equals]”
Modifier keys: “right shift”, “left shift”, “right ctrl”, “left ctrl”, “right alt”, “left alt”, “right cmd”, “left cmd”
Mouse Buttons: “mouse 0”, “mouse 1”, “mouse 2”, …
Joystick Buttons (from any joystick): “joystick button 0”, “joystick button 1”, “joystick button 2”, …
Joystick Buttons (from a specific joystick): “joystick 1 button 0”, “joystick 1 button 1”, “joystick 2 button 0”, …
Special keys: “backspace”, “tab”, “return”, “escape”, “space”, “delete”, “enter”, “insert”, “home”, “end”, “page up”, “page down”
Function keys: “f1”, “f2”, “f3”, …
SECOND TECNIQUE – to map directly to a physical key on the keyboard.
Returns true while the user holds down the key.
End users can not configure Keyboard input.
Write:
#pragma strict function Start () { } function Update () { if (Input.GetKey (KeyCode.UpArrow)) print ("up arrow key is held down"); if (Input.GetKey (KeyCode.DownArrow)) print ("down arrow key is held down"); }
Statement: static function GetKey(KeyCode.key): bool;
‘key’ complete list:
VARIABLES – Description
None Not assigned (never returned as the result of a keystroke).
Backspace The backspace key.
Delete The forward delete key.
Tab The tab key.
Clear The Clear key.
Return Return key.
Pause Pause on PC machines.
Escape Escape key.
Space Space 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.
KeypadPeriod Numeric keypad ‘.’.
KeypadDivide Numeric keypad ‘/’.
KeypadMultiply Numeric keypad ‘*’.
KeypadMinus Numeric keypad ‘-‘.
KeypadPlus Numeric keypad ‘+’.
KeypadEnter Numeric keypad enter.
KeypadEquals Numeric keypad ‘=’.
UpArrow Up arrow key.
DownArrow Down arrow key.
RightArrow Right arrow key.
LeftArrow Left arrow key.
Insert Insert key key.
Home Home key.
End End key.
PageUp Page up.
PageDown Page down.
F1 F1 function key.
F2 F2 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.
F10 F10 function key.
F11 F11 function key.
F12 F12 function key.
F13 F13 function key.
F14 F14 function key.
F15 F15 function 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.
Exclaim Exclamation mark key ‘!’.
DoubleQuote Double quote key ‘”‘.
Hash Hash key ‘#’.
Dollar Dollar sign key ‘$’.
Ampersand Ampersand key ‘&’.
Quote Quote key ‘.
LeftParen Left Parenthesis key ‘(‘.
RightParen Right Parenthesis key ‘)’.
Asterisk Asterisk key ‘*’.
Plus Plus key ‘+’.
Comma Comma ‘,’ key.
Minus Minus ‘-‘ key.
Period Period ‘.’ key.
Slash Slash ‘/’ key.
Colon Colon ‘:’ key.
Semicolon Semicolon ‘;’ key.
Less Less than ‘<' key.
Equals Equals '=' key.
Greater Greater than '>‘ key.
Question Question mark ‘?’ key.
At At key ‘@’.
LeftBracket Left square bracket key ‘[‘.
Backslash Backslash key ‘\’.
RightBracket Right square bracket key ‘]’.
Caret Caret key ‘^’.
Underscore Underscore ‘_’ key.
BackQuote Back 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.
Numlock Numlock key.
CapsLock Capslock key.
ScrollLock Scroll lock key.
RightShift Right shift key.
LeftShift Left shift key.
RightControl Right Control key.
LeftControl Left Control key.
RightAlt Right Alt key.
LeftAlt Left Alt key.
LeftCommand Left Command key.
LeftApple Left Command key.
LeftWindows Left Windows key.
RightCommand Right Command key.
RightApple Right Command key.
RightWindows Right Windows key.
AltGr Alt Gr key.
Help Help key.
Print Print key.
SysReq Sys Req key.
Break Break key.
Menu Menu key.
Mouse0 First (primary) mouse button.
Mouse1 Second (secondary) mouse button.
Mouse2 Third mouse button.
Mouse3 Fourth mouse button.
Mouse4 Fifth mouse button.
Mouse5 Sixth mouse button.
Mouse6 Seventh mouse button.
JoystickButton0 Button 0 on any joystick.
JoystickButton1 Button 1 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.
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.
Joystick1Button0 Button 0 on first joystick.
Joystick1Button1 Button 1 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.
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.
Joystick2Button0 Button 0 on second joystick.
Joystick2Button1 Button 1 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.
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.
Joystick3Button0 Button 0 on third joystick.
Joystick3Button1 Button 1 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.
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.
Joystick4Button0 Button 0 on forth joystick.
Joystick4Button1 Button 1 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.
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.
AUTO FIRE SAMPLE – RILEVA LA PRESSIONE PROLUNGATA, RITORNA ‘TRUE’ DURANTE TUTTA LA PRESSIONE
Returns true while the virtual button identified by buttonName is held down.
Think auto fire – this will return true as long as the button is held down.
Use this only when implementing action like events IE: shooting a weapon.
Statement: static function GetButton(buttonName: string): bool;
buttonName -> virtual Axes button names
Virtual Axes and buttons can be created in: MAIN TOP MENU> Edit> Project Settings> Input.
Example:
// Instantiates a projectile every 0.5 seconds, // if the Fire1 button (default is Ctrl) is pressed. var projectile : GameObject; var fireRate : float = 0.5; private var nextFire : float = 0.0; function Update () { if (Input.GetButton ("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; var clone : GameObject = Instantiate(projectile, transform.position, transform.rotation) as GameObject; }
REFERENCE
http://docs.unity3d.com/Documentation/ScriptReference/Input.html