improved keydown/keyup algorithms