Scripting
Beginner

GetButton and GetKey

Checked with version: 4.0

-

Difficulty: Beginner

-

Date: 13 Mar 2014

How to get button or key for input and how these axes behave / can be modified with the Input manager

GetButton and GetKey

Beginner Scripting

KeyInput

Code snippet

using UnityEngine;
using System.Collections;

public class KeyInput : MonoBehaviour
{
    public GUITexture graphic;
    public Texture2D standard;
    public Texture2D downgfx;
    public Texture2D upgfx;
    public Texture2D heldgfx;
    
    void Start()
    {
        graphic.texture = standard;
    }
    
    void Update ()
    {
        bool down = Input.GetKeyDown(KeyCode.Space);
        bool held = Input.GetKey(KeyCode.Space);
        bool up = Input.GetKeyUp(KeyCode.Space);
        
        if(down)
        {
            graphic.texture = downgfx;
        }
        else if(held)
        {
            graphic.texture = heldgfx;
        }
        else if(up)
        {
            graphic.texture = upgfx;
        }
        else
        {
            graphic.texture = standard; 
        }
        
        guiText.text = " " + down + "\n " + held + "\n " + up;
    }
}
#pragma strict

public var graphic : GUITexture;
public var standard : Texture2D;
public var downgfx : Texture2D;
public var upgfx : Texture2D;
public var heldgfx : Texture2D;

function Start()
{
    graphic.texture = standard;
}

function Update ()
{
    var down  = Input.GetKeyDown(KeyCode.Space);
    var held = Input.GetKey(KeyCode.Space);
    var up = Input.GetKeyUp(KeyCode.Space);
    
    if(down)
    {
        graphic.texture = downgfx;
    }
    else if(held)
    {
        graphic.texture = heldgfx;
    }
    else if(up)
    {
        graphic.texture = upgfx;
    }
    else
    {
        graphic.texture = standard;
    }

    guiText.text = " " + down + "\n " + held + "\n " + up;
}
import UnityEngine
import System.Collections


public class KeyInput(MonoBehaviour):

    public graphic as GUITexture

    public standard as Texture2D

    public downgfx as Texture2D

    public upgfx as Texture2D

    public heldgfx as Texture2D

    
    private def Start():
        graphic.texture = standard

    
    private def Update():
        down as bool = Input.GetKeyDown(KeyCode.Space)
        held as bool = Input.GetKey(KeyCode.Space)
        up as bool = Input.GetKeyUp(KeyCode.Space)
        
        if down:
            graphic.texture = downgfx
        elif held:
            graphic.texture = heldgfx
        elif up:
            graphic.texture = upgfx
        else:
            graphic.texture = standard
        
        guiText.text = (((((' ' + down) + '\n ') + held) + '\n ') + up)

ButtonInput

Code snippet

using UnityEngine;
using System.Collections;

public class ButtonInput : MonoBehaviour
{
    public GUITexture graphic;
    public Texture2D standard;
    public Texture2D downgfx;
    public Texture2D upgfx;
    public Texture2D heldgfx;
    
    void Start()
    {
        graphic.texture = standard;
    }
    
    void Update ()
    {
        bool down = Input.GetButtonDown("Jump");
        bool held = Input.GetButton("Jump");
        bool up = Input.GetButtonUp("Jump");
        
        if(down)
        {
            graphic.texture = downgfx;
        }
        else if(held)
        {
            graphic.texture = heldgfx;
        }
        else if(up)
        {
            graphic.texture = upgfx;
        }
        else
        {
            graphic.texture = standard;
        }
    
        guiText.text = " " + down + "\n " + held + "\n " + up;
    }
}
#pragma strict

public var graphic : GUITexture;
public var standard : Texture2D;
public var downgfx : Texture2D;
public var upgfx : Texture2D;
public var heldgfx : Texture2D;

function Start()
{
    graphic.texture = standard;
}

function Update ()
{
    var down  = Input.GetButtonDown("Jump");
    var held = Input.GetButton("Jump");
    var up = Input.GetButtonUp("Jump");
    
    if(down)
    {
        graphic.texture = downgfx;
    }
    else if(held)
    {
        graphic.texture = heldgfx;
    }
    else if(up)
    {
        graphic.texture = upgfx;
    }
    else
    {
        graphic.texture = standard;
    }

    guiText.text = " " + down + "\n " + held + "\n " + up;
}
import UnityEngine
import System.Collections


public class ButtonInput(MonoBehaviour):

    public graphic as GUITexture

    public standard as Texture2D

    public downgfx as Texture2D

    public upgfx as Texture2D

    public heldgfx as Texture2D

    
    private def Start():
        graphic.texture = standard

    
    private def Update():
        down as bool = Input.GetButtonDown('Jump')
        held as bool = Input.GetButton('Jump')
        up as bool = Input.GetButtonUp('Jump')
        
        if down:
            graphic.texture = downgfx
        elif held:
            graphic.texture = heldgfx
        elif up:
            graphic.texture = upgfx
        else:
            graphic.texture = standard
        
        guiText.text = (((((' ' + down) + '\n ') + held) + '\n ') + up)
GetAxis
GetComponent

Related documentation

Community resources