How to make Ctrl+V or Paste not trigger the keyup
function TWICE?
This is a problem for me because I made an AutoComplete functionality, and it displays the same data twice when i paste in a textbox. I hope this makes more sense now.
EDIT:
Okay guys, I have found out how to detect Ctrl+V by $('#this-id').bind('paste', function() {});
But now another follow up question is how to bine it with keyup
so that when paste
is pressed, keyup
wont trigger anymore.
How to make Ctrl+V or Paste not trigger the keyup
function TWICE?
This is a problem for me because I made an AutoComplete functionality, and it displays the same data twice when i paste in a textbox. I hope this makes more sense now.
EDIT:
Okay guys, I have found out how to detect Ctrl+V by $('#this-id').bind('paste', function() {});
But now another follow up question is how to bine it with keyup
so that when paste
is pressed, keyup
wont trigger anymore.
- 3 Why is it a problem for you? You do release two keys (V and CTRL) after all... – ThiefMaster Commented Apr 1, 2014 at 6:58
- 2 Do you have any trials respective to the question asked? actually this question does not make any sense. – Jai Commented Apr 1, 2014 at 6:59
-
Why don't you use
keydown
to trap theCtrl
key andkeyup
to trap theV
key? – CodingIntrigue Commented Apr 1, 2014 at 7:02 - okay! i have some issues like this before in the inclusion of same script twice, you can check for that. – Jai Commented Apr 1, 2014 at 7:03
- Then how about if 'V' is a part of the letters i am typing to search in an autoplete i made? @RGraham – user2881063 Commented Apr 1, 2014 at 7:04
3 Answers
Reset to default 15Okay guys, Thank you all for your answers but as I go through some reading, a lot of blogs say this: "IF you're implementing an autoplete functionality, DON'T rely on 'keyup' function"
So I changed my code to $('#this-id').bind('input', function() {});
And it worked, I don't have to worry now about pasting or anything else. I hope this helps to others too.
you can try this
$(window).on('keyup', function (event) {
if (!event.ctrlKey) {
/* here your code for all keys besides CTRL ;-) */
}
});
You could use underscore's debounce to set delay for reading the keyup event.
See the working code at:
JSFiddle
JS:
var count = 0;
function lookup() {
$('div#test').html($('input.text').val());
count++;
$('div#count').html(count);
}
$(document).keyup( _.debounce(lookup, 250, true) );
HTML:
<div>
<input type="text">
</div>
Input: <div id="test"></div>
Keyup Count: <div id="count"></div>
underscore.js from:
http://documentcloud.github./underscore/underscore-min.js