19

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
sabreshack
  • 558
  • 1
  • 4
  • 11
  • 1
    Is the above your actual shortcut? Meaning are you sure you're not trying to take over an existing Chrome shortcut which would have priority? – Matt Pavelle Aug 08 '13 at 18:08
  • I tried a bunch of different hotkey combinations, but none worked. I'm pretty sure these are not used by Chrome. – sabreshack Aug 08 '13 at 22:40

10 Answers10

26

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.

simo
  • 15,078
  • 7
  • 45
  • 59
  • 1
    Thanks! After two hours trying to understand why shortcuts don't work... I think Google needs to update their docs! – tomericco Dec 13 '13 at 14:14
  • 17
    @tomericco: It turns out the docs are not totally wrong: The suggested key works fine if and only if there is currently no other action bound to the same **[keyboard shortcut](https://support.google.com/chrome/topic/25799)**. One more thing that is not mentioned is that after updating the `commands` property in manifest, one needs to uninstall and reinstall the extension for the changes to take effect (as reloading or just disabling and re-enabling won't work). – gkalpak Dec 22 '13 at 11:20
  • @ExpertSystem is right on the money. I had to remove the extension, then reinstall it for the "commands" directives to work properly. Thanks! – Phil LaNasa Jan 11 '14 at 14:18
  • I'm still trying to figure out why on the linked keyboard shortcut page, Mac gets an extra Cmd+Shift+P neither windows nor linux has for "Page Setup Dialog" - which I assume to mean for printing... and does nothing. Way to go google! – Jeremy Mar 12 '14 at 07:01
  • 1
    The `Keyboard shortcuts` button has been removed. Shortcuts can now be accessed directly at `chrome://extensions/shortcuts` or via the hamburger meny on `chrome://extensions` page. – nu_popli Aug 27 '22 at 18:48
10

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.

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
sabreshack
  • 558
  • 1
  • 4
  • 11
10

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

5

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

Daejin Hyeon
  • 51
  • 1
  • 1
2

As per documentation:

The user is free to designate any shortcut as global using the UI in chrome://extensions \ Keyboard Shortcuts, but the extension developer is limited to specifying only Ctrl+Shift+[0..9] as global shortcuts. This is to minimize the risk of overriding shortcuts in other applications since if, for example, Alt+P were to be allowed as global, the printing shortcut might not work in other applications.

Silviu
  • 1,309
  • 1
  • 9
  • 20
  • This is for GLOBAL shortcuts, the original post does not use the global key denoting them as global so this does not appl.y – empire29 Oct 30 '20 at 13:27
1

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?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Matt Pavelle
  • 819
  • 5
  • 8
  • No that does not work either..you have an interesting point though; it might be the case that even Ctrl+Shift+U is used by something else on my machine. – sabreshack Aug 09 '13 at 17:56
  • Just confirming, here is the sequence of steps you followed right? 1) Modify manifest.json with the suggested keys 2) Go to chrome://extensions and reload the extension 3) Go to a random tab and try out Command+Shift+U and the browser action popup should open – sabreshack Aug 09 '13 at 17:59
  • Or might it be the case that my Chrome has keyboard shortcuts disabled? If so, how do I enable them? – sabreshack Aug 09 '13 at 18:06
  • Yeah, so download this: http://developer.chrome.com/extensions/examples/api/commands.zip and just change the "commands" to what I pasted above and it works... – Matt Pavelle Aug 09 '13 at 20:48
  • Hmm..still no luck..even after I reload the extension in Chrome, the shortcut does not register itself until I go and manually set it. – sabreshack Aug 09 '13 at 20:57
  • At this point it's probably worth trying it on a different user account on your Mac or a different machine altogether. – Matt Pavelle Aug 10 '13 at 15:05
1

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.

255.tar.xz
  • 700
  • 7
  • 23
0

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".

gianni
  • 11
0

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.

0

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.

Yoni Dor
  • 323
  • 3
  • 8