ThiefMaster
ThiefMaster

Reputation: 318508

Override Chrome keyboard shortcuts in a user script

I wrote a user script that performs a certain operation on selected text in a textarea when pressing CTRL+SHIFT+B.

This is done by registering a keypress event for the textarea and then checking the pressed key. To prevent the browser from handling the key combination I'm using the preventDefault() method which works fine in Firefox (the Library window is not opened but my code is executed instead).

However, in Chrome that key combination opens the bookmarks bar and the keypress event is not even triggered.

I wonder if there's a way to create such a shortcut in Chrome. It needs to work with a userscript - a real extension is not an option since I'd prefer not to maintain two different "versions" for Firefox and Chrome.

Upvotes: 7

Views: 3015

Answers (1)

Brock Adams
Brock Adams

Reputation: 93473

Apparently, the Chrome UI triggers off of keydown instead of keypress (This Quirksmode article may suggest why -- keypress is supposed to fire when an actual character is being inserted).

So changing the appropriate line to:

$(document).on('keydown', '.wmd-input', function(e) {

Seems to work on both FF and Chrome.

Upvotes: 7

Related Questions