Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在网站上实现键盘功能快捷键?_Javascript_Events_Event Handling - Fatal编程技术网

Javascript 如何在网站上实现键盘功能快捷键?

Javascript 如何在网站上实现键盘功能快捷键?,javascript,events,event-handling,Javascript,Events,Event Handling,我想在我的网站中使用键盘功能键。那么我如何在我的网站上实现键盘功能快捷键呢。感谢大家……使用Javascript: // event.type should be keypress function getChar(event) { if (event.which == null) { // IE if (event.keyCode < 32) return null; // special character return String.

我想在我的网站中使用键盘功能键。那么我如何在我的网站上实现键盘功能快捷键呢。感谢大家……

使用Javascript:

// event.type should be keypress
function getChar(event) {
    if (event.which == null) {  // IE
        if (event.keyCode < 32) return null; // special character
            return String.fromCharCode(event.keyCode) 
    } 

    if (event.which != 0 && event.charCode != 0) { // not IE
        if (event.which < 32) return null; // special character
            return String.fromCharCode(event.which); // other
    } 

    return null; // special character
}
//event.type应为keypress
函数getChar(事件){
if(event.which==null){//IE
if(event.keyCode<32)返回null;//特殊字符
返回字符串.fromCharCode(event.keyCode)
} 
如果(event.which!=0&&event.charCode!=0){//不是IE
if(event.which<32)返回null;//特殊字符
返回字符串.fromCharCode(event.which);//其他
} 
返回null;//特殊字符
}
控制键使用:event.shiftKey、event.ctrlKey、event.altKey或event.metaKey。

使用Javascript:

// event.type should be keypress
function getChar(event) {
    if (event.which == null) {  // IE
        if (event.keyCode < 32) return null; // special character
            return String.fromCharCode(event.keyCode) 
    } 

    if (event.which != 0 && event.charCode != 0) { // not IE
        if (event.which < 32) return null; // special character
            return String.fromCharCode(event.which); // other
    } 

    return null; // special character
}
//event.type应为keypress
函数getChar(事件){
if(event.which==null){//IE
if(event.keyCode<32)返回null;//特殊字符
返回字符串.fromCharCode(event.keyCode)
} 
如果(event.which!=0&&event.charCode!=0){//不是IE
if(event.which<32)返回null;//特殊字符
返回字符串.fromCharCode(event.which);//其他
} 
返回null;//特殊字符
}

对于控制键,请使用:event.shiftKey、event.ctrlKey、event.altKey或event.metaKey。

好。如果您不知道jquery,我建议您先学习javascript。 键盘上的每个键都有按键代码。您只需检测按下哪个键的按键,并根据按下哪个键添加游戏功能。

总而言之-->

为了你想要的-->


好的。如果你不懂jquery,我建议你先学习javascript。 键盘上的每个键都有按键代码。您只需检测按下哪个键的按键,并根据按下哪个键添加游戏功能。

总而言之-->

为了你想要的-->


如果您愿意使用jQuery,那么它可能会更简单一些

为了能够捕获复合键,您必须首先查找控制键上的keydown并存储状态。然后在常规密钥上查找keydown,并对照存储状态进行检查。一旦有钥匙接通,重置控制状态

下面是一个如何做到这一点的示例:

// Bind keyup to document to trap shortcut keys for various operations
$(document).keyup(function (e) {
    IsCtrl = false;
    IsShift = false;
}).keydown(function (e) {

    // first capture Ctrl 
    if (e.which == 17) { IsCtrl = true; }

    // now capture Shift 
    if (e.which == 16) { IsShift = true; }

    switch (e.which) {

        // now capture S and if Ctrl is pressed                                                                                                                                                                                          
        case 83: 
            if (IsCtrl) { alert("Ctrl S pressed");  } 
            if (IsShift) { alert("Shift S pressed");  } 
            e.preventDefault(); 
            break;

        // capture F1                                                                                                                                                                                           
        case 112: alert("Heellllppp!"); e.preventDefault(); break;

        // capture ESC                                                                                                                                                                                           
        case 27: alert("Escape!"); e.preventDefault(); break;
    }
});
其中“
IsCtrl
”和“
IsShift
”是全局范围内的变量。上述代码专门检查控制键。如果要检查其他组合(包括),请更改要检查控制键状态的部分。功能键从112(f1)开始。您可以参考@MESSIAH发布的图表来获取其他关键代码


希望对您有所帮助。

如果您愿意使用jQuery,那么它可能会简单一点

为了能够捕获复合键,您必须首先查找控制键上的keydown并存储状态。然后在常规密钥上查找keydown,并对照存储状态进行检查。一旦有钥匙接通,重置控制状态

下面是一个如何做到这一点的示例:

// Bind keyup to document to trap shortcut keys for various operations
$(document).keyup(function (e) {
    IsCtrl = false;
    IsShift = false;
}).keydown(function (e) {

    // first capture Ctrl 
    if (e.which == 17) { IsCtrl = true; }

    // now capture Shift 
    if (e.which == 16) { IsShift = true; }

    switch (e.which) {

        // now capture S and if Ctrl is pressed                                                                                                                                                                                          
        case 83: 
            if (IsCtrl) { alert("Ctrl S pressed");  } 
            if (IsShift) { alert("Shift S pressed");  } 
            e.preventDefault(); 
            break;

        // capture F1                                                                                                                                                                                           
        case 112: alert("Heellllppp!"); e.preventDefault(); break;

        // capture ESC                                                                                                                                                                                           
        case 27: alert("Escape!"); e.preventDefault(); break;
    }
});
其中“
IsCtrl
”和“
IsShift
”是全局范围内的变量。上述代码专门检查控制键。如果要检查其他组合(包括),请更改要检查控制键状态的部分。功能键从112(f1)开始。您可以参考@MESSIAH发布的图表来获取其他关键代码


希望对您有所帮助。

您可以使用javascript。您能详细说明“键盘功能快捷键”的含义吗?您想在您的网站上创建快捷键,如“Ctrl+N”或“Alt+N”吗?为此,您可以使用javascript。您能否详细说明“键盘功能快捷键”的含义?你想在你的网站上创建快捷键,如“Ctrl+N”或“Alt+N”吗?我想在我的网站上使用功能键,如Ctrl、shift、F1、F2等………我想在我的网站上使用功能键,如Ctrl、shift、F1、F2等………谢谢大家。谢谢大家。