Apr 20, 2014
I needed to catch Ctrl+A and redirect it to copy the content of a specific element. This does exactly that:
document.addEventListener('keydown', function (event) {
if(event.ctrlKey && event.keyCode == 65) {
var range = document.createRange();
range.selectNode(document.getElementsByTagName('main')[0]);
window.getSelection().addRange(range);
event.preventDefault();
}
});
What it does:
keydown
handler to document
to get all key presses<main>
in this case)preventDefault
to make sure the event is ignored by the browser