Can someone explain me how can I bind a key likewise "ctrl+k" to a javascript piece of code to be ran linked to a google chrome extension? This should work on any webpage that is opened.
Can someone explain me how can I bind a key likewise "ctrl+k" to a javascript piece of code to be ran linked to a google chrome extension? This should work on any webpage that is opened.
Share Improve this question edited Sep 21, 2013 at 19:42 Joheo asked Sep 21, 2013 at 19:10 JoheoJoheo 3521 gold badge4 silver badges14 bronze badges 2- Did you take a look at the documentation and sample extensions? developer.chrome./extensions/mands.html – rsanchez Commented Sep 21, 2013 at 19:54
- Yes I did, but still I am confused because I don't understand how should I bind any kind of action to the click of the keybind. Am I supposed to write that in the background page? And how should I write that same function specifically to "ctrl+k"? – Joheo Commented Sep 21, 2013 at 20:25
1 Answer
Reset to default 7In your manifest, you specify:
"mands": {
"my-mand-name": {
"suggested_key": {
"default": "Ctrl+K",
},
"description": "My description"
}
}
Then, in your background page you can do:
chrome.mands.onCommand.addListener(function(mand) {
if(mand === "my-mand-name") {
// Do your stuff
}
});
EDIT
One issue seems to be that the bination Ctrl+K
won't be assigned by default by Chrome because it's normally used for another purpose. If you specify Ctrl+K
as the default shortcut, the mand will not have a shortcut assigned, and the user needs to assign one from the 'Keyboard shortcuts' link at the bottom of the chrome://extensions
page.
If you use another bination, like Ctrl+Shift+K
it will probably be automatically assigned by Chrome. You can always check the shorcuts assignations in the link mentioned, and programmatically using chrome.mands.getAll
.
Also note that if you change the manifest and reload the extension, changes in the suggested shortcut key won't have any effect. You need to remove the extension and add it again for the suggested shorcut key to be considered.