使用ESC键进入全屏

时间:2013-10-19 13:27:04

标签: javascript google-chrome fullscreen

所有按键都可以用来进入全屏但是看起来不是ESC的一个(它进入全屏但是之后立即退出,看起来像preventDefault没有帮助)б任何想法?

function onkeydownP(b) 
{
    switch (b.keyCode) 
    {
    case 27: // KeyEvent.DOM_VK_ESC
        var el = document.documentElement
        var rfs = el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen;
        rfs.call(el);
        b.preventDefault();
        break;
    }
}
window.addEventListener("keydown", onkeydownP, true);

编辑:将代码中的键码修正为右键

1 个答案:

答案 0 :(得分:1)

将事件键更改为27(ESC)。有用的资源http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

此外,您在el声明结束时遗漏了一个分号。将您的代码更改为:

function onkeydownP(b) 
{
    switch (b.keyCode) 
    {
    case KeyEvent.DOM_VK_DOWN:
        var el = document.documentElement;
        var rfs = (el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen);
        rfs.call(el);
        cancelEvent(b);
        break;
    case 27: // KeyEvent.DOM_VK_ESC
        var el = document.documentElement;
        var rfs = (el.requestFullScreen || el.webkitRequestFullScreen || el.mozRequestFullScreen);
        rfs.call(el);
        b.preventDefault();
        cancelEvent(b);
        break;
    }
}