简体   繁体   中英

How to detect whether shift was pressed during onclick handler?

I have the handler:

myObj[id] = new google.maps.Circle({/*Initialization*/}
myObj[id].addListener('click', function (event) {
     //Some code here
 myFunction(this, event);
}

And the function that does not work.

myFunction(elem, event) {
    console.log(event);
    if(!event.shiftKey) { //Do Something }
}    

As you can see from the image below, the qZ object has the Aa Attribute? which is responsible for the MouseEvents , right ?

错误

Well, if I change my function to:

myFunction(elem, event) {
    if(!event.Aa.shiftKey) { //Do Something }
}  

It works. The problem is, this Aa once were Va and Xa before that. I can't keep chaning all the time. I have no idea why this happens nor how to fix it.

It's detecting the click is calling the function normally, but the shiftKey is ignored. How could I detect the shiftKey on click event ?

Update:

Following the tip gave by Dan, I tried this to loop through the object and try to find the shiftKey :

var shiftKey;
      if (Object.keys(event).some(function (key) {
            if (event[key] && 'shiftKey' in event[key]) {
                  shiftKey = event[key].shiftKey;
                  return true;
            }
            return false;
      })) {
            // We found it, `shiftKey` has the value. Do nothing
      } else {
            //If no shiftKey is pressed execute my code!
      }

But now it never execute my code that should be trigger when shiftKey is NOT pressed...

From your update I see a mistake:

var shiftKey;
      if (Object.keys(event).some(function (key) {
            if (event[key] && 'shiftKey' in event[key]) {
                  shiftKey = event[key].shiftKey;
                  return true; // <- should be return shiftKey
            }
            return false;
      })) {
            // We found it, `shiftKey` has the value. Do nothing
      } else {
            //If no shiftKey is pressed execute my code!
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM