Cocoa: Complete and refactor keyboard handling