It looks like you need http://patorjk.com/keyboard-layout-analyzer/
This handy program will analyze a block of text and tell you how far your fingers had to travel to type it, then recommend your optimal layout.
To answer your original question, on Linux you can read from /dev/event* for local keyboard, mouse and joystick events. I believe you could for example simply cat /dev/event0 > keylogger
. The events are instances of struct input_event
. See also http://www.linuxjournal.com/article/6429.
Python's struct module is a convenient way to parse binary data.
For OSX, take a look at the source code to logkext. http://code.google.com/p/logkext/