0

I'd like to focus a search input on my Vue-based website using a shortcut like CMD + F on macOS or CTRL + F on Windows/Linux.

As shown in Check if Shift + letter is pressed in JS / Vue 2, a snippet like

document.onkeydown = function(e) {
  if (e.shiftKey && e.keyCode >= 46 && e.keyCode <= 90) {
    console.log("SHIFT + " + e.keyCode);
    return false;
  }
}

works fine for Shift etc., but I can't console.log out CMD so I don't really know how to proceed here.

A nice example (with CMD + K) is shown here: https://vee-validate.logaretm.com/v4/

So my questions:

  • What are the best ways to focus an input using a shortcut? I guess I'd always have to determine the OS family to check, whether I'd have to listen for CMD or CTRL?
  • Are there any libs for that?
SPQRInc
  • 343
  • 2
  • 12
  • 44
  • 3
    _Please_ don't. I would absolutely hate it, if you captured my attempt to trigger my browser's native search functionality in any way. – CBroe Dec 07 '21 at 07:45

0 Answers0