fix read-past-end of buffer for key input (issue 7)