- Instant help with your JavaScript coding problems

Detect Ctrl+C and Ctrl+V using JavaScript

Question:
How to detect Ctrl+C and Ctrl+V using JavaScript?
Answer:
document.addEventListener('keydown', evt => {
    if (evt.key === 'c' && evt.ctrlKey) {
        alert('Ctrl+C was pressed');
    } else if (evt.key === 'v' && evt.ctrlKey) {
        alert('Ctrl+V was pressed');
    }
});
Description:

KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard. The event type (keydown , keypress , or keyup ) identifies what kind of keyboard activity occurred.

Note: The keyCode attribute - used in a lot of examples - is deprecated.

 

Share "How to detect Ctrl+C and Ctrl+V using JavaScript?"
Tags:
detect ctrl+c, detect ctrl+v, detect copy and paste, keyboard event
Technical term:
Detect Ctrl+C and Ctrl+V using JavaScript