improved onkeydown event function