module Eagle::Input

Overview

Keyboard, mouse and gamepad state you can check from anywhere, plus an action map so game code talks about "jump" instead of specific keys.

There are two ways to read input:

.down? is true every frame the input is held. .pressed? and .released? are true only on the frame it changed, so use them for one-shot actions like jumping or firing.

Actions bind any mix of keys, mouse buttons, gamepad buttons and stick directions to a name. Map them once in App#load, then ask about the action:

Input.map "left", Key::A, Key::Left, Input.axis(GamepadAxis::LeftX, -1)
Input.map "right", Key::D, Key::Right, Input.axis(GamepadAxis::LeftX, 1)
Input.map "up", Key::W, Key::Up, Input.axis(GamepadAxis::LeftY, -1)
Input.map "down", Key::S, Key::Down, Input.axis(GamepadAxis::LeftY, 1)
Input.map "jump", Key::Space, GamepadButton::A
Input.map "fire", MouseButton::Left, GamepadButton::RightShoulder

class Player < Node2D
  def process(dt : Float32) : Nil
    move = Input.vector("left", "right", "up", "down") # analog on sticks, digital on keys
    self.position += move * 200 * dt
    puts "jump!" if Input.pressed?("jump")
    look_at(Input.mouse) if Input.down?("fire")
  end
end

Defined in:

eagle/input/input.cr

Class Method Summary

Class Method Detail

def self.actions : Hash(String, Array(Binding)) #

Every action and its bindings, for building a controls menu.


def self.alt? : Bool #

True while either alt/option key is held.


def self.any_pressed? : Bool #

True if any key, mouse button or gamepad button went down this frame. Handy for "press any key".


def self.axis(a : GamepadAxis, sign : Int32 = 1, threshold : Number = 0.5) : AxisBinding #

--- actions --- Builds a stick-direction binding for .map. Input.axis(GamepadAxis::LeftX, -1) means "left stick pushed left".


def self.axis(negative : String, positive : String) : Float32 #

Combines two actions into one value from -1 to 1: positive minus negative.

steer = Input.axis("left", "right") # -1 for left, 1 for right

def self.bindings(action : String) : Array(Binding) #

The bindings for one action.


def self.ctrl? : Bool #

True while either control key is held.


def self.down?(k : Key) : Bool #

--- keyboard --- True every frame while the key is held.


def self.down?(action : String) : Bool #

True while any binding of the action is held.


def self.gamepad(index : Int32 = 0) : Gamepad | Nil #

The controller at index in connection order, or nil. Index 0 is the first player.


def self.gamepads : Array(Gamepad) #

--- gamepads --- Every connected controller.


def self.map(action : String, *bindings : Binding) : Nil #

Binds one or more inputs to an action name. Calling it again adds more bindings.

Input.map "pause", Key::Escape, Key::P, GamepadButton::Start

def self.mods : KeyMod #

Modifier keys currently held (shift, ctrl, alt, gui).


def self.mouse : Vec2 #

--- mouse --- Mouse position in window coordinates. With a Camera2D, convert it with Camera2D#screen_to_world.


def self.mouse_delta : Vec2 #

How far the mouse moved this frame. Works with Window.relative_mouse= for mouse-look.


def self.mouse_down?(b : MouseButton = MouseButton::Left) : Bool #

True while the mouse button is held.


def self.mouse_pressed?(b : MouseButton = MouseButton::Left) : Bool #

True on the frame the mouse button went down.


def self.mouse_released?(b : MouseButton = MouseButton::Left) : Bool #

True on the frame the mouse button went up.


def self.pressed?(k : Key) : Bool #

True only on the frame the key went down.


def self.pressed?(action : String) : Bool #

True on the frame any binding of the action went down.


def self.released?(k : Key) : Bool #

True only on the frame the key went up.


def self.released?(action : String) : Bool #

True on the frame any binding of the action went up.


def self.reset : Nil #

Clears everything: held keys and buttons, the mouse, known gamepads and all action mappings. Mainly for tests; call .map again afterwards.


def self.shift? : Bool #

True while either shift key is held.


def self.strength(action : String) : Float32 #

How strongly the action is held, from 0 to 1. Keys and buttons are 0 or 1, and sticks and triggers give values in between.


def self.text : String #

Text typed this frame, when text input is enabled.


def self.text_input=(v : Bool) #

Turns OS text input on or off. While on, typing produces TextEvents and fills .text. TextInput controls handle this for you.


def self.unmap(action : String) : Nil #

Removes an action and all its bindings, for example before rebinding controls.


def self.vector(left : String, right : String, up : String, down : String, normalize : Bool = true) : Vec2 #

Combines four actions into a direction vector. With normalize, diagonals aren't faster than straight lines.


def self.wheel : Vec2 #

Scroll amount this frame. wheel.y is positive when scrolling up.