Overview
The PSL1GHT SDK supports USB keyboards and mice through the io/kb.h and io/mouse.h APIs. You can read key presses, modifier keys, LED states, mouse movement, and button clicks.
Up to 7 keyboards and 127 mice can be connected simultaneously.
Keyboard Support
Initialization
Initialize the keyboard system:
#include <io/kb.h>
// Initialize with maximum keyboards (max: 7)
s32 ret = ioKbInit (MAX_KB_PORT_NUM);
if (ret != 0 ) {
printf ( "Failed to initialize keyboard: %d \n " , ret);
}
Query connected keyboards:
KbInfo kbinfo;
ioKbGetInfo ( & kbinfo );
printf ( "Max keyboards: %u \n " , kbinfo.max);
printf ( "Connected: %u \n " , kbinfo.connected);
for ( int i = 0 ; i < MAX_KEYBOARDS; i ++ ) {
if ( kbinfo . status [i]) {
printf ( "Keyboard %d connected \n " , i);
}
}
Maximum keyboards allowed
Number of currently connected keyboards
Bit 0: System intercept flag
Connection status array (0: disconnected, 1: connected)
Read key events in your main loop:
KbData kbdata;
s32 ret = ioKbRead ( 0 , & kbdata ); // Keyboard 0
if (ret == 0 && kbdata.nb_keycode > 0 ) {
// Process each keycode
for ( int j = 0 ; j < kbdata . nb_keycode ; j ++ ) {
u16 keycode = kbdata . keycode [j];
printf ( "Key: 0x %04X \n " , keycode);
// Check for specific keys
if (keycode == KB_RAWKEY_ENTER) {
printf ( "Enter pressed \n " );
}
if (keycode == KB_RAWKEY_ESCAPE) {
printf ( "Escape pressed \n " );
}
}
// Check modifier keys
if ( kbdata . mkey . l_ctrl ) printf ( "Left Ctrl held \n " );
if ( kbdata . mkey . l_shift ) printf ( "Left Shift held \n " );
if ( kbdata . mkey . l_alt ) printf ( "Left Alt held \n " );
if ( kbdata . mkey . l_win ) printf ( "Left Win held \n " );
// Check LED states
if ( kbdata . led . num_lock ) printf ( "Num Lock ON \n " );
if ( kbdata . led . caps_lock ) printf ( "Caps Lock ON \n " );
if ( kbdata . led . scroll_lock ) printf ( "Scroll Lock ON \n " );
}
KbData Structure
LED state (Num Lock, Caps Lock, Scroll Lock, etc.)
Modifier key state (Ctrl, Shift, Alt, Win)
Number of keycodes (0 = no data)
Modifier Keys
The KbMkey structure contains modifier key states:
typedef struct KbMkey {
union {
u32 mkeys;
struct {
u32 reserved : 24 ;
u32 r_win : 1 ; // Right Windows key
u32 r_alt : 1 ; // Right Alt
u32 r_shift : 1 ; // Right Shift
u32 r_ctrl : 1 ; // Right Ctrl
u32 l_win : 1 ; // Left Windows key
u32 l_alt : 1 ; // Left Alt
u32 l_shift : 1 ; // Left Shift
u32 l_ctrl : 1 ; // Left Ctrl (LSB)
};
};
} KbMkey;
LED Control
Control keyboard LEDs:
KbLed led_state;
led_state.leds = 0 ;
// Turn on Num Lock
led_state.num_lock = 1 ;
ioKbSetLEDStatus ( 0 , led_state);
// Turn on Caps Lock
led_state.leds = 0 ;
led_state.caps_lock = 1 ;
ioKbSetLEDStatus ( 0 , led_state);
// Turn on Scroll Lock
led_state.leds = 0 ;
led_state.scroll_lock = 1 ;
ioKbSetLEDStatus ( 0 , led_state);
// Turn all LEDs on
led_state.leds = 0x FFFFFFFF ;
led_state.reserved = 0 ;
ioKbSetLEDStatus ( 0 , led_state);
// Turn all LEDs off
led_state.leds = 0 ;
ioKbSetLEDStatus ( 0 , led_state);
Keyboard Configuration
Get and set keyboard mode:
KbConfig kbconfig;
ioKbGetConfiguration ( 0 , & kbconfig );
printf ( "Mapping: %d \n " , kbconfig.mapping);
printf ( "Read mode: %d \n " , kbconfig.rmode);
printf ( "Code type: %d \n " , kbconfig.codetype);
// Set read mode
ioKbSetReadMode ( 0 , KB_RMODE_INPUTCHAR);
// Set code type to ASCII
ioKbSetCodeType ( 0 , KB_CODETYPE_ASCII);
Read Modes
KB_RMODE_INPUTCHAR Character input mode (default)
KB_RMODE_PACKET Packet mode for raw data
Code Types
KB_CODETYPE_RAW Raw device codes
KB_CODETYPE_ASCII ASCII character codes
Key Code Conversion
Convert raw keycodes to ASCII:
KbMapping mapping = KB_MAPPING_101; // US keyboard
KbMkey mkey;
KbLed led;
u16 rawcode = KB_RAWKEY_A;
mkey.mkeys = kbdata.mkey.mkeys;
led.leds = kbdata.led.leds;
u16 ascii = ioKbCnvRawCode (mapping, mkey, led, rawcode);
printf ( "ASCII: %c (0x %04X ) \n " , ascii & 0x FF , ascii);
Keyboard Mappings
Supported keyboard layouts:
Constant Layout KB_MAPPING_101US (101-key) KB_MAPPING_106Japanese (106-key) KB_MAPPING_GERMAN_GERMANYGerman KB_MAPPING_FRENCH_FRANCEFrench KB_MAPPING_ENGLISH_UKUK English KB_MAPPING_SPANISH_SPAINSpanish KB_MAPPING_ITALIAN_ITALYItalian KB_MAPPING_DUTCH_NETHERLANDSDutch KB_MAPPING_RUSSIAN_RUSSIARussian KB_MAPPING_KOREAN_KOREAKorean
Common Raw Key Codes
Constant Key Code KB_RAWKEY_ENTEREnter 0x28 KB_RAWKEY_ESCAPEEscape 0x29 KB_RAWKEY_BSBackspace 0x2A KB_RAWKEY_TABTab 0x2B KB_RAWKEY_SPACESpace 0x2C KB_RAWKEY_A - KB_RAWKEY_ZLetters 0x04-0x1D KB_RAWKEY_0 - KB_RAWKEY_9Numbers 0x27, 0x1E-0x26 KB_RAWKEY_F1 - KB_RAWKEY_F12Function keys 0x3A-0x45 KB_RAWKEY_UP_ARROWUp arrow 0x52 KB_RAWKEY_DOWN_ARROWDown arrow 0x51 KB_RAWKEY_LEFT_ARROWLeft arrow 0x50 KB_RAWKEY_RIGHT_ARROWRight arrow 0x4F
Clear Keyboard Buffer
Cleanup
Mouse Support
Initialization
Initialize the mouse system:
#include <io/mouse.h>
// Initialize with maximum mice (max: 127)
s32 ret = ioMouseInit (MAX_MICE);
if (ret != 0 ) {
printf ( "Failed to initialize mouse: %d \n " , ret);
}
Query connected mice:
mouseInfo info;
ioMouseGetInfo ( & info );
printf ( "Max mice: %u \n " , info.max);
printf ( "Connected: %u \n " , info.connected);
for ( int i = 0 ; i < MAX_MICE; i ++ ) {
if ( info . status [i]) {
printf ( "Mouse %d connected \n " , i);
printf ( " Vendor ID: %04x \n " , info . vendor_id [i]);
printf ( " Product ID: %04x \n " , info . product_id [i]);
}
}
Read mouse movement and button states:
mouseData data;
s32 ret = ioMouseGetData ( 0 , & data ); // Mouse 0
if (ret == 0 && data.update) {
// Mouse movement (signed 8-bit delta)
s8 x_delta = data . x_axis ;
s8 y_delta = data . y_axis ;
s8 wheel = data . wheel ;
s8 tilt = data . tilt ;
printf ( "Mouse: dx= %d dy= %d wheel= %d tilt= %d \n " ,
x_delta, y_delta, wheel, tilt);
// Button states (bit flags)
u8 buttons = data . buttons ;
if (buttons & 0x 01 ) printf ( "Left button \n " );
if (buttons & 0x 02 ) printf ( "Right button \n " );
if (buttons & 0x 04 ) printf ( "Middle button \n " );
}
mouseData Structure
Update flag (non-zero if data changed)
Horizontal movement delta (-127 to +127)
Vertical movement delta (-127 to +127)
Tilt wheel delta (for mice with tilt)
Bit Button 0x01 Left button 0x02 Right button 0x04 Middle button 0x08 Button 4 0x10 Button 5
Mouse Data List
Get multiple mouse events at once:
mouseDataList dataList;
ioMouseGetDataList ( 0 , & dataList );
printf ( "Event count: %u \n " , dataList.count);
for ( int i = 0 ; i < dataList.count; i ++ ) {
mouseData * event = & dataList . list [i];
printf ( "Event %d : dx= %d dy= %d buttons=0x %02x \n " ,
i, event -> x_axis , event -> y_axis , event -> buttons );
}
Clear Mouse Buffer
Cleanup
Complete Example
From samples/input/kbtest/:
#include <io/kb.h>
#include <io/pad.h>
#include <stdio.h>
#include <unistd.h>
int main () {
KbInfo kbinfo;
KbData kbdata;
padInfo2 padinfo2;
padData paddata;
int running = 1 ;
// Initialize
ioPadInit (MAX_PORT_NUM);
ioKbInit (MAX_KB_PORT_NUM);
// Set keyboard mode
ioKbGetInfo ( & kbinfo);
for ( int i = 0 ; i < MAX_KEYBOARDS; i ++ ) {
if ( kbinfo . status [i]) {
ioKbSetReadMode (i, KB_RMODE_INPUTCHAR);
ioKbSetCodeType (i, KB_CODETYPE_ASCII);
}
}
// Main loop - read keyboard until pad X button
while (running) {
// Check for exit (pad X button)
if ( ioPadGetInfo2 ( & padinfo2) == 0 ) {
for ( int i = 0 ; i < MAX_PORT_NUM; i ++ ) {
if ( padinfo2 . port_status [i]) {
ioPadGetData (i, & paddata);
if ( paddata . BTN_CROSS ) {
printf ( "Exit requested \n " );
running = 0 ;
}
}
}
}
// Read keyboard input
if ( ioKbGetInfo ( & kbinfo) == 0 ) {
for ( int i = 0 ; i < MAX_KEYBOARDS; i ++ ) {
if ( kbinfo . status [i]) {
if ( ioKbRead (i, & kbdata) == 0 ) {
for ( int j = 0 ; j < kbdata . nb_keycode ; j ++ ) {
u16 keycode = kbdata . keycode [j];
KbMkey mkey;
KbLed led;
mkey . mkeys = kbdata . mkey . mkeys ;
led . leds = kbdata . led . leds ;
if (keycode != 0 ) {
printf ( "Key= %c (0x %04X ) "
"mkeys=0x %08X leds=0x %08X \n " ,
keycode & 0x FF , keycode,
mkey . mkeys , led . leds );
}
}
}
}
}
}
usleep ( 20000 );
}
// Cleanup
ioPadEnd ();
ioKbEnd ();
return 0 ;
}
API Reference
Keyboard Functions
ioKbInit
s32 ioKbInit(const u32 max)
Initialize keyboard library
ioKbGetInfo
s32 ioKbGetInfo(KbInfo* kb_info)
Get keyboard connection information
ioKbRead
s32 ioKbRead(const u32 kb_no, KbData* data)
Read keyboard data
ioKbSetReadMode
s32 ioKbSetReadMode(const u32 kb_no, const KbRmode kb_rmode)
Set keyboard read mode
ioKbSetCodeType
s32 ioKbSetCodeType(const u32 kb_no, const KbCodeType kb_codetype)
Set keyboard code type
ioKbSetLEDStatus
s32 ioKbSetLEDStatus(const u32 kb_no, const KbLed led_state)
Control keyboard LEDs
ioKbCnvRawCode
u16 ioKbCnvRawCode(const KbMapping mapping, const KbMkey mkey, const KbLed led, const u16 rawcode)
Convert raw keycode to character
ioKbGetConfiguration
s32 ioKbGetConfiguration(const u32 kb_no, KbConfig* kb_config)
Get keyboard configuration
ioKbClearBuf
s32 ioKbClearBuf(const u32 kb_no)
Clear keyboard buffer
Mouse Functions
ioMouseGetInfo
s32 ioMouseGetInfo(mouseInfo* info)
Get mouse connection information
ioMouseGetData
s32 ioMouseGetData(u32 port, mouseData* data)
Get mouse data
ioMouseGetDataList
s32 ioMouseGetDataList(u32 port, mouseDataList* dataList)
Get list of mouse events
ioMouseClearBuf
s32 ioMouseClearBuf(u32 port)
Clear mouse buffer
See Also