我使用javascript使键盘输入大写。
例如,如果我键入小写的'a',则window.event.keycode为97。
有没有人知道下面的代码在Chrome中是否有效。在IE中工作正常
window.event.keycode = 97 - 32;
应为大写“A”
发布于 2010-01-09 08:06:56
小写字母与大写字母具有相同的键代码。您必须结合使用keydown和keyup来判断用户是否按下了shift键来生成字母。我最近用jquery写了一个脚本:
var shiftDown = false;
var outString;
$(document).ready(function() {
$(document).keydown(function(event) {
if (event.keyCode == 16)
shiftDown = true;
});
$(document).keyup(function(event){
if (event.keyCode != 16)
{
if (shiftDown)
{
outString += (String.fromCharCode(event.keyCode)).toUpperCase();
}
else
{
outString += String.fromCharCode(event.keyCode);
}
}
shiftDown = false;
});有趣的是,根据事件的不同,键码有时会有所不同。下面是我发现非常有用的测试页面:http://asquare.net/javascript/tests/KeyCode.html
https://stackoverflow.com/questions/2031545
复制相似问题