OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
Controller Input

Reads the SNES controller and displays which button is currently held. The simplest possible input example — press a button, see its name on screen.

Screenshot

What You'll Learn

Controls

Button Display
A "A PRESSED"
B "B PRESSED"
X / Y "X PRESSED" / "Y PRESSED"
L / R "L PRESSED" / "R PRESSED"
D-pad "UP/DOWN/LEFT/RIGHT PRESSED"
START / SELECT "START/SELECT PRESSED"
None (blank)

SNES Concepts

How Input Works on the SNES

The SNES joypad is a shift register — the hardware clocks out 16 bits of button state every frame during the auto-joypad read period (after VBlank). The NMI handler in crt0.asm captures this automatically into pad_keys[]. Your code never touches hardware registers directly.

padHeld vs padPressed

u16 held = padHeld(0); // Buttons held RIGHT NOW (every frame)
u16 pressed = padPressed(0); // Buttons pressed THIS FRAME ONLY (edge detection)
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
u16 padHeld(u8 pad)
Get buttons currently held.
u16 padPressed(u8 pad)
Get buttons pressed this frame.

Use padHeld for continuous actions (movement, charging). Use padPressed for one-shot actions (jump, menu select, pause toggle).

Button Bitmask

Multiple buttons can be held simultaneously. Test with bitwise AND:

if (pad & KEY_A) { /* A is held */ }
if ((pad & KEY_L) && (pad & KEY_R)) { /* both shoulders held */ }
#define KEY_A
Definition input.h:81
#define KEY_L
Definition input.h:83
#define KEY_R
Definition input.h:84

Modules Used

Module Why it's here
console consoleInit(), WaitForVBlank(), NMI handler setup
sprite OAM buffer (required by NMI handler even with no visible sprites)
dma DMA transfers used internally by console init
background BG layer configuration
text textInit(), textPrintAt(), textFlush() for button name display
input padHeld(), padPressed() for reading controller state

Build & Run

cd $OPENSNES_HOME
make -C examples/input/controller

Open controller.sfc in Mesen2 and press buttons.