input

This module allows you to read user input from the GBA’s keypad.

Example:

import natu/[video, irq, bios, input]

dispcnt.init()
irq.enable(iiVBlank)

while true:
  VBlankIntrWait()

  # Update key state (call this once per frame.)
  keyPoll()

  # Change backdrop color when the player presses "A".
  if keyIsDown(kiA):
    bgColorMem[0] = rgb5(15, 31, 20)

Types

type KeyIndex = enum

Bit positions for keyinput and keycnt. Used with input functions such as keyIsDown.

  • kiAA button
  • kiBB button
  • kiSelectSelect button
  • kiStartStart button
  • kiRightRight D-pad
  • kiLeftLeft D-pad
  • kiUpUp D-pad
  • kiDownDown D-pad
  • kiRRight shoulder button
  • kiLLeft shoulder button
type KeyState = set[KeyIndex]
type KeyInput = object

    Field    

    Type    

invertedState KeyState
type KeyIntrOp = enum
  • opOrRaise interrupt if any of the specified keys are pressed.
  • opAndRaise interrupt if all specified keys are pressed at the same time.
type KeyCnt = object

    Field    

    Type    

    Description    

keys KeyState

The set of keys that fire the keypad interrupt.

_ uint16
irq bool

Enables the keypad interrupt.

op KeyIntrOp

The condition under which the interrupt will be raised (opOr vs opAnd)


Globals

const allKeys: KeyState = {kiA..kiL}
var keyCurrState: KeyState

The set of keys that are currently down on this frame.

var keyPrevState: KeyState

The set of keys that were down on the previous frame.


Basic Procs

proc keyPoll()

Should be called once per frame to update the current key state.

proc keysDown(): KeyState

Get all the keys which are currently down.

proc keysUp(): KeyState

Get all the keys which are currently up.

proc keysHit(): KeyState

Get all the keys which were just pressed on this frame.

proc keysReleased(): KeyState

Get all the keys which were previously pressed but are no longer pressed.

proc keyIsDown(k: KeyIndex): bool

True if the given key is currently down.

proc keyIsUp(k: KeyIndex): bool

True if the given key is currently up.

proc keyWasDown(k: KeyIndex): bool

True if the given key was previously down.

proc keyWasUp(k: KeyIndex): bool

True if the given key was previously up.

proc keyHit(k: KeyIndex): bool

True if the given key was just pressed on this frame.

proc keyReleased(k: KeyIndex): bool

True if the given key was just released on this frame.

proc anyKeyHit(s: KeyState): bool

True if any of the given keys were just pressed on this frame.


Repeated Keys

proc keysRepeated(): KeyState

Get the keys that just repeated or were newly pressed.

proc keyRepeated(k: KeyIndex): bool

Check if a key just repeated or was newly pressed.

proc setKeyRepeatMask(mask: KeyState)

Set which keys will be considered for repeats.

proc setKeyRepeatDelay(delay: uint8)

Set the initial delay from when a key is first pressed to when it starts repeating.

If keys are already repeating, this will delay them again.

proc setKeyRepeatPeriod(period: uint8)

Set the interval between repeated keys.


I/O Registers

let keyinput: KeyInput

Keypad status register (read only).

This can be used to obtain the current state (up or down) of all the buttons on the GBA. Note that the state is inverted.

It is generally preferable to call keyPoll and use the various input procedures (keyIsDown etc.) rather than reading this directly.

var keycnt: KeyCnt

Key interrupt control register.

See the irq module for details.

proc state(keyinput: KeyInput): KeyState

Flip the keyinput register to obtain the set of keys which are currently pressed.