better implementation of keypress simulation