gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > keyboardGetString() function

#174021 - JackUzi - Thu May 13, 2010 4:54 am

I've been playing around with getting the user to enter a text string using both keyboardGetString() and the scanf("%s", buffer) method demonstrated in the keyboard_stdin example. I prefer the keyboardGetString() function as it provides the ability to set a maximum number of characters to read but the function pointed to by OnKeyPressed doesn't seem to get called when using this function. Is there a way to have the users typing reflected on the screen when using keyboardGetString()?

Regards,
Stuart

#174032 - JackUzi - Fri May 14, 2010 3:51 am

I ended up using the keyboardUpdate() function and just writing a wrapper function that gave me the console echo and the overrun protection...

Code:


void
read_keyboard(char *input_buffer, int maxlen) {
    int index = 0;

    while(index < maxlen) {

        int key = keyboardUpdate();

        if(key > 0) {
            *(input_buffer + index++) = key;
            printf("%c", key);
            if (key == 13 || key == 10) break;

            swiWaitForVBlank();
        }
    }
}

#174047 - JackUzi - Sat May 15, 2010 1:31 am

Just for the sake of completeness, this version of the function supports backspace, only prints printable characters and null terminates the string...

Code:


void
read_keyboard(char *input_buffer, int maxlen) {
   int index = 0;

   while(index < maxlen) {
       
      int key = keyboardUpdate();

      if(key > 0) {
         if (key == 8 && index != 0) {
            // DELETE THE PREVIOUS CHARACTER FROM THE BUFFER
            *(input_buffer + --index) = 0;
            printf("\x1b[1D \x1b[1D", key);
         } else if (key == 10) {
            printf("%c", key);
            break;
         } else if (key > 31 && key < 123) {
            // ADD THE KEY TO THE BUFFER AND DISPLAY IT
            *(input_buffer + index++) = key;
            printf("%c", key);
         }

         // WAIT FOR THE VERTICAL BLANK INTERRUPT (SCREEN REFRESH)
         swiWaitForVBlank();
      }
   }

        // NULL TERMINATE THE STRING
       *(input_buffer + index) = 0;

}