refactoring keypress and sendmessage function

Next step is to turn these into directives.