开发者

Keypress check (onkeyup isn't triggered)

I've coded the following functions to switch the content for divs using keypresses. Basically I display 2 images, which are being changed to another onkeydown. If onkeyup, the image will be restored.

It works beautifully, as long as only one key is being pressed. If I make The keypress shortcut [224+86] (Meta+V), the key released last "stays" onkeydown (no onkeyup is generated for some reason).

//CMD
$(document).keydown(function (e) {
    if (e.keyCode == 224)
        document.getElementById("loading").innerHTML = '<div style="position: absolute; left: -30px; top: 8px; width: 600px; height:600px; padding: 0px; border: 0px; z-index:200; padding:158px;"><img src=开发者_StackOverflow"img/mac/1_b.png" /></div>';    
});

$(document).keyup(function (e) {
    if (e.keyCode != 224)
        document.getElementById("loading").innerHTML = '<div style="position: absolute; left: -30px; top: 8px; width: 600px; height:600px; padding: 0px; border: 0px; z-index:200; padding:158px;"><img src="img/mac/1.png" /></div>';
});

//V
$(document).keydown(function (e) {
    if (e.keyCode == 86)
        document.getElementById("loading2").innerHTML = '<div style="position: absolute; left: 95px; top: 9px; width: 600px; height:600px; padding: 0px; border: 0px; z-index:200; padding:158px;"><img src="img/mac/2_b.png" /></div>';    
});

$(document).keyup(function (e) {
    if (e.keyCode == 86)
        document.getElementById("loading2").innerHTML = '<div style="position: absolute; left: 95px; top: 9px; width: 600px; height:600px; padding: 0px; border: 0px; z-index:200; padding:158px;"><img src="img/mac/2.png" /></div>';    
});

Can anyone please point me in the right direction? Thanks so much for any hint!


I think the problem is that you are separating these out too much. You should combine them all into the same event handler and see what happens. You should combine the logic too.

Instead of doing this many times:

$(document).keydown(function (e) {
    if (e.keyCode == 224)
        document.getElementById("loading").innerHTML = '....';    
});

Try combining the logic into one callback event:

$(document).keydown(function (e) {
    if (e.keyCode == 224)
        document.getElementById("loading").innerHTML = '....'; 
    if (e.keyCode == 86)
        document.getElementById("loading2").innerHTML = '....';   
});

Also - are you sure you have the correct key for the CMD key? I think it is a little more difficult to detect than what you are doing as per here - http://www.quirksmode.org/js/keys.html. Take a look here: jQuery key code for command key for some suggestions on how to use the CMD key.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