How to Detect Ctrl+V, Ctrl+C Using JavaScript

How to detect Ctrl+V, Ctrl+C using JavaScript?

I just did this out of interest. I agree it's not the right thing to do, but I think it should be the op's decision... Also the code could easily be extended to add functionality, rather than take it away (like a more advanced clipboard, or Ctrl+s triggering a server-side save).

$(document).ready(function() {    var ctrlDown = false,        ctrlKey = 17,        cmdKey = 91,        vKey = 86,        cKey = 67;
$(document).keydown(function(e) { if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = true; }).keyup(function(e) { if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = false; });
$(".no-copy-paste").keydown(function(e) { if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey)) return false; }); // Document Ctrl + C/V $(document).keydown(function(e) { if (ctrlDown && (e.keyCode == cKey)) console.log("Document catch Ctrl+C"); if (ctrlDown && (e.keyCode == vKey)) console.log("Document catch Ctrl+V"); });});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><h3>Ctrl+c Ctrl+v disabled</h3><textarea class="no-copy-paste"></textarea><br><br><h3>Ctrl+c Ctrl+v allowed</h3><textarea></textarea>

how to detect CTRL+C and CTRL+V key pressing using regular expression?

You can't detect key pressing with RegExp, though you can like following:

document.body.addEventListener("keydown",function(e){
e = e || window.event;
var key = e.which || e.keyCode; // keyCode detection
var ctrl = e.ctrlKey ? e.ctrlKey : ((key === 17) ? true : false); // ctrl detection

if ( key == 86 && ctrl ) {
console.log("Ctrl + V Pressed !");
} else if ( key == 67 && ctrl ) {
console.log("Ctrl + C Pressed !");
}

},false);

JSFiddle

Detect Ctrl + C and Ctrl + V in an input from browsers

You simply can do who : for information this code manage all mac user who user CMD instead of ctrl

@HostListener('window:keydown',['$event'])
onKeyPress($event: KeyboardEvent) {
if(($event.ctrlKey || $event.metaKey) && $event.keyCode == 67)
console.log('CTRL + C');
if(($event.ctrlKey || $event.metaKey) && $event.keyCode == 86)
console.log('CTRL + V');
}

If you want to detect other kind of shortcut :

  • event.ctrlKey
  • event.altKey
  • event.metaKey (Aka Cmd for mac user)

Online sample

--- UPDATE AFTER COMMENT ---

You may can do something like this

  ngOnInit() {
this.bindKeypressEvent().subscribe(($event: KeyboardEvent) => this.onKeyPress($event));
}

onKeyPress($event: KeyboardEvent) {
if(($event.ctrlKey || $event.metaKey) && $event.keyCode == 67)
console.log('CTRL + C');
if(($event.ctrlKey || $event.metaKey) && $event.keyCode == 86)
console.log('CTRL + V');
}

private bindKeypressEvent(): Observable<KeyboardEvent> {
const eventsType$ = [
fromEvent(window, 'keypress'),
fromEvent(window, 'keydown')
];
// we merge all kind of event as one observable.
return merge(...eventsType$)
.pipe(
// We prevent multiple next by wait 10ms before to next value.
debounce(() => timer(10)),
// We map answer to KeyboardEvent, typescript strong typing...
map(state => (state as KeyboardEvent))
);
}

or if is not working, just :

private bindKeypress(): Observable<KeyboardEvent> {
const typeOfEvent = this.deviceService.getKeybordEvent();
fromEvent(window, typeOfEvent)
.pipe(
// We map answer to KeyboardEvent, typescript strong typing...
map(state => (state as KeyboardEvent))
);
}

Where this.deviceService.getKeybordEvent(); is method who return type of event base on User Agent. massive list of user agent can be find here

VueJS - How to detect Ctrl+V?

To detect two keys, Vue provides modifier keys, For example to detect Alt+C, you can simply do:

<input @keyup.alt.67="YourFn">

Similarly for Ctrl+V, you can do:

<input @keyup.ctrl.76="YourFn">

As I can see here, ASCII code for Ctrl+v is 22, so you should be simply able to do :

<input @keyup.22="YourFn">

Allow only numbers and ctrl+a , ctrl+v , ctrl+c to a textbox

Try with event.keyCode and event.metaKey like this.

$(document).ready(function() {  $(".allow_only_numbers").keydown(function(e) {    // Allow: backspace, delete, tab, escape, enter and .    if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110, 190]) !== -1 ||      // Allow: Ctrl+A,Ctrl+C,Ctrl+V, Command+A      ((e.keyCode == 65 || e.keyCode == 86 || e.keyCode == 67) && (e.ctrlKey === true || e.metaKey === true)) ||      // Allow: home, end, left, right, down, up      (e.keyCode >= 35 && e.keyCode <= 40)) {      // let it happen, don't do anything      return;    }    // Ensure that it is a number and stop the keypress    if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {      e.preventDefault();    }  });});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><input type="number" class="allow_only_numbers" />

How to detect CTRL+V in javascript with Cyrillic layout?

This works fo me on ubuntu too(FF 5):

.keypress(function(e){
if ( e.ctrlKey && (e.which == 86 || e.which==118) ) {
console.log ('ctrl+v!');
}
});

for ctrl+n use

 if ( e.ctrlKey && (e.which == 110 || e.which==78) ) 

and for ctrl+t:

if ( e.ctrlKey && (e.which == 84 || e.which==116) )

How to differentiate between the native Ctrl+C and Ctrl+V and the document level listeners added using Javascript

Instead of using the keyboard events to handle the copy and paste. Simply use the clipboard events to handle them as follows:

@HostListener('document:copy', ['$event'])
copy(evt: ClipboardEvent) {
let folder = this.folder.json;
let item = folder.activeItem;
this.clipboard.copy(folder, item);
}

@HostListener('document:paste', ['$event'])
paste(evt: ClipboardEvent) {
this.clipboard.paste(this.finder.activeFolder);
}

Note: The Clipboard copy events are not supported in IE11.

Hope this helps.



Related Topics



Leave a reply



Submit