Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to detect copy paste commands Ctrl+V, Ctrl+C using JavaScript?
You'll learn how to use JavaScript to detect the keystrokes Ctrl+V and Ctrl+C in this article. When a user wishes to paste something into an input field, they have two options: either they can use the context menu by right-clicking on the webpage or they may use their keyboard by pressing the CTRL and V buttons simultaneously.
The keydown event's ctrlKey property is used to figure out the key combination that contains "Ctrl." To identify whether "ctrl" is pressed or not during the key event, it produces a "boolean" value.
Syntax
event.ctrlKey
Return Value
true ? After pressing the "ctrl" key.
false ? If the "ctrl" key was not pressed.
Example 1: Using Key Codes
This example demonstrates detecting Ctrl+C (copy) and Ctrl+V (paste) using key codes and the ctrlKey property.
<!DOCTYPE html>
<html>
<head>
<title>Detect Copy Paste Commands</title>
<style>
.container {
display: flex;
flex-direction: column;
align-items: center;
margin: 20px;
}
textarea {
margin: 10px;
padding: 10px;
width: 300px;
height: 100px;
}
.result {
margin: 10px;
padding: 10px;
font-weight: bold;
color: green;
}
</style>
</head>
<body>
<div class="container">
<h3>Copy and Paste Detection</h3>
<textarea placeholder="Type some text here"></textarea>
<textarea placeholder="Paste text here"></textarea>
<div id="result1" class="result"></div>
<div id="result2" class="result"></div>
</div>
<script>
document.body.addEventListener("keydown", function(event) {
let key = event.keyCode || event.which;
// Check for Ctrl+V (paste) - V key code is 86
if (key === 86 && event.ctrlKey) {
document.getElementById("result1").innerHTML = "Ctrl+V (Paste) detected!";
setTimeout(() => {
document.getElementById("result1").innerHTML = "";
}, 2000);
}
// Check for Ctrl+C (copy) - C key code is 67
else if (key === 67 && event.ctrlKey) {
document.getElementById("result2").innerHTML = "Ctrl+C (Copy) detected!";
setTimeout(() => {
document.getElementById("result2").innerHTML = "";
}, 2000);
}
});
</script>
</body>
</html>
Example 2: Using Modern Key Property
This example uses the more modern key property instead of key codes for better readability and cross-browser compatibility.
<!DOCTYPE html>
<html>
<head>
<title>Modern Copy Paste Detection</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin: 50px;
}
.instruction {
background: #f0f0f0;
padding: 20px;
border-radius: 5px;
margin: 20px 0;
}
</style>
</head>
<body>
<h2>Keyboard Shortcut Detection</h2>
<div class="instruction">
Try pressing Ctrl+C or Ctrl+V to see the detection in action!
</div>
<textarea placeholder="Test area for copy/paste operations" style="width: 400px; height: 100px;"></textarea>
<script>
document.addEventListener('keydown', function(event) {
if (event.key === 'c' && event.ctrlKey) {
alert('Copy command detected! (Ctrl+C)');
console.log('User pressed Ctrl+C');
}
else if (event.key === 'v' && event.ctrlKey) {
alert('Paste command detected! (Ctrl+V)');
console.log('User pressed Ctrl+V');
}
});
</script>
</body>
</html>
Key Properties Comparison
| Property | Description | Example Values |
|---|---|---|
event.keyCode |
Numeric key code (deprecated) | 67 for 'C', 86 for 'V' |
event.key |
String representation of key | 'c', 'v', 'Enter' |
event.ctrlKey |
Boolean for Ctrl key state | true/false |
Practical Applications
Detecting copy-paste operations is useful for:
Security ? Preventing unauthorized copying of sensitive content
Analytics ? Tracking user interactions with forms
Custom Actions ? Triggering special behaviors on copy/paste
Input Validation ? Processing pasted content before insertion
Browser Compatibility
The ctrlKey property and keydown events are supported in all modern browsers. The key property provides better cross-platform support than legacy keyCode.
Conclusion
Detecting Ctrl+C and Ctrl+V commands in JavaScript is straightforward using the keydown event listener with ctrlKey property. Use the modern key property for better code readability and maintainability.
