admin管理员组文章数量:1406177
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 bination 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 bination 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.
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 bination 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 bination 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.
Share Improve this question edited May 19, 2021 at 12:35 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 24, 2012 at 21:29 ThiefMasterThiefMaster 319k85 gold badges607 silver badges646 bronze badges1 Answer
Reset to default 7Apparently, 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.
本文标签: javascriptOverride Chrome keyboard shortcuts in a user scriptStack Overflow
版权声明:本文标题:javascript - Override Chrome keyboard shortcuts in a user script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744968641a2635101.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论