While debugging an issue with Bookee (keyboard shorcuts not working), I noticed that a friend of mine had an extension installed that allowed her to define a Global Shortcut.
At first, I had no clue what a Global Shortcut was (but I was curious). Upon researching more, I realized that a Global Shortcut allows users to initiate an extension command even when they're not in Chrome!
I thought this was ideal for Bookee, since I often want to go to a bookmarked URL even when I'm not using Chrome. Here are a few things I noticed/ran into:
- Extension shortcuts can be accessed here: chrome://extensions/shortcuts (for security reasons, this link will likely not work)
- This page allows users to override/define Chrome/Web Extension shortcuts, different from what the developer(s) may have defined.
- These settings cannot be changed or defined programmatically; they can only be changed or defined by users.
-
A
Chrome
scope for a shortcut defines the keyboard combination that can be pressed while the Chrome window is the focus -
A
Global
scope for a shortcut defines the keyboard combination that can be be pressed regardless of what operating-system level application is in focus -
Not all extensions will be able to define a
Global
shortcut (for example, initially Bookee couldn't). -
In order to allow a user to change the keyboard
combination from a
Chrome
-level shortcut to aGlobal
-level shortcut, changes may need to be made to themanifest.json
file for the extension (see below).
Manifest changes requireed to allow for Global
shortcuts
While exploring the manifest.json
file for the
Pocket Chrome Extension,
I noticed that they define their shortcut combination
differently. This is what I had for
Bookee
before:
"commands": {
"_execute_action": {
"suggested_key": {
"default": "Ctrl+Shift+K"
},
"description": "Show Bookee"
}
}
This is what I changed it to:
"commands": {
"toggle-bookee-command": {
"suggested_key": {
"default": "Ctrl+Shift+K"
},
"description": "Toggle Bookee"
}
}
Then, I naturally had to change the code/logic in the background script (or worker script if you're using version 3 of Chrome's manifest) to listen for that event.
By doing this, it made it possible for users (specifically,
me) to change the scope of the keyboard shortcut to
Global
. Now, I can trigger Chrome to spring
into action from any window on my computer.
For a little more context, the _execute_action
property for the manifest.json
file is kind of
like a "default" property. Defining it will make a specific
browser-action event available (which you can listen to
using chrome.action.onClicked.addListener
). But
this has limitations: it's scope cannot be changed to
Global
, which means the keyboard combination
will only be available within Chrome.
By defining the keyboard combination through it's own unique
key (in this case toggle-bookee-command
), the
keyboard shortcut can be bound to the operating system (via
the Global
scope value).
Hope you find this helpful :)
Updates
-
It turns out that these commands can be
defined programmatically by developers, but there are
limitations: commands can have a
global
property defined with a possible value oftrue
orfalse
, but only for commands that follow the following combination format:Ctrl+Shift+[0..9]