Welcome! Log In Create A New Profile

Advanced

libwiikeyboard : space key problem

Posted by TheDrev 
libwiikeyboard : space key problem
May 27, 2010 04:35PM
Hi, I'm using libwiikeyboard like in [wiibrew.org]
I've replace getchar with a scanf

	if (KEYBOARD_Init(keyPress_cb) == 0) printf("keyboard initialised\n");
	char cmd[32];
	do {
		// Call WPAD_ScanPads each loop, this reads the latest controller states
		WPAD_ScanPads();
 
		// WPAD_ButtonsDown tells us which buttons were pressed in this loop
		// this is a "one shot" state which will not fire again until the button has been released
		u32 pressed = WPAD_ButtonsDown(0);
 
		//char key = getchar();
		
		scanf("%s", cmd);

The strange thing if that the space key (ASCII code 32) act like a carriage return, event if the key code is not used in the callback function

void keyPress_cb(char sym) {
 
	if (sym == 13) putchar('\n');
	//else if (sym == 32) putchar(' ');
	else if (sym > 32 ) putchar(sym);
	else if ( sym == 0x1b) quitapp = true;
}

ALT Gr and Tab key also break the scanf's listening for user input...

what the heck ?



Edited 1 time(s). Last edit at 05/27/2010 04:46PM by TheDrev.
Re: libwiikeyboard : space key problem
May 28, 2010 12:34AM
try using this
if(sym!=0){
printf("ScanCode: %c\n",sym);
}
I havent tested this, but it should print the scancode of the number. Use this to determine if the scancodes are the same.
Re: libwiikeyboard : space key problem
May 28, 2010 01:26PM
Thanks for your response,
I alderly did that, the scan code is the good ASCII one.
I figured out that when using scanf, the spae, and tab keys 'break' the scanf. Even if the keycode is not used in the callback function (the putchar is only for the display).

The 'problem' is that inputs are in raw mode, it's good because it's a low level library, but still a pain in the *** for a quick development.



Edited 1 time(s). Last edit at 05/28/2010 02:58PM by TheDrev.
Re: libwiikeyboard : space key problem
May 28, 2010 03:58PM
Nothing wrong here. Except for you not reading the right documentation about scanf:
[www.cplusplus.com]

Quote

String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab).



Edited 1 time(s). Last edit at 05/28/2010 03:59PM by Daid.
Re: libwiikeyboard : space key problem
May 30, 2010 03:48AM
Take a look at the [[i]pattern[/i]] format specifier in the newlib docs on scanf().

