Factorisation code onKeyPressed