Ctrl-Z/Y makes the browser focus on a recently changed input

Viewed 686

I have a canvas web application where I can an undo actions by pressing Ctrl+Z The application has also got text input elements

When I hit Ctrl+Z, on some occasions, the browser automatically focuses on a recently changed input. Does anyone know how to prevent this?

I'm using Chrome.

2 Answers

I had the same problem, though interestingly on Windows only. Calling preventDefault() on the event solved it for me.

In regards to Oak's answer, in my case I couldn't just preventDefault() every keydown because we need the key strokes so this worked for me:

 keyHandler = (e: KeyboardEvent) => {
   switch (e.key) {
    case e.ctrlKey && 'z': {
      e.preventDefault();
      break;
    }
}

It is strange how an onBlur doesn't stop this from happening but if a user manually clicks off the input then CTRL + Z automatically doesn't call the input...

Related