GP2X Reading Usb Keyboard


foft

Certified Guru
Joined
Mar 14, 2004
Messages
480
Location
London, UK
Website
www.scrameta.net
I'm pulling my hair out trying to read characters from the keyboard! All I want is a nice simple way to find out which keys are currently pressed (unbuffered press/release events also ok) under Linux. I don't want to use SDL or some heavyweight library. There MUST be a simple way of doing this since its pretty much a core OS function!

Closest I've found is:

CODE

#include <stdio.h>
#include <termios.h>
#include <sys/ioctl.h>

main()
{
struct termios ts;
struct termios new_ts;
ioctl(0, TCGETS, &ts);
new_ts = ts;
new_ts.c_lflag &= !ICANON;
new_ts.c_lflag &= !ECHO;
ioctl(0, TCSETS, &new_ts);

char c=0;
while (c!='q')
{
c=getchar();
printf("%c\n",c);
}
ioctl(0, TCSETS, &ts);
}



Which is still rubbish since I want to know everything that is pressed at the current time. Or as a minimum an event on press/release of each key.

Any advice greatfully appreciated. Someone kindly sent me a Cradle to fix TVout/keyboard/joystick on Atari800. TVout was simple due to Kounch's work. Keyboard is proving hard - spent a few hours on it so far - to read a keyboard :angry: Any good (multiple) joystick code/suggestions also greatly appreciated.

Thanks,

Mark
 
Thanks I'll check out usbjoy. As for keyboard think I'll have a go starting from the scankeys app as a base... Amazing how something so simple seems to be so hard. Go Linux!

Mark
 
foft said:
Thanks I'll check out usbjoy. As for keyboard think I'll have a go starting from the scankeys app as a base... Amazing how something so simple seems to be so hard. Go Linux!

Mark
i never tried reading an usb keyboard from gp2x, but if you manage to read it succesfully please let me know :)
 
Last edited by a moderator:
foft said:
I'm pulling my hair out trying to read characters from the keyboard! All I want is a nice simple way to find out which keys are currently pressed (unbuffered press/release events also ok) under Linux. I don't want to use SDL or some heavyweight library. There MUST be a simple way of doing this since its pretty much a core OS function!
I don't want to use SDL either, but each and every program that I've heard that works with USB keyboard seems to be SDL based, so I searched for the source of gp2x's SDL libraries and didn't find them, but the WIP open2x can be accessed via SVN, and there I found a method opening tty terminals, in some functions of this file:

http://open2x.svn.sourceforge.net/viewvc/o...s.c?view=markup

and also another alternative method that seems better, but unfortunately is still unfinished:

http://open2x.svn.sourceforge.net/viewvc/o...s.c?view=markup



I was waiting for paeryn to finish his new USB keyboard code, but time passes and there's no futher advance, so I think I'll try the tty method.

Keep us informed if you manage to handle it properly.
 
Last edited by a moderator:
Thanks metalbrain, some good info there.

My latest attempt is at: http://ssh.scrameta.net/www.scrameta.net/l...eyboard.tar.bz2. It uses /dev/input/eventXX to get the press/release events. Just using the defines from /usr/include/linux/input.h is rubbish since it only works for a fixed US layout I think. So the codes are then decoded via a keyboard map loaded using a hacked version of loadkeys (built in). Of course you could use loadkeys itself, then read the keymap from /dev/tty0. I left the /dev/tty0 code in (commented out) as an example...

To use it, 'build' then copy keyboardexample and keymaps folder to sd card root. Then run keyboardexample and press some keys...

Mark
 
In case anyone was (silently) interested - I've just updated the link with a much newer version.
i) Connect/disconnect/multiple device support.
ii) Joystick support.
iii) Mouse support.

Going to try it for real in atari800 now... Only tried the included example so far.

Mark
 
foft said:
In case anyone was (silently) interested - I've just updated the link with a much newer version.
i) Connect/disconnect/multiple device support.
ii) Joystick support.
iii) Mouse support.

Going to try it for real in atari800 now... Only tried the included example so far.
Thanks a lot for your work.

To use the USB keyboard in emulators, I think only the raw key codes are really necessary, since you want to map the keys to the emulated computer ones, not to get the extra symbols that vary between local distributions, so a simpler version without keymaps and loadkeys would be nice.

Good luck with the next atari800 version!
 
Last edited by a moderator:
Metalbrain said:
Thanks a lot for your work.

To use the USB keyboard in emulators, I think only the raw key codes are really necessary, since you want to map the keys to the emulated computer ones, not to get the extra symbols that vary between local distributions, so a simpler version without keymaps and loadkeys would be nice.

Good luck with the next atari800 version!
Yes true, thought I'd quite like it to show the key I've pressed on the emulator menu. Also for common (non-system specific) stuff I'd like the value on the keyboard to be used. Then you only need to remember special keys, rather than get out the old computer to find out where the " character was... Not to mention it being in the wrong place on the keyboard.

Anyway to rip it out just remove loadKeys library, then toUnicode and setkeymap from keyboard.cpp/h.
 
Last edited by a moderator:
Course that keycode stuff isn't unicode. I'm yet to work out what it IS though. Conversion to ascii by & with 0xff works in some cases, but I want to know what the upper bits represent... The code for loadkeys is as clear as very thick mud.
 
Thought this might be of interest to people who find this thread in future... I've not tried it, but certainly looks simpler.

QUOTE

OTOH, Gnostic found another way to read the keyboard for Vice, and has posted his code here:
http://www.gp32x.de/board/index.php?showt...43255&st=75



Also check Metalbrain's code from GP2Xpectrum, which uses /dev/input/event... .
 
Last edited by a moderator:
Back
Top