Skip to content

Instantly share code, notes, and snippets.

@eguneys
Created May 11, 2026 09:21
Show Gist options
  • Select an option

  • Save eguneys/4a27a7d609732b2caca9085ef32e0749 to your computer and use it in GitHub Desktop.

Select an option

Save eguneys/4a27a7d609732b2caca9085ef32e0749 to your computer and use it in GitHub Desktop.
document.addEventListener('keydown', function(e) {
// 1. Detect Ctrl + J (or Cmd + J on Mac)
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'j') {
// 2. Stop the browser's default action for Ctrl + J
e.preventDefault();
// 3. Create a synthetic "Enter" keydown event
const enterEvent = new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
keyCode: 13,
which: 13,
bubbles: true, // Allows the event to travel up the DOM
cancelable: true // Allows other scripts to call preventDefault()
});
// 4. Dispatch the event on the currently focused element
e.target.dispatchEvent(enterEvent);
}
// Detect Ctrl + H (or Cmd + H on Mac)
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 'h') {
// Prevent default browser action (often "History" in some browsers)
e.preventDefault();
// Create a synthetic "Backspace" keydown event
const backspaceEvent = new KeyboardEvent('keydown', {
key: 'Backspace',
code: 'Backspace',
keyCode: 8,
which: 8,
bubbles: true,
cancelable: true
});
// Dispatch it to the element currently in focus
e.target.dispatchEvent(backspaceEvent);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment