OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
input.h File Reference

SNES Controller Input. More...

#include <snes/types.h>

Go to the source code of this file.

Macros

#define KEY_A   BIT(7)
#define KEY_B   BIT(15)
#define KEY_DOWN   BIT(10)
#define KEY_DPAD   (KEY_UP | KEY_DOWN | KEY_LEFT | KEY_RIGHT)
 All D-pad directions.
#define KEY_FACE   (KEY_A | KEY_B | KEY_X | KEY_Y)
 All face buttons.
#define KEY_L   BIT(5)
#define KEY_LEFT   BIT(9)
#define KEY_R   BIT(4)
#define KEY_RIGHT   BIT(8)
#define KEY_SELECT   BIT(13)
#define KEY_START   BIT(12)
#define KEY_UP   BIT(11)
#define KEY_X   BIT(6)
#define KEY_Y   BIT(14)
#define MOUSE_BUTTON_LEFT   0x01
#define MOUSE_BUTTON_RIGHT   0x02
#define MOUSE_SENS_HIGH   2
#define MOUSE_SENS_LOW   0
#define MOUSE_SENS_MEDIUM   1
#define padHeld(pad)
#define padPressed(pad)
#define SSC_CURSOR   0x4000
#define SSC_FIRE   0x8000
#define SSC_NOISE   0x0100
#define SSC_OFFSCREEN   0x0200
#define SSC_PAUSE   0x1000
#define SSC_TURBO   0x2000

Functions

u8 mouseButtonsHeld (u8 port)
 Get currently held mouse buttons.
u8 mouseButtonsPressed (u8 port)
 Get newly pressed mouse buttons this frame.
u8 mouseGetSensitivity (u8 port)
 Get current mouse sensitivity.
s16 mouseGetX (u8 port)
 Get X displacement since last frame.
s16 mouseGetY (u8 port)
 Get Y displacement since last frame.
u8 mouseInit (u8 port)
 Initialize mouse on given port.
u8 mouseIsConnected (u8 port)
 Check if mouse is connected on port.
void mouseSetSensitivity (u8 port, u8 sensitivity)
 Set mouse sensitivity.
u16 padHeld (u8 pad)
 Get buttons currently held.
u8 padIsConnected (u8 pad)
 Check if controller is connected.
u16 padPressed (u8 pad)
 Get buttons pressed this frame.
u16 padReleased (u8 pad)
 Get buttons released this frame.
u16 scopeButtonsHeld (void)
 Buttons currently down — the same meaning as padHeld() and mouseButtonsHeld().
u16 scopeButtonsPressed (void)
 Get newly pressed buttons this frame.
u16 scopeButtonsRepeat (void)
 Buttons held past the hold delay, re-triggering every repeat delay (see scopeSetRepeatDelay()) — a keyboard-style auto-repeat.
void scopeCalibrate (void)
 Calibrate aim from a center-screen shot.
u16 scopeGetRawX (void)
 Get raw (uncalibrated) H position from PPU.
u16 scopeGetRawY (void)
 Get raw (uncalibrated) V position from PPU.
u16 scopeGetX (void)
 Get calibration-adjusted H position.
u16 scopeGetY (void)
 Get calibration-adjusted V position.
u8 scopeInit (void)
 Detect Super Scope on port 2.
u8 scopeIsConnected (void)
 Check if Super Scope is connected.
void scopeSetHoldDelay (u16 frames)
 Set hold delay (frames before hold triggers).
void scopeSetRepeatDelay (u16 frames)
 Set repeat delay (frames between repeat fires after hold).
u16 scopeSinceShot (void)
 Get frames since last detected shot.

Variables

u16 pad_keys [5]
u16 pad_keysdown [5]
u16 pad_keysold [5]

Detailed Description

SNES Controller Input.

Functions for reading controller input.

Button Constants

Use these constants with padPressed(), padHeld(), etc:

Constant Button
KEY_A A button
KEY_B B button
KEY_X X button
KEY_Y Y button
KEY_L L shoulder
KEY_R R shoulder
KEY_START Start
KEY_SELECT Select
KEY_UP D-pad up
KEY_DOWN D-pad down
KEY_LEFT D-pad left
KEY_RIGHT D-pad right

