input/software-keyboard/source/main.c
#include <3ds.h>
#include <stdio.h>
#include <string.h>
static SwkbdCallbackResult MyCallback(void* user, const char** ppMessage, const char* text, size_t textlen)
{
if (strstr(text, "lenny"))
{
*ppMessage = "Nice try but I'm not letting you use that meme right now";
return SWKBD_CALLBACK_CONTINUE;
}
if (strstr(text, "brick"))
{
*ppMessage = "~Time to visit Brick City~";
return SWKBD_CALLBACK_CLOSE;
}
return SWKBD_CALLBACK_OK;
}
int main(int argc, char **argv)
{
gfxInitDefault();
consoleInit(GFX_TOP, NULL);
printf("Software keyboard demo - by fincs\n");
printf("A: text input\n");
printf("B: numpad input\n");
printf("X: western input\n");
printf("Y: filtered input + HOME/POWER/reset handling\n");
printf("START: exit\n");
while (aptMainLoop())
{
hidScanInput();
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break;
static SwkbdState swkbd;
static char mybuf[60];
static SwkbdStatusData swkbdStatus;
static SwkbdLearningData swkbdLearning;
SwkbdButton button = SWKBD_BUTTON_NONE;
bool didit = false;
if (kDown & KEY_A)
{
didit = true;
swkbdInit(&swkbd, SWKBD_TYPE_NORMAL, 3, -1);
swkbdSetInitialText(&swkbd, mybuf);
swkbdSetHintText(&swkbd, "Please enter dank memes");
swkbdSetButton(&swkbd, SWKBD_BUTTON_LEFT, "Maybe Not", false);
swkbdSetButton(&swkbd, SWKBD_BUTTON_MIDDLE, "~Middle~", true);
swkbdSetButton(&swkbd, SWKBD_BUTTON_RIGHT, "Do It", true);
swkbdSetFeatures(&swkbd, SWKBD_PREDICTIVE_INPUT);
SwkbdDictWord words[2];
swkbdSetDictWord(&words[0], "lenny", "( ͡° ͜ʖ ͡°)");
swkbdSetDictWord(&words[1], "shrug", "¯\\_(ツ)_/¯");
swkbdSetDictionary(&swkbd, words, sizeof(words)/sizeof(SwkbdDictWord));
static bool reload = false;
swkbdSetStatusData(&swkbd, &swkbdStatus, reload, true);
swkbdSetLearningData(&swkbd, &swkbdLearning, reload, true);
reload = true;
button = swkbdInputText(&swkbd, mybuf, sizeof(mybuf));
}
if (kDown & KEY_B)
{
didit = true;
swkbdInit(&swkbd, SWKBD_TYPE_NUMPAD, 1, 8);
swkbdSetPasswordMode(&swkbd, SWKBD_PASSWORD_HIDE_DELAY);
swkbdSetValidation(&swkbd, SWKBD_ANYTHING, 0, 0);
swkbdSetFeatures(&swkbd, SWKBD_FIXED_WIDTH);
swkbdSetNumpadKeys(&swkbd, L'ツ', L'益');
button = swkbdInputText(&swkbd, mybuf, sizeof(mybuf));
}
if (kDown & KEY_X)
{
didit = true;
swkbdInit(&swkbd, SWKBD_TYPE_WESTERN, 1, -1);
swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, SWKBD_FILTER_DIGITS | SWKBD_FILTER_AT | SWKBD_FILTER_PERCENT | SWKBD_FILTER_BACKSLASH | SWKBD_FILTER_PROFANITY, 2);
swkbdSetFeatures(&swkbd, SWKBD_MULTILINE);
swkbdSetHintText(&swkbd, "No Japanese text allowed ¯\\_(ツ)_/¯");
button = swkbdInputText(&swkbd, mybuf, sizeof(mybuf));
}
if (kDown & KEY_Y)
{
didit = true;
swkbdInit(&swkbd, SWKBD_TYPE_WESTERN, 2, -1);
swkbdSetValidation(&swkbd, SWKBD_NOTEMPTY_NOTBLANK, 0, 0);
swkbdSetFeatures(&swkbd, SWKBD_DARKEN_TOP_SCREEN | SWKBD_ALLOW_HOME | SWKBD_ALLOW_RESET | SWKBD_ALLOW_POWER);
swkbdSetFilterCallback(&swkbd, MyCallback, NULL);
bool shouldQuit = false;
mybuf[0] = 0;
do
{
swkbdSetInitialText(&swkbd, mybuf);
button = swkbdInputText(&swkbd, mybuf, sizeof(mybuf));
if (button != SWKBD_BUTTON_NONE)
break;
SwkbdResult res = swkbdGetResult(&swkbd);
if (res == SWKBD_RESETPRESSED)
{
shouldQuit = true;
aptSetChainloaderToSelf();
break;
}
else if (res != SWKBD_HOMEPRESSED && res != SWKBD_POWERPRESSED)
break; // An actual error happened, display it
shouldQuit = !aptMainLoop();
} while (!shouldQuit);
if (shouldQuit)
break;
}
if (didit)
{
if (button != SWKBD_BUTTON_NONE)
{
printf("You pressed button %d\n", button);
printf("Text: %s\n", mybuf);
} else
printf("swkbd event: %d\n", swkbdGetResult(&swkbd));
}
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
gspWaitForVBlank();
}
gfxExit();
return 0;
}
3ds.h
Central 3DS header.
aptSetChainloaderToSelf
void aptSetChainloaderToSelf(void)
Configures the chainloader to relaunch the current application (i.e. soft-reset)
aptMainLoop
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
consoleInit
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
gfxSwapBuffers
void gfxSwapBuffers(void)
Updates the configuration of both screens.
gfxInitDefault
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
GFX_TOP
@ GFX_TOP
Top screen.
Definition: gfx.h:26
gfxExit
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
gfxFlushBuffers
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
gspWaitForVBlank
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
KEY_B
@ KEY_B
B.
Definition: hid.h:13
KEY_X
@ KEY_X
X.
Definition: hid.h:22
KEY_Y
@ KEY_Y
Y.
Definition: hid.h:23
KEY_START
@ KEY_START
Start.
Definition: hid.h:15
KEY_A
@ KEY_A
A.
Definition: hid.h:12
hidKeysDown
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
hidScanInput
void hidScanInput(void)
Scans HID for input data.
SwkbdDictWord
Keyboard dictionary word for predictive input.
Definition: swkbd.h:114
SwkbdLearningData
Keyboard predictive input learning data.
Definition: swkbd.h:126
SwkbdState
Software keyboard parameter structure, it shouldn't be modified directly.
Definition: swkbd.h:141
SwkbdStatusData
Keyboard status data.
Definition: swkbd.h:124
swkbdSetButton
void swkbdSetButton(SwkbdState *swkbd, SwkbdButton button, const char *text, bool submit)
Configures a dialog button in a software keyboard.
SwkbdCallbackResult
SwkbdCallbackResult
Keyboard filter callback return values.
Definition: swkbd.h:73
SWKBD_CALLBACK_CLOSE
@ SWKBD_CALLBACK_CLOSE
Displays an error message, then closes the keyboard.
Definition: swkbd.h:75
SWKBD_CALLBACK_OK
@ SWKBD_CALLBACK_OK
Specifies that the input is valid.
Definition: swkbd.h:74
SWKBD_CALLBACK_CONTINUE
@ SWKBD_CALLBACK_CONTINUE
Displays an error message and continues displaying the keyboard.
Definition: swkbd.h:76
swkbdSetInitialText
void swkbdSetInitialText(SwkbdState *swkbd, const char *text)
Sets the initial text that a software keyboard will display on launch.
swkbdInputText
SwkbdButton swkbdInputText(SwkbdState *swkbd, char *buf, size_t bufsize)
Launches a software keyboard in order to input text.
swkbdSetFilterCallback
void swkbdSetFilterCallback(SwkbdState *swkbd, SwkbdCallbackFn callback, void *user)
Configures a custom function to be used to check the validity of input when it is submitted in a soft...
swkbdSetLearningData
void swkbdSetLearningData(SwkbdState *swkbd, SwkbdLearningData *data, bool in, bool out)
Configures software keyboard predictive input learning data management.
swkbdSetFeatures
void swkbdSetFeatures(SwkbdState *swkbd, u32 features)
Specifies which special features are enabled in a software keyboard.
swkbdSetDictWord
void swkbdSetDictWord(SwkbdDictWord *word, const char *reading, const char *text)
Configures a word in a predictive dictionary for use with a software keyboard.
SWKBD_PASSWORD_HIDE_DELAY
@ SWKBD_PASSWORD_HIDE_DELAY
Characters are concealed a second after they've been typed.
Definition: swkbd.h:43
swkbdSetPasswordMode
static void swkbdSetPasswordMode(SwkbdState *swkbd, SwkbdPasswordMode mode)
Configures password mode in a software keyboard.
Definition: swkbd.h:201
SwkbdButton
SwkbdButton
Keyboard dialog buttons.
Definition: swkbd.h:30
SWKBD_BUTTON_LEFT
@ SWKBD_BUTTON_LEFT
Left button (usually Cancel)
Definition: swkbd.h:31
SWKBD_BUTTON_RIGHT
@ SWKBD_BUTTON_RIGHT
Right button (usually OK)
Definition: swkbd.h:33
SWKBD_BUTTON_MIDDLE
@ SWKBD_BUTTON_MIDDLE
Middle button (usually I Forgot)
Definition: swkbd.h:32
SWKBD_BUTTON_NONE
@ SWKBD_BUTTON_NONE
No button (returned by swkbdInputText in special cases)
Definition: swkbd.h:35
swkbdSetStatusData
void swkbdSetStatusData(SwkbdState *swkbd, SwkbdStatusData *data, bool in, bool out)
Configures software keyboard internal status management.
swkbdSetValidation
static void swkbdSetValidation(SwkbdState *swkbd, SwkbdValidInput validInput, u32 filterFlags, int maxDigits)
Configures input validation in a software keyboard.
Definition: swkbd.h:213
SWKBD_FILTER_PROFANITY
@ SWKBD_FILTER_PROFANITY
Disallow profanity using Nintendo's profanity filter.
Definition: swkbd.h:53
SWKBD_FILTER_AT
@ SWKBD_FILTER_AT
Disallow the use of the @ sign.
Definition: swkbd.h:50
SWKBD_FILTER_BACKSLASH
@ SWKBD_FILTER_BACKSLASH
Disallow the use of the \ sign.
Definition: swkbd.h:52
SWKBD_FILTER_DIGITS
@ SWKBD_FILTER_DIGITS
Disallow the use of more than a certain number of digits (0 or more)
Definition: swkbd.h:49
SWKBD_FILTER_PERCENT
@ SWKBD_FILTER_PERCENT
Disallow the use of the % sign.
Definition: swkbd.h:51
SWKBD_NOTEMPTY_NOTBLANK
@ SWKBD_NOTEMPTY_NOTBLANK
Empty or blank inputs (consisting solely of whitespace) are not accepted.
Definition: swkbd.h:22
SWKBD_ANYTHING
@ SWKBD_ANYTHING
All inputs are accepted.
Definition: swkbd.h:20
swkbdGetResult
static SwkbdResult swkbdGetResult(SwkbdState *swkbd)
Retrieves the result condition of a software keyboard after it has been used.
Definition: swkbd.h:318
SwkbdResult
SwkbdResult
Keyboard return values.
Definition: swkbd.h:81
SWKBD_RESETPRESSED
@ SWKBD_RESETPRESSED
The soft-reset key combination was pressed.
Definition: swkbd.h:94
SWKBD_POWERPRESSED
@ SWKBD_POWERPRESSED
The POWER button was pressed.
Definition: swkbd.h:95
SWKBD_HOMEPRESSED
@ SWKBD_HOMEPRESSED
The HOME button was pressed.
Definition: swkbd.h:93
swkbdSetHintText
void swkbdSetHintText(SwkbdState *swkbd, const char *text)
Sets the hint text of a software keyboard (that is, the help text that is displayed when the textbox ...
SWKBD_ALLOW_POWER
@ SWKBD_ALLOW_POWER
Allow the usage of the POWER button.
Definition: swkbd.h:67
SWKBD_ALLOW_RESET
@ SWKBD_ALLOW_RESET
Allow the usage of a software-reset combination.
Definition: swkbd.h:66
SWKBD_ALLOW_HOME
@ SWKBD_ALLOW_HOME
Allow the usage of the HOME button.
Definition: swkbd.h:65
SWKBD_FIXED_WIDTH
@ SWKBD_FIXED_WIDTH
Enable fixed-width mode.
Definition: swkbd.h:64
SWKBD_MULTILINE
@ SWKBD_MULTILINE
Enable multiline input.
Definition: swkbd.h:63
SWKBD_DARKEN_TOP_SCREEN
@ SWKBD_DARKEN_TOP_SCREEN
Darken the top screen when the keyboard is shown.
Definition: swkbd.h:61
SWKBD_PREDICTIVE_INPUT
@ SWKBD_PREDICTIVE_INPUT
Enable predictive input (necessary for Kanji input in JPN systems).
Definition: swkbd.h:62
swkbdInit
void swkbdInit(SwkbdState *swkbd, SwkbdType type, int numButtons, int maxTextLength)
Initializes software keyboard status.
swkbdSetNumpadKeys
static void swkbdSetNumpadKeys(SwkbdState *swkbd, int left, int right)
Configures what characters will the two bottom keys in a numpad produce.
Definition: swkbd.h:226
SWKBD_TYPE_NORMAL
@ SWKBD_TYPE_NORMAL
Normal keyboard with several pages (QWERTY/accents/symbol/mobile)
Definition: swkbd.h:11
SWKBD_TYPE_WESTERN
@ SWKBD_TYPE_WESTERN
On JPN systems, a text keyboard without Japanese input capabilities, otherwise same as SWKBD_TYPE_NOR...
Definition: swkbd.h:14
SWKBD_TYPE_NUMPAD
@ SWKBD_TYPE_NUMPAD
Number pad.
Definition: swkbd.h:13
swkbdSetDictionary
void swkbdSetDictionary(SwkbdState *swkbd, const SwkbdDictWord *dict, int wordCount)
Sets the custom word dictionary to be used with the predictive input system of a software keyboard.
u32
uint32_t u32
32-bit unsigned integer
Definition: types.h:23