implement applyKeys with collision handling (half of it was already committed accidentally)