窗口焦点事件ctrlkey undefined

时间:2013-12-11 12:38:59

标签: javascript jquery events focus undefined

我有一个活动:

$(window).on("focus", function(e){
    console.log(e);
});

当我关注谷歌浏览器并按ctrlKey时,我的控制台日志会写入:

jQuery.Event {
    altKey: undefined
    bubbles: false
    cancelable: false
    ctrlKey: undefined
    currentTarget: Window
    data: undefined
    delegateTarget: Window
    eventPhase: 2
    handleObj: Object
    isDefaultPrevented: function returnFalse() {
    jQuery19108790766424499452: true
    metaKey: false
    originalEvent: Event
    relatedTarget: undefined
    shiftKey: undefined
    target: Window
    timeStamp: 1386764900891
    type: "focus"
    view: undefined
    which: undefined
}

为什么我按下它并在谷歌的窗口同时聚焦时未定义CtrlKey?

1 个答案:

答案 0 :(得分:1)

如果符合您的需要,请尝试解决方法:

DEMO

$(window).on("focus", windFocus);

function windFocus(){    
    $(this).one('keydown',checkCtrlKey).delay(100).queue(function(next){
        $(this).off('keydown',checkCtrlKey); next();
    });
    this.timeout = setTimeout(checkCtrlKey,100);
}

function checkCtrlKey(e){
    clearTimeout(this.timeout);
    if(!e || !e.ctrlKey) console.log("Ctrl NOT pressed!");
    else
     console.log("Ctrl IS pressed!");
}