Here's an example that allows spaces and tabs:
scanf("%31[0-9a-zA-Z \t]", cmd);
Re: libwiikeyboard : space key problem
May 31, 2010 02:29AM
thanks, that helps a lot. I wonder how scanf on GNU/libc or Windows' stdlib handle scanfs space key in the console.
My last problem is that the MAJ key put a Start of Header (http://en.wikipedia.org/wiki/C0_and_C1_control_codes, scan code 0x01, label SOH) character in the input buffer.
As I said the putchar used in keyPress_cb are simply here for echos to screen but the char is in the stdin, how do I remove/prevent thoses caracteres ?
Re: libwiikeyboard : space key problem
June 01, 2010 05:54AM
There's a problem in libwiikeyboard where pressing a modifier key (SHIFT, CTRL, etc) places a value in stdin. Here's the change I made to keyboard.c from libwiikeyboard to fix this:

--- libwiikeyboard/keyboard.c.orig      2009-11-11 11:37:47.000000000 -0500
+++ libwiikeyboard/keyboard.c   2010-02-27 09:27:52.000000000 -0500
@@ -434,9 +434,10 @@
        while (!_kbd_thread_quit) {
                if (((_keyBuffer.tail+1)&255) != _keyBuffer.head) {
                        if ( KEYBOARD_GetEvent(&event)) {
-                               if (event.type == KEYBOARD_PRESSED) {
-                                       _keyBuffer.buf[_keyBuffer.tail] = event.symbol;
-                                       _keyBuffer.tail++;
+                         if ( (event.type == KEYBOARD_PRESSED)
+                              && (event.symbol < 255)) {
+                                  _keyBuffer.buf[_keyBuffer.tail] = event.symbol;
+                                  _keyBuffer.tail++;
                                }
                        }
                }
I added the '&& (event.symbol < 255))' part. This might not be the ideal solution, but it's what I've been using without any problems.
Re: libwiikeyboard : space key problem
June 02, 2010 02:52PM
I'v recompiled the libwiikeyboard with this modification, and this had solved the MAJ key problem.

Unfortunatly, With scanf or fgets I am using the AltGr key to 'submit' the input (the Return key do not submit, only add the scan code 13), so I am using :

	char cur_input;
	for(i=0; i <= BUFFER_SIZE; i++) {
			cur_input = getc(stdin);
			if(cur_input != 13){
				cmd = cur_input;
			}
			else{
				cmd = '\0';
				break;
			}
	}

and it works !
Unfortunalty, i am also using lua and I need that AltGr key for sumbit io.read statments.
Question : How to disable the event symbol for Maj but not for AltGr, what is the symbol for (not ascii?)
Re: libwiikeyboard : space key problem
June 02, 2010 04:39PM
Quote
TheDrev
thanks, that helps a lot. I wonder how scanf on GNU/libc or Windows' stdlib handle scanfs space key in the console.
Just a wild guess, but I've noticed that they do some kind of line buffering, where you won't get any input until the enter key is pressed.
Re: libwiikeyboard : space key problem
June 02, 2010 04:41PM
Quote
TheDrev
Unfortunatly, With scanf or fgets I am using the AltGr key to 'submit' the input (the Return key do not submit, only add the scan code 13)

This works as expected for me with scanf(). I type something in, hit return, and whatever I typed, is in my cmd[] array. The only difference I can think of is I'm using a different keyboard than you. If you want, post somewhere your complete test app with source and a compiled .dol. I'll run this to see if I get the same results.
Re: libwiikeyboard : space key problem
June 04, 2010 03:21AM
I've solve the problem by only disabling the left and right shift character with

				if ( (event.type == KEYBOARD_PRESSED) &&  (event.symbol != 61697) &&  (event.symbol != 61698) ) {
					_keyBuffer.buf[_keyBuffer.tail] = event.symbol;
					_keyBuffer.tail++;
				}

I'll release a little demo of text based adventure game very soon and I'll be interrested to have some feedbacks.

The problem is maybe a AZERTY / QWERTY mess ?
In the keyboard_event structure I guess that the symbol property is the actual character typed, and the keycode is the wired keyboard key code ? example : the symbol of keycode 20 (first alpha key) is 97 'a' for AZERTY and 113 'q' for QWERTY.
But it is strange that AltGr and Return messed up as they are not likely to swap in every worldwide keyboard dispositions...
Anyway I got a AZERTY keyboard and AltGr symbol is 61706 for a keycode of 230 and the return key has a symbol of 13 for a keycode of 40.


****************************************
Edit: The application is done !
in the archive there is a directory ./v4_lua_wii/release with the files for the homebrew channel >>>>here<<<<
This is a text only adventure game 'engine' for Lua. there is only a mini test adventure in french, so it's not worth it to be put on the wiki right now. The C gets and Lua io.read inputs both works this is AltGr Key as submit button, can anyone test if it works with the return key with his IOS region and keyboard layout.
on the prompt the mini h2g2 demo can be started by typing the path to the main lua script, h2g2/h2g2_v3_fr.lua
Maybe one day I'll make a whole game from this.

****************************************



PS : for the scanf side question about buffering, I checked very fast the GNU libc and scanf source code is

stdio-common/scanf.c
int
__scanf (const char *format, ...)
{
  va_list arg;
  int done;

  va_start (arg, format);
  done = INTUSE(_IO_vfscanf) (stdin, format, arg, NULL);
  va_end (arg);

  return done;
}
ldbl_strong_alias (__scanf, scanf)

scanf is a wrapper around a modified version of vfscanf with the stdin as file argument, the doc says that INUSE is a macro for Internal use only function.
_IO_vfscanf, first argument is a buffered sort of File (_IO_FILE) defined in : libio/libioP.h
* The _IO_FILE type is used to implement the FILE type in GNU libc,
* as well as the streambuf class in GNU iostreams for C++.
* These are all the same, just used differently.



Edited 4 time(s). Last edit at 06/07/2010 07:56PM by TheDrev.
Sorry, only registered users may post in this forum.

Click here to login