最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Action on key-bind google chrome extension - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 7

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

发布评论

评论列表(0)

  1. 暂无评论