1 <h2 id="manifest">Manifest</h2> 2 <p> 3 You must have a <code>"manifest_version"</code> of at least <code>2</code> to use this API. 4 </p> 5 6 {{?is_apps +partials.warning_dev /}} 7 8 <h2 id="usage">Usage</h2> 9 <p>The commands API allows you to define specific commands, and bind them to a 10 default key combination. Each command your extension accepts must be listed in 11 the manifest as an attribute of the 'commands' manifest key. An extension can 12 have many commands but only 4 suggested keys can be specified. The user can 13 manually add more shortcuts from the chrome://extensions/configureCommands 14 dialog.</p> 15 16 <p>Supported keys: A-Z, 0-9, Comma, Period, Home, End, PageUp, PageDown, Insert, 17 Delete, Arrow keys (Up, Down, Left, Right) and the Media Keys 18 (MediaNextTrack, MediaPlayPause, MediaPrevTrack, MediaStop).</p> 19 20 <p>Note: All key combinations must include either Ctrl* or Alt. Combinations 21 that involve Ctrl+Alt are not permitted in order to avoid conflicts with the 22 AltGr key. Shift can be used in addition to Alt or Ctrl, but is not required. 23 Modifiers (such as Ctrl) can not be used in combination with the Media Keys. 24 Tab key was removed from list of supported keys in Chrome version 33 and above 25 for accessibility reasons.<p> 26 27 <p>* Also note that on Mac 'Ctrl' is automatically converted to 'Command'. If 28 you want 'Ctrl' instead, please specify 'MacCtrl'.</p> 29 30 <p>* Additionally, on Chrome OS, you can specify 'Search' as a modifier.</p> 31 32 <p>Certain Chrome shortcuts (e.g. window management) always take priority over 33 Extension Command shortcuts and can not be overwritten.</p> 34 35 <pre data-filename="manifest.json"> 36 { 37 "name": "My extension", 38 ... 39 <b> "commands": { 40 "toggle-feature-foo": { 41 "suggested_key": { 42 "default": "Ctrl+Shift+Y", 43 "mac": "Command+Shift+Y" 44 }, 45 "description": "Toggle feature foo" 46 }, 47 "_execute_browser_action": { 48 "suggested_key": { 49 "windows": "Ctrl+Shift+Y", 50 "mac": "Command+Shift+Y", 51 "chromeos": "Ctrl+Shift+U", 52 "linux": "Ctrl+Shift+J" 53 } 54 }, 55 "_execute_page_action": { 56 "suggested_key": { 57 "default": "Ctrl+Shift+E", 58 "windows": "Alt+Shift+P", 59 "mac": "Alt+Shift+P" 60 } 61 } 62 }</b>, 63 ... 64 }</pre> 65 66 <p>In your background page, you can bind a handler to each of the commands 67 defined in the manifest (except for '_execute_browser_action' and 68 '_execute_page_action') via onCommand.addListener. For example:</p> 69 70 <pre> 71 chrome.commands.onCommand.addListener(function(command) { 72 console.log('Command:', command); 73 }); 74 </pre> 75 76 <p>The '_execute_browser_action' and '_execute_page_action' commands are 77 reserved for the action of opening your extension's popups. They won't normally 78 generate events that you can handle. If you need to take action based on your 79 popup opening, consider listening for an 'onDomReady' event inside your popup's 80 code. 81 </p> 82 83 <h2 id="usage">Scope</h2> 84 <p>By default, Commands are scoped to the Chrome browser, which means that while 85 the browser does not have focus, the shortcut will be inactive. On desktop 86 Chrome, Commands can instead have global scope, as of version 35, and will then 87 also work while Chrome does *not* have focus. NOTE: The exception here is 88 ChromeOS, where global commands are not allowed at the moment.</p> 89 90 <p>The user is free to designate any shortcut as global using the UI in 91 chrome://extensions \ Keyboard Shortcuts, but the extension developer is limited 92 to specifying only Ctrl+Shift+[0..9] as global shortcuts. This is to minimize 93 the risk of overriding shortcuts in other applications since if, for example, 94 Alt+P were to be allowed as global, the printing shortcut might not work in 95 other applications.</p> 96 97 <p>Example:</p> 98 99 <pre data-filename="manifest.json"> 100 { 101 "name": "My extension", 102 ... 103 "commands": { 104 "toggle-feature-foo": { 105 "suggested_key": { 106 "default": "Ctrl+Shift+5" 107 }, 108 "description": "Toggle feature foo", 109 <b>"global": true</b> 110 } 111 }, 112 ... 113 }</pre> 114