Keyboard optimizations by Divran

This is entirely Divrans code, I just checked it if it worked.
The only code from me is line 240 to prevent the Buffer from underflowing
(it was possible to have a negative amount of keys in the buffer)

See PR #235 for more information
Merge pull request #236 from Xandaros/patch-1

Keyboard optimizations by Divran