libctru  v2.4.1
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";
}
if (strstr(text, "brick"))
{
*ppMessage = "~Time to visit Brick City~";
}
}
int main(int argc, char **argv)
{
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())
{
u32 kDown = hidKeysDown();
if (kDown & KEY_START)
break;
static SwkbdState swkbd;
static char mybuf[60];
static SwkbdStatusData swkbdStatus;
static SwkbdLearningData swkbdLearning;
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);
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);
swkbdSetNumpadKeys(&swkbd, L'ツ', L'益');
button = swkbdInputText(&swkbd, mybuf, sizeof(mybuf));
}
if (kDown & KEY_X)
{
didit = true;
swkbdInit(&swkbd, SWKBD_TYPE_WESTERN, 1, -1);
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);
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;
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
}
return 0;
}
Central 3DS header.
void aptSetChainloaderToSelf(void)
Configures the chainloader to relaunch the current application (i.e. soft-reset)
bool aptMainLoop(void)
Main function which handles sleep mode and HOME/power buttons - call this at the beginning of every f...
PrintConsole * consoleInit(gfxScreen_t screen, PrintConsole *console)
Initialise the console.
void gfxSwapBuffers(void)
Updates the configuration of both screens.
void gfxInitDefault(void)
Initializes the LCD framebuffers with default parameters This is equivalent to calling:
@ GFX_TOP
Top screen.
Definition: gfx.h:26
void gfxExit(void)
Deinitializes and frees the LCD framebuffers.
void gfxFlushBuffers(void)
Flushes the data cache for the current framebuffers.
#define gspWaitForVBlank()
Waits for VBlank.
Definition: gspgpu.h:151
@ KEY_B
B.
Definition: hid.h:13
@ KEY_X
X.
Definition: hid.h:22
@ KEY_Y
Y.
Definition: hid.h:23
@ KEY_START
Start.
Definition: hid.h:15
@ KEY_A
A.
Definition: hid.h:12
u32 hidKeysDown(void)
Returns a bitmask of newly pressed buttons, this frame.
void hidScanInput(void)
Scans HID for input data.
Keyboard dictionary word for predictive input.
Definition: swkbd.h:114
Keyboard predictive input learning data.
Definition: swkbd.h:126
Software keyboard parameter structure, it shouldn't be modified directly.
Definition: swkbd.h:141
Keyboard status data.
Definition: swkbd.h:124
void swkbdSetButton(SwkbdState *swkbd, SwkbdButton button, const char *text, bool submit)
Configures a dialog button in a software keyboard.
SwkbdCallbackResult
Keyboard filter callback return values.
Definition: swkbd.h:73
@ SWKBD_CALLBACK_CLOSE
Displays an error message, then closes the keyboard.
Definition: swkbd.h:75
@ SWKBD_CALLBACK_OK
Specifies that the input is valid.
Definition: swkbd.h:74
@ SWKBD_CALLBACK_CONTINUE
Displays an error message and continues displaying the keyboard.
Definition: swkbd.h:76
void swkbdSetInitialText(SwkbdState *swkbd, const char *text)
Sets the initial text that a software keyboard will display on launch.
SwkbdButton swkbdInputText(SwkbdState *swkbd, char *buf, size_t bufsize)
Launches a software keyboard in order to input text.
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...
void swkbdSetLearningData(SwkbdState *swkbd, SwkbdLearningData *data, bool in, bool out)
Configures software keyboard predictive input learning data management.
void swkbdSetFeatures(SwkbdState *swkbd, u32 features)
Specifies which special features are enabled in a software keyboard.
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
Characters are concealed a second after they've been typed.
Definition: swkbd.h:43
static void swkbdSetPasswordMode(SwkbdState *swkbd, SwkbdPasswordMode mode)
Configures password mode in a software keyboard.
Definition: swkbd.h:201
SwkbdButton
Keyboard dialog buttons.
Definition: swkbd.h:30
@ SWKBD_BUTTON_LEFT
Left button (usually Cancel)
Definition: swkbd.h:31
@ SWKBD_BUTTON_RIGHT
Right button (usually OK)
Definition: swkbd.h:33
@ SWKBD_BUTTON_MIDDLE
Middle button (usually I Forgot)
Definition: swkbd.h:32
@ SWKBD_BUTTON_NONE
No button (returned by swkbdInputText in special cases)
Definition: swkbd.h:35
void swkbdSetStatusData(SwkbdState *swkbd, SwkbdStatusData *data, bool in, bool out)
Configures software keyboard internal status management.
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
Disallow profanity using Nintendo's profanity filter.
Definition: swkbd.h:53
@ SWKBD_FILTER_AT
Disallow the use of the @ sign.
Definition: swkbd.h:50
@ SWKBD_FILTER_BACKSLASH
Disallow the use of the \ sign.
Definition: swkbd.h:52
@ SWKBD_FILTER_DIGITS
Disallow the use of more than a certain number of digits (0 or more)
Definition: swkbd.h:49
@ SWKBD_FILTER_PERCENT
Disallow the use of the % sign.
Definition: swkbd.h:51
@ SWKBD_NOTEMPTY_NOTBLANK
Empty or blank inputs (consisting solely of whitespace) are not accepted.
Definition: swkbd.h:22
@ SWKBD_ANYTHING
All inputs are accepted.
Definition: swkbd.h:20
static SwkbdResult swkbdGetResult(SwkbdState *swkbd)
Retrieves the result condition of a software keyboard after it has been used.
Definition: swkbd.h:318
SwkbdResult
Keyboard return values.
Definition: swkbd.h:81
@ SWKBD_RESETPRESSED
The soft-reset key combination was pressed.
Definition: swkbd.h:94
@ SWKBD_POWERPRESSED
The POWER button was pressed.
Definition: swkbd.h:95
@ SWKBD_HOMEPRESSED
The HOME button was pressed.
Definition: swkbd.h:93
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
Allow the usage of the POWER button.
Definition: swkbd.h:67
@ SWKBD_ALLOW_RESET
Allow the usage of a software-reset combination.
Definition: swkbd.h:66
@ SWKBD_ALLOW_HOME
Allow the usage of the HOME button.
Definition: swkbd.h:65
@ SWKBD_FIXED_WIDTH
Enable fixed-width mode.
Definition: swkbd.h:64
@ SWKBD_MULTILINE
Enable multiline input.
Definition: swkbd.h:63
@ SWKBD_DARKEN_TOP_SCREEN
Darken the top screen when the keyboard is shown.
Definition: swkbd.h:61
@ SWKBD_PREDICTIVE_INPUT
Enable predictive input (necessary for Kanji input in JPN systems).
Definition: swkbd.h:62
void swkbdInit(SwkbdState *swkbd, SwkbdType type, int numButtons, int maxTextLength)
Initializes software keyboard status.
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
Normal keyboard with several pages (QWERTY/accents/symbol/mobile)
Definition: swkbd.h:11
@ 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
Number pad.
Definition: swkbd.h:13
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.
uint32_t u32
32-bit unsigned integer
Definition: types.h:23