Added key shortcuts for editing text

Original fix by dvwilliamson. Also cleaned up whitespace and added
explanatory comments to the key handling code.