从Windows应用商店应用中的keydown事件调用时,不会触发execCommand

时间:2013-04-20 10:32:32

标签: windows html5 windows-store-apps onkeydown

我正在编写一个包含一些简单的富文本编辑的Windows应用商店应用(HTML)。我可以使用触发document.execCommand("bold",false,null);

的按钮将粗体应用于当前选中

然而,当我将它绑定到像CTRL + B这样的keydown事件时,没有任何反应。这是我的keydown代码。

document.addEventListener("keydown", catchShortCuts, false);

function catchShortCuts(e) {
    if (e.ctrlKey) {
        if (e.keyCode == 66) // b
            document.execCommand('bold', true, null);
        }
    }
}

我知道我的keydown代码工作正常,因为如果我用另一行代码替换document.execCommand,当我按下CTRL + B时​​它会触发。看来execCommand有关于keydown事件的问题吗?

1 个答案:

答案 0 :(得分:2)

事实证明,如果您使用 keypress 而不是 keydown ,它可以正常工作。为了防止其他人遇到同样的问题,这是一种解决方法。仍然不确定为什么onkeydown不起作用。

工作代码:

document.addEventListener("keypress", catchShortCuts, false);

function catchShortCuts(e) {
    if (e.ctrlKey) {
        if (e.keyCode == 66) // b
            document.execCommand('bold', true, null);
        }
    }
}