libnds
Data Structures | Typedefs | Enumerations | Functions
keyboard.h File Reference

Integration of stdin with a simple keyboard. More...

#include <nds/arm9/background.h>
#include <nds/ndstypes.h>

Data Structures

struct  Keyboard
 Describes a keyboard. More...
 
struct  KeyMap
 Defines a key mapping. More...
 

Typedefs

typedef struct Keyboard Keyboard
 Describes a keyboard.
 
typedef void(* KeyChangeCallback) (int key)
 Callback function pointer when a key changes.
 
typedef struct KeyMap KeyMap
 Defines a key mapping.
 

Enumerations

enum  KeyboardState {
  Lower = 0 ,
  Upper = 1 ,
  Numeric = 2 ,
  Reduced = 3
}
 States the keyboard can be in, currently only Lower and Upper supported. More...
 
enum  Keys {
  NOKEY = -1 ,
  DVK_FOLD = -23 ,
  DVK_TAB = 9 ,
  DVK_BACKSPACE = 8 ,
  DVK_CAPS = -15 ,
  DVK_SHIFT = -14 ,
  DVK_SPACE = 32 ,
  DVK_MENU = -5 ,
  DVK_ENTER = 10 ,
  DVK_CTRL = -16 ,
  DVK_UP = -17 ,
  DVK_RIGHT = -18 ,
  DVK_DOWN = -19 ,
  DVK_LEFT = -20 ,
  DVK_ALT = -26
}
 Enum values for the keyboard control keys. More...
 

Functions

KeyboardkeyboardDemoInit (void)
 Initializes the default keyboard of libnds. More...
 
void keyboardExit (void)
 De-initializes the keyboard system, if initialized. More...
 
s16 keyboardGetChar (void)
 Waits for user to press a key and returns the key pressed. More...
 
KeyboardkeyboardGetDefault (void)
 Gets the default keyboard. More...
 
s16 keyboardGetKey (int x, int y)
 Returns the ASCII code for the key located at the supplied x and y. More...
 
void keyboardGetString (char *buffer, int maxLen)
 Reads the input until a the return key is pressed or the maxLen is exceeded. More...
 
void keyboardHide (void)
 Hides the keyboard. More...
 
static KeyboardkeyboardInit (Keyboard *keyboard, int layer, BgType type, BgSize size, int mapBase, int tileBase, bool mainDisplay, bool loadGraphics)
 Initializes the keyboard system with the supplied keyboard. More...
 
void keyboardShow (void)
 Displays the keyboard.
 
s16 keyboardUpdate (void)
 Processes the keyboard. More...
 

Detailed Description

Integration of stdin with a simple keyboard.

The keyboard component allows the use of a default keyboard via stdin as well as direct via the functions exposed below. The default behavior is a hidden keyboard that shows on a call to scanf(stdin, ...).

By default the keyboard uses background 3 of the sub display, consumes approximatly 40 KiB of background VRAM begining at tile base 0 and 4 KiB of map stored at map base 20. The default is designed to work alongside an instance of the demo console.

To customize keyboard behavior and resource usage modify the keyboard structure returned by keyboardGetDefault() or create your own keyboard.

Enumeration Type Documentation

◆ KeyboardState

States the keyboard can be in, currently only Lower and Upper supported.

Enumerator
Lower 

Normal keyboard display (lowercase letters)

Upper 

Caps lock held.

Numeric 

Numeric only keypad (not provided by the default keyboard)

Reduced 

Reduced footprint keyboard (not provided by the default keyboard)

◆ Keys

enum Keys

Enum values for the keyboard control keys.

Negative values are keys with no sensible ASCII representation. Numbers are chosen to mimic ASCII control sequences.

Enumerator
NOKEY 

No key was pressed.

DVK_FOLD 

Fold key (top left on the default keyboard)

DVK_TAB 

Tab key.

DVK_BACKSPACE 

Backspace.

DVK_CAPS 

Caps key.

DVK_SHIFT 

Shift key.

DVK_SPACE 

Space key.

DVK_MENU 

Menu key.

DVK_ENTER 

Enter key.

DVK_CTRL 

Ctrl key.

DVK_UP 

Up key.

DVK_RIGHT 

Right key.

DVK_DOWN 

Down key.

DVK_LEFT 

Left key.

DVK_ALT 

Alt key.

Function Documentation

◆ keyboardDemoInit()

Keyboard * keyboardDemoInit ( void  )

Initializes the default keyboard of libnds.

Same as calling:

keyboardInit(NULL, 3, BgType_Text4bpp, BgSize_T_256x512, 20, 0, false, true)
@ BgType_Text4bpp
4bpp Tiled background with 16 bit tile indexes and no allowed rotation or scaling
Definition: background.h:453
@ BgSize_T_256x512
256 x 512 pixel text background
Definition: background.h:475
static Keyboard * keyboardInit(Keyboard *keyboard, int layer, BgType type, BgSize size, int mapBase, int tileBase, bool mainDisplay, bool loadGraphics)
Initializes the keyboard system with the supplied keyboard.
Definition: keyboard.h:127
Returns
A pointer to the current keyboard.

◆ keyboardExit()

void keyboardExit ( void  )

De-initializes the keyboard system, if initialized.

After calling this, one may de-allocate any custom keyboard struct used.

◆ keyboardGetChar()

s16 keyboardGetChar ( void  )

Waits for user to press a key and returns the key pressed.

Use keyboardUpdate instead for async operation.

Remember to call scanKeys() every frame if you use keyboardGetChar().

Returns
The key pressed.

◆ keyboardGetDefault()

Keyboard * keyboardGetDefault ( void  )

Gets the default keyboard.

Returns
Returns the default keyboard.

◆ keyboardGetKey()

s16 keyboardGetKey ( int  x,
int  y 
)

Returns the ASCII code for the key located at the supplied x and y.

Will not effect keyboard shift state.

Parameters
xThe pixel x location.
yThe pixel y location.
Returns
The key pressed or NOKEY if user pressed outside the keypad.

◆ keyboardGetString()

void keyboardGetString ( char *  buffer,
int  maxLen 
)

Reads the input until a the return key is pressed or the maxLen is exceeded.

Parameters
buffera buffer to hold the input string
maxLenthe maximum length to read

◆ keyboardHide()

void keyboardHide ( void  )

Hides the keyboard.

If scrollSpeed has been set to a non-zero value it will scroll it out of the screen. If not, it will hide it right away.

◆ keyboardInit()

static Keyboard * keyboardInit ( Keyboard keyboard,
int  layer,
BgType  type,
BgSize  size,
int  mapBase,
int  tileBase,
bool  mainDisplay,
bool  loadGraphics 
)
inlinestatic

Initializes the keyboard system with the supplied keyboard.

Parameters
keyboardThe keyboard struct to initialize (can be NULL).
layerThe background layer to use.
typeThe background type to initialize.
sizeThe background size to initialize.
mapBaseThe map base to use for the background.
tileBaseThe graphics tile base to use for the background.
mainDisplayIf true the keyboard will render on the main display.
loadGraphicsIf true the keyboard graphics will be loaded.
Returns
Returns the initialized keyboard struct.

◆ keyboardUpdate()

s16 keyboardUpdate ( void  )

Processes the keyboard.

Should be called once per frame when using the keyboard in an async manner.

Remember to call scanKeys() every frame if you use keyboardUpdate().

Returns
the ASCII code of the key pressed or NOKEY if no key was pressed.