我需要找出从jQuerykeydown
函数调用的处理程序中将哪个字符键入到文本字段中。key.which
仅给我键码,但是我需要弄清楚哪个ASCII字符key
代表。我该怎么做呢?
Answers:
对于字符输入,建议您使用keypress()
,它将报告所按字符的实际ASCII码。它会自动处理字母大小写,并忽略非字符印刷。无论哪种情况,都可以使用fromCharCode()转换为字符串表示形式。例如
var c = String.fromCharCode(e.which) // or e.keyCode
只要记住forkeydown()
和keyup()
,就必须使用e.shiftKey
state来跟踪案例。
String
不是var ...:((
该keyPress
事件是您获取输入哪个字符所需要的。(有关keydown事件,请参见下面的解决方法)。
keydown
并keyup
提供指示按下哪个键的代码,同时keypress
指示输入哪个字符。
使用jQuerye.which
您可以获取键码,使用String.fromCharCode您可以获取被按下的特定字符(包括shiftKey)。
演示: http : //jsfiddle.net/9TyzP/3
码:
element.on ('keypress', function (e) {
console.log(String.fromCharCode(e.which));
})
请注意,我
e.which
之所以说jQuery是因为不同的浏览器使用不同的属性来存储此信息。jQuery规范化了该.which
属性,因此您可以可靠地使用它来检索键码。
解决方法 keydown
但是,您可以编写一个简单的解决方法以使按下的字符起作用keydown
。解决方法是创建一个键为charCode的对象,而无需使用shift keypress,而值使用shift键。
注意:正如@Sajjan Sarkar指出的那样,e.which
从不同的浏览器返回的键码值存在一些差异。在这里阅读更多
更新了DEMO代码以标准化跨浏览器的keyCode值。经过IE 8,FF和Chrome的测试和验证。
下面的完整代码和更新的DEMO: http : //jsfiddle.net/S2dyB/17/
$(function() {
var _to_ascii = {
'188': '44',
'109': '45',
'190': '46',
'191': '47',
'192': '96',
'220': '92',
'222': '39',
'221': '93',
'219': '91',
'173': '45',
'187': '61', //IE Key codes
'186': '59', //IE Key codes
'189': '45' //IE Key codes
}
var shiftUps = {
"96": "~",
"49": "!",
"50": "@",
"51": "#",
"52": "$",
"53": "%",
"54": "^",
"55": "&",
"56": "*",
"57": "(",
"48": ")",
"45": "_",
"61": "+",
"91": "{",
"93": "}",
"92": "|",
"59": ":",
"39": "\"",
"44": "<",
"46": ">",
"47": "?"
};
$(element).on('keydown', function(e) {
var c = e.which;
//normalize keyCode
if (_to_ascii.hasOwnProperty(c)) {
c = _to_ascii[c];
}
if (!e.shiftKey && (c >= 65 && c <= 90)) {
c = String.fromCharCode(c + 32);
} else if (e.shiftKey && shiftUps.hasOwnProperty(c)) {
//get shifted keyCode value
c = shiftUps[c];
} else {
c = String.fromCharCode(c);
}
//$(element).val(c);
}).on('keypress', function(e) {
//$(element).val(String.fromCharCode(e.which));
});
});
有关键盘事件的更多信息-
用户按下某个键时,将触发keydown,keypress和keyup事件。
KEYDOWN当用户按下一个键触发。在用户按住键的同时重复此操作。
按键时触发被插入在实际的字符,例如,文本输入。在用户按住键的同时重复此操作。
KEYUP时触发用户发布的关键,在已经执行该键的默认操作后。
首次按下键时,将keydown
发送事件。如果该键不是修饰键,keypress
则发送事件。当用户释放密钥时,将keyup
发送事件。
按住某个键时,它会自动重复。这将导致发生一系列类似于以下事件的事件:
keydown
keypress
keydown
keypress
<<repeating until the user releases the key>>
keyup
演示: http : //jsfiddle.net/9TyzP/1/
按键,按键与按键
keydown和keyup事件表示正在按下或释放的键,而keypress事件则表示正在键入的字符。
演示: http : //jsfiddle.net/9TyzP/
参考文献:
keyCode
(e.which)keydown/keyup/keypress
。如果需要ASCII字符,则可以使用String.fromCharCode
function。你没有得到这个角色keyup/keypress
。
Selvakumar Arumugam的答案对我来说就像一个魅力……直到我测试了数字键盘。所以这里有一个小更新:
$(document).on('keydown', function(e) {
var c = e.which;
if (_to_ascii.hasOwnProperty(c)) {
c = _to_ascii[c];
}
if (!e.shiftKey && (c >= 65 && c <= 90)) {
c = String.fromCharCode(c + 32);
} else if (e.shiftKey && shiftUps.hasOwnProperty(c)) {
c = shiftUps[c];
} else if (96 <= c && c <= 105) {
c = String.fromCharCode(c - 48);
}else {
c = String.fromCharCode(c);
}
$kd.val(c);
})
我创建并使用上述javascript类将gr转换为en字符。它可以用于更多语言。它使用JQuery更改用户按下的值。
var CharMapper = function (selector) {
this.getLanguageMapper = function (languageSource, languageTarget) {
// Check if the map is already defined.
if (typeof langugageCharMap === "undefined") {
langugageCharMap = {};
}
if (typeof langugageCharMap[languageSource] === "undefined") {
langugageCharMap[languageSource] = {};
}
// Initialize or get the language mapper.
if (typeof langugageCharMap[languageSource][languageTarget] === "undefined") {
switch (languageSource) {
case "GR":
switch (languageTarget) {
case "EN":
langugageCharMap[languageSource][languageTarget] = {
"α": "a", "ά": "a", "β": "b", "γ": "g", "δ": "d", "ε": "e", "έ": "e", "ζ": "z", "η": "h", "ή": "h", "θ": "th", "ι": "i", "ί": "i", "ϊ": "i", "ΐ": "i", "κ": "k", "λ": "l", "μ": "m", "ν": "n", "ξ": "ks", "ο": "o", "ό": "o", "π": "p", "ρ": "r", "σ": "s", "ς": "s", "τ": "t", "υ": "y", "ύ": "y", "ϋ": "y", "ΰ": "y", "φ": "f", "χ": "x", "ψ": "ps", "ω": "o", "ώ": "o", "Α": "A", "Ά": "A", "Β": "B", "Γ": "G", "Δ": "D", "Ε": "E", "Έ": "E", "Ζ": "Z", "Η": "H", "Ή": "H", "Θ": "TH", "Ι": "I", "Ί": "I", "Ϊ": "I", "Κ": "K", "Λ": "L", "Μ": "M", "Ν": "N", "Ξ": "KS", "Ο": "O", "Ό": "O", "Π": "P", "Ρ": "R", "Σ": "S", "Τ": "T", "Υ": "Y", "Ύ": "Y", "Ϋ": "Y", "Φ": "F", "Χ": "X", "Ψ": "PS", "Ω": "O", "Ώ": "O"
};
break;
case "GR":
default:
throw "Language(" + languageTarget + ") is not supported as target for Language(" + languageSource + ").";
}
break;
case "EN":
default:
throw "Language(" + languageSource + ") is not supported as source.";
}
}
return langugageCharMap[languageSource][languageTarget];
};
// Check the existance of the attribute.
var items = $(selector).find("*[data-mapkey]");
if (items.length === 0) {
return;
}
// For each item.
for (var i = 0; i < items.length; i++) {
var item = items[i];
// Get the source and target language.
var languages = $(item).attr("data-mapkey");
var languageSource = languages.split("_")[0];
var languageTarget = languages.split("_")[1];
// Add the event listener.
var self = this;
$(item).keypress(function (event) {
event.stopPropagation();
// Get the mapper to use.
var mapper = self.getLanguageMapper(languageSource, languageTarget);
// Get the key pressed.
var keyPressed = String.fromCharCode(event.which);
// Get the key to set. In case it doesn't exist in the mapper, get the key pressed.
var keyToSet = mapper[keyPressed] || keyPressed;
// Set the key to the dom.
this.value = this.value + keyToSet;
// Do not propagate.
return false;
});
}
};
例,
<input type="text" data-mapkey="GR_EN" />
<script type="text/javascript">
new CharMapper("body");
</script>