Usage

// Check for button press (just pressed this frame)
if (padPressed(0) & KEY_A) {
playerJump();
}
// Check for button held (continuously pressed)
if (padHeld(0) & KEY_RIGHT) {
playerMoveRight();
}
#define KEY_RIGHT
Definition input.h:81
#define KEY_A
Definition input.h:84
#define padHeld(pad)
Definition input.h:158
#define padPressed(pad)
Definition input.h:159
Author
OpenSNES Team

Macro Definition Documentation

◆ padHeld

#define padHeld ( pad)
Value:
(pad_keys[(pad)])
u16 pad_keys[5]

◆ padPressed

#define padPressed ( pad)
Value:
(pad_keysdown[(pad)])
u16 pad_keysdown[5]

Function Documentation

◆ padHeld()

u16 padHeld ( u8 pad)

Get buttons currently held.

Returns all buttons currently being pressed.

Parameters
padController number: 0 or 1. Indices 2-4 are accepted and always read 0 — the multitap path cannot be armed (KNOWN_LIMITATIONS.md)
Returns
Button mask of held buttons
if (padHeld(0) & KEY_RIGHT) {
}
static s16 player_x
Player X position in screen coordinates.
Definition main.c:56
See also
Measured frame costs — what this call costs per frame in five real scenes.

◆ padIsConnected()

u8 padIsConnected ( u8 pad)

Check if controller is connected.

Parameters
padController number: 0 or 1. Indices 2-4 are accepted and always read 0 — the multitap path cannot be armed (KNOWN_LIMITATIONS.md)
Returns
1 if a standard pad answered on that port during the last VBlank, 0 otherwise (0xFF for "yes" until 2026-09-22)

The NMI handler reads one serial bit past the 16 of auto-joypad reading: a joypad returns 1s after its 16 bits (anomie's register doc, Joypads), which is how the snesdev-wiki Multitap page detects controllers. A port holding another device (mouse, Super Scope) reads 0: it is not a pad. Valid from the first VBlank after consoleInit() / setScreenOn().

Note
Until 2026-09-26 it answered 1 for an empty port: an empty port and an idle pad both auto-read $0000, and only the 17th bit tells them apart. What an empty port returns there is modelled as 0 by luna, ares and Mesen2; no reference states it and it is not yet measured on a console.
Warning
Reading the serial ports yourself: mask them first. REG_JOYA ($4016) and REG_JOYB ($4017) are whole-byte reads; only bits 0-1 are the port's data lines. On $4017 bits 2-4 are tied and always read 1, bits 5-7 are open bus; on $4016 bits 2-7 are open bus (fullsnes, "Unused bits in ports"; snesdev-wiki, JOYSER1: "D4-2 always 1"). So if (REG_JOYB) is true on every console and every emulator — test REG_JOYB & 1.

◆ padPressed()

u16 padPressed ( u8 pad)

Get buttons pressed this frame.

Returns buttons that were just pressed (not held from previous frame).

Parameters
padController number: 0 or 1. Indices 2-4 are accepted and always read 0 — the multitap path cannot be armed (KNOWN_LIMITATIONS.md)
Returns
Button mask of newly pressed buttons
if (padPressed(0) & KEY_A) {
// A was just pressed
}
See also
Measured frame costs — what this call costs per frame in five real scenes.

◆ padReleased()

u16 padReleased ( u8 pad)

Get buttons released this frame.

Returns buttons that were just released (held last frame, not now).

Parameters
padController number: 0 or 1. Indices 2-4 are accepted and always read 0 — the multitap path cannot be armed (KNOWN_LIMITATIONS.md)
Returns
Button mask of released buttons

Variable Documentation

◆ pad_keys

u16 pad_keys[5]
extern

Buttons down, per pad (written by the NMI handler)

◆ pad_keysdown

u16 pad_keysdown[5]
extern

Buttons that went down on this frame

◆ pad_keysold

u16 pad_keysold[5]
extern

Buttons down on the previous frame