因为input无法抓取ctrl,shift,alt事件所以
我通过页面window的event来获取这3个键,然后因为keypress会导致ALT+SHIFT加输入内容成为一个特殊字符,所以不能使用,本人新手,因为不知道为什么which获取到的F1值是“p”,所以我用keyCode转意了一下。希望大神们知道原因的告诉我怎么解决谢谢。如果大神们有好的实现方法希望能给予指导。
< html >
< head >
< meta charset= "utf-8" >
< script type= "text/javascript" src= "jquery-2.1.1.js" ></ script >
< script type= "text/javascript" >
$ (document ).ready (function (){
$ ("input" ).keydown(function (){
var preHotkey = isKeyPressed (event );
var key = document .getElementById ('myinput' );
key .onkeyup = function (ev){
var nextKeyStr ;
if (ev.keyCode >111 &&ev.keyCode <124 ){
nextKeyStr = transCodeToF (ev.keyCode );
}else {
nextKeyStr = String.fromCharCode (ev.which );
}
preHotkey += nextKeyStr ;
//alert(ev.keyCode);
//alert(String.fromCharCode(ev.which));
//alert(preHotkey);
document .getElementById ('myinput' ).value = preHotkey ;
ev.returnValue = false ;
}
});
});
function isKeyPressed (event)
{
var hotKey = "" ;
if (event.shiftKey ==1 )
{
if (hotKey .length == 0 ){
hotKey = "shift + " ;
}
else {
hotKey += "shift + " ;
}
}
else
{
if (hotKey .length == 0 ){
hotKey = "" ;
}
else {
hotKey += "" ;
}
}
if (event.altKey ==1 )
{
if (hotKey .length == 0 ){
hotKey = "alt + " ;
}
else {
hotKey += "alt + " ;
}
}
else
{
if (hotKey .length == 0 ){
hotKey = "" ;
}
else {
hotKey += "" ;
}
}
if (event.ctrlKey ==1 )
{
if (hotKey .length == 0 ){
hotKey = "ctrl + " ;
}
else {
hotKey += "ctrl + " ;
}
}
else
{
if (hotKey .length == 0 ){
hotKey = "" ;
}
else {
hotKey += "" ;
}
}
hotKey ;
return hotKey ;
}
function transCodeToF (code){
switch (code)
{
case 112 :
return "F1" ;
break ;
case 113 :
return "F2" ;
break ;
case 114 :
return "F3" ;
break ;
case 115 :
return "F4" ;
break ;
case 116 :
return "F5" ;
break ;
case 117 :
return "F6" ;
break ;
case 118 :
return "F7" ;
break ;
case 119 :
return "F8" ;
break ;
case 120 :
return "F9" ;
break ;
case 121 :
return "F10" ;
break ;
case 122 :
return "F11" ;
break ;
case 123 :
return "F12" ;
break ;
default :
break ;
}
}
</ script >
</ head >
< body >
Enter your name: < input type= "text" id= "myinput" />
</ body >
</ html >