chrome.commands keyboard shortcuts not working

2019-01-11 14:09发布

问题:

I am trying to add some keyboard shortcuts to my Chrome extension, specifically to allow the user to use hotkeys to open up a browser action/popup. I've read the documentation, and added the following lines of code to the manifest.json file:

"commands": {
  "_execute_browser_action": {
    "suggested_key": {
      "windows": "Ctrl+Shift+Y",
      "mac": "Command+Shift+Y",
      "chromeos": "Ctrl+Shift+U",
      "linux": "Ctrl+Shift+J"
    }
  }
}

After adding this, I reloaded my extension in chrome://extensions and proceeded to try out Command+Shift+Y on my Mac and nothing happened. For about 2 hours, I tried setting different hotkey combinations in manifest.json, but none worked. I switched from the dev version of Chrome to the stable version but to no avail. When I went to chrome://extensions and clicked on the 'Keyboard Shortcuts' button at the bottom right, I was able to manually set a hotkey combination and then it worked. But I don't want the user to have to do that manual work.

回答1:

On Chrome 29 you have to navigate to chrome://extensions/ and scroll down to the bottom of the page. On the right side there is a button Keyboard shortcuts.

Modal dialog pops up with all extensions that have registered some commands in their manifest file. But the shortcuts itself are Not set so the user must set them manually.



回答2:

As you can see in the source code here: https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/extensions/api/commands/command_service.cc&l=303&sq=package:chromium&rcl=1409677023

The key binding update is only run when OnExtensionWillBeInstalled callback is triggered.

So you need to uninstall and reinstall your local extension to see the default keyboard command appear in : chrome://extensions/configureCommands



回答3:

Somehow the keyboard shortcuts started working after I set the shortcuts to contain just one of Ctrl / Cmd / Alt / Shift. So, Alt+S worked but Alt+Shift+S did not work.



回答4:

If you modified your code after loading your extension, you need to remove and reload it.



回答5:

Your (and my) Command+Shift+Y keystroke is likely being used by another OSX app (possibly stickies).

This works on my Mac/Chrome combo (changed the Y to U):

"browser_action": {
  "default_popup": "browser_action.html"
},
"commands": {
  "_execute_browser_action": {
    "suggested_key": {
      "default": "Ctrl+Shift+U",
      "windows": "Ctrl+Shift+U",
      "mac": "Command+Shift+U",
      "chromeos": "Ctrl+Shift+U",
      "linux": "Ctrl+Shift+U"
    }
  }
}

Does it work for you?



回答6:

If you are testing your shortcut with console.log, it will not show up. Perhaps test it with chrome.tabs.create({url: "http://www.google.com/"});

This will require permissions to "tabs".



回答7:

I was struggling with the same issue of the "_execute_browser_action" keyboard shortcut not being set automatically despite not conflicting with any existing shortcuts.

It turns out my problem was caused by the following:

...
"commands" : {
"_execute_browser_action": {
        "suggested_key": {
          "mac": "Alt+J",
          "linux": "Ctrl+Shift+J"
        },
      "global": true <-- this shouldn't be here
      }
...

Removing the "global": true resolved my issue. Hope this helps.



回答8:

Adding to all of the correct answers above: After removing and adding the extension in the extensions page, the shortcut still didn't apply to the current window i was working in. It only worked once i opened a new chrome window.



回答9:

Guys this is very obvious!

  • Ctrl+Shift+J
  • Ctrl+Shift+S
  • Ctrl+Shift+U

All are built in browser functions!
Which cannot be overridden!
Just simply make your shortcut not a built in one.

A list of Chrome browser shortcuts can be found here

Example:

  • Ctrl+Shift+L

Would work to trigger Browser_Action.