Godot 3 Tutorial–Keyboard, Mouse and Joystick Input

This video will teach you how to handle input in Godot 3 including keyboard, mouse, and joystick/gamepad devices.  We will look at handling input via both polling and event driven, as well as creating an input map to handle input actions in a generic manner.

The Video

Assets and Code Samples

PolledInput

extends Sprite2D

#This example shows input polling, both keyboard and mouse
func _process(delta):
  
  #Handle arrow key presses
  if(Input.is_key_pressed(KEY_LEFT)):
    self.position.x-= 1
  if(Input.is_key_pressed(KEY_RIGHT)):
    self.position.x+= 1 
  if(Input.is_key_pressed(KEY_UP)):
    if(Input.is_key_pressed(KEY_SHIFT)):
      self.position.y-= 10
    else:
      self.position.y-= 1

  #Holding SHIFT moves by 10x up and down 
  if(Input.is_key_pressed(KEY_DOWN)):
    if(Input.is_key_pressed(KEY_SHIFT)):
      self.position.y+= 10
    else:
      self.position.y+= 1

  #Quit on Q press      
  if(Input.is_key_pressed(KEY_Q)):
    get_tree().quit()
    
  #Check for mouse movement
  if(Input.is_mouse_button_pressed(BUTTON_LEFT)):
    self.position = get_viewport().get_mouse_position()   

EventDriven

extends Sprite

var justwarped = false

func _input(event):
  if event is InputEventMouseMotion:
    if !justwarped:
      self.translate(event.relative)
    else:
      justwarped = false
  if event is InputEventMouseButton:
    match event.button_index:
      BUTTON_LEFT:
        Input.warp_mouse_position(self.position)
        justwarped = true
      BUTTON_RIGHT:
        self.position = Vector2(get_viewport().size.x/2, get_viewport().size.y/2)
  if (event is InputEventKey):
    print("Key pressed: " + char(event.scancode)) 
    if(event.echo == true):
      print("Key held down")
    else:
      print("First press")

Gamepad

extends Sprite

var startingPos = Vector2(self.position)
# Dead zone is the area around the center of the control when it is at rest
var deadZone = 0.2

# Speed Multiplier is what you would use to implement a sensitivity setting.  Higher == more movement per press
var speedMultiplier = 3

func _ready():
  # Register event to monitor if joystick connected or disconnected
  Input.connect("joy_connection_changed",self,"joy_con_changed")

func _process(delta):
  #Make sure at least one Joystick is connected
  if Input.get_connected_joypads().size() > 0:
    var xAxis = Input.get_joy_axis(0,JOY_AXIS_7)
    if abs(xAxis) > deadZone:
      
      # xAxis is a value from +/0 0-1 depending on how hard the stick is being pressed
      if xAxis < 0:
        self.position.x-= 100 * delta * ( speedMultiplier * abs(xAxis))
      if xAxis > 0:
        self.position.x+= 100 * delta * ( speedMultiplier * abs(xAxis))
  
    if Input.is_joy_button_pressed(0,JOY_BUTTON_0): # Same as JOY_XBOX_A
        self.position = startingPos   
    
    # Buttons have different meanings on different devices
    # Let's loop through and see what they are defined as
    for i in range(16):
      if Input.is_joy_button_pressed(0,i):
        print("Button at " + str(i) + " pressed, should be button: " + Input.get_joy_button_string(i))
    
#
    if Input.is_joy_button_pressed(0,JOY_DPAD_UP):
      position.y -= 10
      
func joy_con_changed(deviceid,isConnected):
  if isConnected:
    print("Joystick " + str(deviceid) + " connected")
    if Input.is_joy_known(0):
      print("Recognized and compatible joystick")
      print(Input.get_joy_name(0) + " device connected")
  else:
    print("Joystick " + str(deviceid) + " disconnected")

Vibrate (Missed in video… sorry)

extends Node

func _process(delta):
  if Input.is_joy_button_pressed(0,JOY_BUTTON_0):
    # Full weak and strong motor vibrate, vibrate 2 second
    Input.start_joy_vibration(0,1.0,1.0,2)  
  if Input.is_joy_button_pressed(0,JOY_BUTTON_1):
    # Half strength both motors
    Input.start_joy_vibration(0,0.5,0.5,2)
  if Input.is_joy_button_pressed(0,JOY_BUTTON_2):
    # Full strength strong motor, nothing to weak
    Input.start_joy_vibration(0,0.0,1.0,2)
  if Input.is_joy_button_pressed(0,JOY_BUTTON_3):
    # Full strength weak, none to strong
    Input.start_joy_vibration(0,1.0,0.0,2)
    
    
  # Now output whats currently happening in vibration land
  print("Duration " + str(Input.get_joy_vibration_duration(0)))
  print("Strength weak " + str(Input.get_joy_vibration_strength(0).x))
  print("Strength strong " + str(Input.get_joy_vibration_strength(0).y))

InputMap

extends Sprite

func _input(event):
  if(event.is_action("LEFT")):
    self.position.x-= 1
  if(event.is_action("RIGHT")):
    self.position.x+= 1
  if(event.is_action_pressed("UP")):
    self.position.y-= 1
  if(event.is_action_released("DOWN")):
    self.position.y+= 1
    

Back to Tutorial Series Homepage

Scroll to Top