[英]Change character when keydown is held
I am trying to make a simple way to change a character in an input field when the key is held down for more than 1second. 我试图提供一种简单的方法来在按住键1秒钟以上时更改输入字段中的字符。 For example holding down a would then change the character to á .
例如,按住a会将字符更改为á 。 The exact thing I am looking to do can be seen on fluencia.com.
我想要做的确切事情可以在fluencia.com上找到。 Also there is a need to be able to change the character if it is held for a further second.
另外,如果将字符再按住一秒钟,则需要能够更改该字符。
So far, all I have done is detect the key held with the following code: 到目前为止,我所要做的就是检测使用以下代码保存的密钥:
count = 0;
$(document).bind('keypress', function(e){
keyisdown = false;
key = e.which
if (e.which === key) {
keyisdown = true;
count ++;
if(count>1){
}
}
}).bind('keyup',function(){
keyisdown = false;
count = 0;
console.log('key up');
});
Thanks Adam 谢谢亚当
This should do it for key "a".. You'll need to look up your keycode 这应该针对键“ a”执行。.您需要查找您的键代码
var fired = false;
$(document).on("keydown", function(e) {
if (e.keyCode == 65) {
var timer = setTimeout(function() {
if (!fired) console.log('its held');
fired = true;
}, 1000);
$(document).on("keyup", function() {
clearTimeout(timer);
});
}
});
fiddle - http://jsfiddle.net/0a9rftt2/ 小提琴-http: //jsfiddle.net/0a9rftt2/
With help from Graham T above and a bit of playing around I come up with the following: 在上述Graham T的帮助下,并进行了一些尝试,我提出了以下建议:
var fired = false;
var keycode = null;
var key = null;
$("#loginEmail").on("keypress", function(e) {
keycode = e.which;
key = String.fromCharCode(keycode);
});
$("#loginEmail").on("keydown", function(e) {
var s = this.value;
var str = this.value;
if (e.keyCode == 65) {
var timer = setTimeout(function() {
if (!fired) console.log('its held');
fired = true;
if(fired){
s = $("#loginEmail").val();
str = s.substring(0, s.length - 1);
replacewith = 'á';
str = str+replacewith;
$("#loginEmail").val(str);
}
}, 500);
$(document).on("keyup", function() {
clearTimeout(timer);
fired = false;
});
}
});
It is not cleaned up as yet. 尚未清除。 I had to use keypress to get the true character (capitalised or not) and then used keydown to detect key being held down.
我必须使用keypress来获取真实字符(是否大写),然后使用keydown来检测被按下的键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.