13

Is it possible to override the Ctrl+D? I want to, for example console.log or something, and not add the links to the bookmarks.

Rob W
  • 341,306
  • 83
  • 791
  • 678
shagon
  • 293
  • 3
  • 14

3 Answers3

10

Shortcuts can be overridden using the chrome.commands API. An extension can suggest a default shortcut (eg. Ctrl+D) in the manifest file, but users are free to override this at chrome://extensions/, as seen below:

Keyboard Shortcuts - Extensions and Apps

Usage

This API is still under development and only available at the Beta and Dev channels, and the Canary builds More info. It will probably available to everyone starting at Chrome 24.

If you want to test the API in Chrome 23 or lower, add the "experimental" permission to the manifest file, and use chrome.experimental.commands instead of chrome.commands. Also visit chrome://flags/ and enable "Experimental Extension APIs", or start Chrome with the --enable-experimental-extension-apis flag.

manifest.json

{
    "name": "Remap shortcut",
    "version": "1",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": [
        "tabs"
    ],
    "commands": {
        "test-shortcut": {
            "suggested_key": {
                "default": "Ctrl+D",
                "mac": "Command+D",
                "linux": "Ctrl+D"
            },
            "description": "Whatever you want"
        }
    }
}

background.js

// Chrome 24+. Use chrome.experimental.commands in Chrome 23-
chrome.commands.onCommand.addListener(function(command) {
    if (command === 'test-shortcut') {
         // Do whatever you want, for instance console.log in the tab:
         chrome.tabs.query({active:true}, function(tabs) {
             var tabId = tabs[0].id;
             var code = 'console.log("Intercepted Ctrl+D!");';
             chrome.tabs.executeScript(tabId, {code: code});
         });
    }
});

Documentation

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Tx, this works. But you should have in mind that you permanently override Ctrl+D, even if the shortcut is changed in the "Configure commands" dialog. That is, users won't be able to use/open the default Chrome dialog to create bookmarks. – cnmuc May 03 '16 at 22:07
7

It's not necessary to use chrome.commands -- you can use a content script to trap the keydown event, call preventDefault and stopPropagation on it, and handle it however you want. Example snippet that should work as part of a content script:

document.addEventListener('keydown', function(event) {
  if (event.ctrlKey && String.fromCharCode(event.keyCode) === 'D') {
    console.log("you pressed ctrl-D");
    event.preventDefault();
    event.stopPropagation();
  }
}, true);

The only things you can't override this way are window-handling commands, like ctrl-N and ctrl-<tab>.

int3
  • 12,861
  • 8
  • 51
  • 80
  • 3
    This does not work in all of the following cases: 1. Outside the context of a page, such as the omnibox. 2. Restricted pages, such as the New Tab or the Chrome Web Store. 3. Incognito mode or `file://` without the appropriate permissions. This is the only method which works in old Chrome versions, though. – Rob W Nov 17 '12 at 19:01
3

Alternative Solution: Type chrome:extensions into your browser's address bar. This will pull up the Chrome Extensions page.

Click on Keyboard Shortcuts in the top left menu ("Menu/Burger Button")

Assign Ctrl-D to a plugin that does not alter your Bookmarks.

This will solve the problem that an bookmark is created directly when you accidentally press Ctrl-D, instead the other Addon will pop up in that case.

Jens Kisters
  • 117
  • 2
  • 10
  • 1
    While I think the accepted answer from Rob W is more "pure", I think this solution will satisfy most people since they can just map ctrl+d to any existing plugins they already have. Note: I had to actually hold +d in the "Type a shortcut" box of the "Keyboard Shortcuts" page. I'm not sure what "Strg-D" is that Jens is referring to here. – airfishey May 29 '20 at 12:44
  • 1
    Sorry Strg is the german label for Ctrl. mixed up the languages there. fixed. – Jens Kisters Oct 06 '20 at 07:52