最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Click Event Does not Trigger when holding down a key - Stack Overflow

programmeradmin4浏览0评论

I have a weird problem that is happening only on Chrome Windows 10, but not on Windows 11 or OSX. Im using Electron (nothing more than Javascript).

My goal is, when I click inside an iframe, I need to be able to detect if either "S" or "B"is being held down. The code below works perfectly fine on latest Chrome (OSX and Windows 11). For some weird reason, I cant get the console log click to print and execute the function. It seems that when either "s" or "b" is held down, the clickEventHandler does not trigger (it does not print the console log message) on Windows 10.

window.is_key_down = (() => {
    window.keystate = {};
    window.ifra = document.querySelector('iframe');
    ifra.contentDocument.body.addEventListener('keyup', (e) => keystate[e.key] = false);
    ifra.contentDocument.body.addEventListener('keydown', (e) => keystate[e.key] = true);
    return (key) => keystate.hasOwnProperty(key) && keystate[key] || false;
})();

var clickEventHandler = function(){
    console.log('Click On Iframe Detected')
    //Can also Replace with is_key_down("s")
    if(keystate.s){
        window.placeAD("side1")
        //keystate={}
    }
    //Can also Replace with is_key_down("b")
    if(keystate.b){
        window.placeAD("side2")
        //keystate={}
    }
}

var iframe = document.querySelector('iframe');
iframe.contentDocument.body.addEventListener('click',clickEventHandler);

I am more than puzzled, so perhaps a different approach to work on both would be great.

I have tried many different combinations and other tricks, but the clickEventHandler is not triggered if "s" or "b" are being held down when clicking. If no keys are being held down, the console log prints just fine.

Thanks in advance!!

发布评论

评论列表(0)

  1. 暂无评论