How to Record Key Presses
// ==UserScript==
// @name Keyboard Shortcut Example
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Example of using Tampermonkey to assign keyboard shortcuts
// @author Your Name
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Add event listener for keydown events
document.addEventListener('keydown', function(event) {
// eslint-disable-next-line no-console
console.log('event received' + JSON.stringify(
event
));
// eslint-disable-next-line no-console
console.log('event shift key ' + event.shiftKey);
// eslint-disable-next-line no-console
console.log('event ctrl key ' + event.ctrlKey);
// eslint-disable-next-line no-console
console.log('event key ' + event.key);
// Check if Ctrl + Shift + Y is pressed
if (event.ctrlKey && event.shiftKey && event.key === 'Y') {
// Prevent the default action
event.preventDefault();
// Perform the desired action
alert('Ctrl+Shift+Y pressed!');
}
});
})();
Example for shortcut plugin
From Example for Short Cut Plugin
Go to text ā
Here is example that can be further extended.
This place of the code where we can extend the to add more custom shortcuts:
const keySequences = {
'Ctrl+J+Ctrl+C': copyPageTitleAndURLAsMarkdown,
'Ctrl+K+Ctrl+L': () => alert('Ctrl+K followed by Ctrl+L pressed!')
// Add more sequences here
};
Tamper monkey user script:
// ==UserScript==
// @name Keyboard Shortcuts
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Example of using Tampermonkey to assign keyboard shortcut sequences
// @author Your Name
// @match *://*/*
// @grant none
// ==/UserScript==
function myNotify(msg){
console.log(msg);
alert(msg);
}
// Function to copy the current page title and URL as markdown
function copyPageTitleAndURLAsMarkdown() {
const title = document.title;
const url = window.location.href;
const markdown = `[${title}](${url})`;
navigator.clipboard.writeText(markdown).then(() => {
myNotify('Copied to clipboard: ' + markdown);
}).catch(err => {
console.error('Failed to copy: ', err);
});
}
(function() {
'use strict';
let sequence = [];
// Configuration for key sequences and their actions
const keySequences = {
'Ctrl+J+Ctrl+C': copyPageTitleAndURLAsMarkdown,
'Ctrl+K+Ctrl+L': () => alert('Ctrl+K followed by Ctrl+L pressed!')
// Add more sequences here
};
// Function to perform actions based on sequence
function performAction(seqString) {
if (keySequences[seqString]) {
keySequences[seqString]();
}
}
// Add event listener for keydown events
document.addEventListener('keydown', function(event) {
console.log("SHORT-CUTS: ", event.key, event.ctrlKey, event.altKey, event.shiftKey);
console.log('SEQUENCE: ', sequence.join(' , ') || 'NONE');
// does not equal control
if (event.ctrlKey && event.key != 'Control') {
sequence.push('Ctrl+' + event.key.toUpperCase());
} else {
// Clear sequence on non-control key press
sequence = [];
}
if (sequence.length === 2) {
const seqString = sequence.join('+');
performAction(seqString);
// Clear sequence after action
sequence = [];
}
});
})();
Children