input

package
v0.0.0-...-4776544 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 24, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package input provides keyboard and mouse input handling with action mapping.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action int

Action represents a game action that can be bound to multiple keys.

const (
	ActionNone Action = iota

	// Movement
	ActionMoveUp
	ActionMoveDown
	ActionMoveLeft
	ActionMoveRight

	// Player actions
	ActionJump
	ActionAttack
	ActionInteract
	ActionPause

	// UI actions
	ActionConfirm
	ActionCancel
	ActionMenu
)

Common game actions (users can define their own).

type InputManager

type InputManager struct {
	// contains filtered or unexported fields
}

InputManager manages keyboard and mouse input state with action mapping.

func NewInputManager

func NewInputManager() *InputManager

NewInputManager creates a new input manager.

Returns:

*InputManager: New input manager with empty bindings

Example:

input := input.NewInputManager()
input.BindAction(input.ActionMoveUp, input.KeyW, input.KeyArrowUp)

func (*InputManager) ActionHeld

func (im *InputManager) ActionHeld(action Action) bool

ActionHeld returns true if action is currently being held.

Parameters:

action: Action to query

Returns:

bool: True if any bound key is currently down

Example:

if input.ActionHeld(input.ActionMoveRight) {
    player.Transform.Position.X += speed * dt
}

func (*InputManager) ActionPressed

func (im *InputManager) ActionPressed(action Action) bool

ActionPressed returns true if action was just pressed this frame.

Parameters:

action: Action to query

Returns:

bool: True if any bound key went from up to down this frame

Example:

if input.ActionPressed(input.ActionJump) {
    player.Jump()
}

func (*InputManager) ActionReleased

func (im *InputManager) ActionReleased(action Action) bool

ActionReleased returns true if action was just released this frame.

Parameters:

action: Action to query

Returns:

bool: True if any bound key went from down to up this frame

Example:

if input.ActionReleased(input.ActionAttack) {
    player.StopAttacking()
}

func (*InputManager) BindAction

func (im *InputManager) BindAction(action Action, keys ...KeyCode)

BindAction binds an action to one or more keys.

Parameters:

action: Action to bind
keys: One or more keys that trigger this action

Behavior:

  • Replaces existing bindings for this action
  • Multiple keys can trigger the same action

Example:

input.BindAction(input.ActionJump, input.KeySpace)
input.BindAction(input.ActionMoveRight, input.KeyD, input.KeyArrowRight)

func (*InputManager) KeyHeld

func (im *InputManager) KeyHeld(key KeyCode) bool

KeyHeld returns true if key is currently being held.

Parameters:

key: Key to query

Returns:

bool: True if key is currently down

func (*InputManager) KeyPressed

func (im *InputManager) KeyPressed(key KeyCode) bool

KeyPressed returns true if key was just pressed this frame.

Parameters:

key: Key to query

Returns:

bool: True if key went from up to down this frame

Example:

if input.KeyPressed(input.KeyEscape) {
    game.Pause()
}

func (*InputManager) KeyReleased

func (im *InputManager) KeyReleased(key KeyCode) bool

KeyReleased returns true if key was just released this frame.

Parameters:

key: Key to query

Returns:

bool: True if key went from down to up this frame

func (*InputManager) MouseDelta

func (im *InputManager) MouseDelta() (int32, int32)

MouseDelta returns mouse movement since last frame.

Returns:

dx, dy: Movement in pixels

Example:

dx, dy := input.MouseDelta()
camera.Position.X -= float64(dx) * sensitivity

func (*InputManager) MousePosition

func (im *InputManager) MousePosition() (int32, int32)

MousePosition returns the current mouse position.

Returns:

x, y: Screen coordinates

Example:

mouseX, mouseY := input.MousePosition()
worldX, worldY := camera.ScreenToWorld(mouseX, mouseY)

func (*InputManager) ProcessKeyEvent

func (im *InputManager) ProcessKeyEvent(event *sdl.KeyboardEvent)

ProcessKeyEvent updates key state from SDL event.

func (*InputManager) ProcessMouseButtonEvent

func (im *InputManager) ProcessMouseButtonEvent(event *sdl.MouseButtonEvent)

ProcessMouseButtonEvent updates mouse button state from SDL event.

func (*InputManager) ProcessMouseMotionEvent

func (im *InputManager) ProcessMouseMotionEvent(event *sdl.MouseMotionEvent)

ProcessMouseMotionEvent updates mouse position from SDL event.

func (*InputManager) Update

func (im *InputManager) Update()

Update swaps input buffers - call at end of frame.

Behavior:

  • Copies current state to previous state
  • Should be called by Engine after update/render
  • Do NOT call manually in game code

type KeyCode

type KeyCode int

KeyCode represents a keyboard key or mouse button.

const (
	// Letters
	KeyA KeyCode = KeyCode(sdl.SCANCODE_A)
	KeyB KeyCode = KeyCode(sdl.SCANCODE_B)
	KeyC KeyCode = KeyCode(sdl.SCANCODE_C)
	KeyD KeyCode = KeyCode(sdl.SCANCODE_D)
	KeyE KeyCode = KeyCode(sdl.SCANCODE_E)
	KeyF KeyCode = KeyCode(sdl.SCANCODE_F)
	KeyG KeyCode = KeyCode(sdl.SCANCODE_G)
	KeyH KeyCode = KeyCode(sdl.SCANCODE_H)
	KeyI KeyCode = KeyCode(sdl.SCANCODE_I)
	KeyJ KeyCode = KeyCode(sdl.SCANCODE_J)
	KeyK KeyCode = KeyCode(sdl.SCANCODE_K)
	KeyL KeyCode = KeyCode(sdl.SCANCODE_L)
	KeyM KeyCode = KeyCode(sdl.SCANCODE_M)
	KeyN KeyCode = KeyCode(sdl.SCANCODE_N)
	KeyO KeyCode = KeyCode(sdl.SCANCODE_O)
	KeyP KeyCode = KeyCode(sdl.SCANCODE_P)
	KeyQ KeyCode = KeyCode(sdl.SCANCODE_Q)
	KeyR KeyCode = KeyCode(sdl.SCANCODE_R)
	KeyS KeyCode = KeyCode(sdl.SCANCODE_S)
	KeyT KeyCode = KeyCode(sdl.SCANCODE_T)
	KeyU KeyCode = KeyCode(sdl.SCANCODE_U)
	KeyV KeyCode = KeyCode(sdl.SCANCODE_V)
	KeyW KeyCode = KeyCode(sdl.SCANCODE_W)
	KeyX KeyCode = KeyCode(sdl.SCANCODE_X)
	KeyY KeyCode = KeyCode(sdl.SCANCODE_Y)
	KeyZ KeyCode = KeyCode(sdl.SCANCODE_Z)

	// Numbers
	Key0 KeyCode = KeyCode(sdl.SCANCODE_0)
	Key1 KeyCode = KeyCode(sdl.SCANCODE_1)
	Key2 KeyCode = KeyCode(sdl.SCANCODE_2)
	Key3 KeyCode = KeyCode(sdl.SCANCODE_3)
	Key4 KeyCode = KeyCode(sdl.SCANCODE_4)
	Key5 KeyCode = KeyCode(sdl.SCANCODE_5)
	Key6 KeyCode = KeyCode(sdl.SCANCODE_6)
	Key7 KeyCode = KeyCode(sdl.SCANCODE_7)
	Key8 KeyCode = KeyCode(sdl.SCANCODE_8)
	Key9 KeyCode = KeyCode(sdl.SCANCODE_9)

	// Arrow keys
	KeyArrowUp    KeyCode = KeyCode(sdl.SCANCODE_UP)
	KeyArrowDown  KeyCode = KeyCode(sdl.SCANCODE_DOWN)
	KeyArrowLeft  KeyCode = KeyCode(sdl.SCANCODE_LEFT)
	KeyArrowRight KeyCode = KeyCode(sdl.SCANCODE_RIGHT)

	// Special keys
	KeySpace  KeyCode = KeyCode(sdl.SCANCODE_SPACE)
	KeyEnter  KeyCode = KeyCode(sdl.SCANCODE_RETURN)
	KeyEscape KeyCode = KeyCode(sdl.SCANCODE_ESCAPE)
	KeyTab    KeyCode = KeyCode(sdl.SCANCODE_TAB)
	KeyShift  KeyCode = KeyCode(sdl.SCANCODE_LSHIFT)
	KeyCtrl   KeyCode = KeyCode(sdl.SCANCODE_LCTRL)
	KeyAlt    KeyCode = KeyCode(sdl.SCANCODE_LALT)

	// Mouse buttons (using high values to avoid conflicts with keyboard scancodes).
	KeyMouseLeft   KeyCode = 1000
	KeyMouseRight  KeyCode = 1001
	KeyMouseMiddle KeyCode = 1002
)

Keyboard keys (wrapping SDL scancodes for type safety).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL