开发者

Is it possible to detect ctrl key state on dblclick event

开发者 https://www.devze.com 2023-01-20 16:10 出处:网络
I am using Google maps API version 3.I would like my double click event handler to work as follows: google.maps.event.addListener(map, \'dblclick\', function(e) {

I am using Google maps API version 3. I would like my double click event handler to work as follows:

google.maps.event.addListener(map, 'dblclick', function(e) {
  if (/* ctrl is pressed */) {
    d开发者_StackOverflow社区oSomething(e)
  } else {
    doSomethingElse(e)
  }
});

It looks the the event handler only provides a MouseEvent which does not contain information about the keyboard state. I need the LatLng information so I doubt that I can use JQuery's event handling.

Am I out of luck here?


You could just cache the control key state

var ctrlPressed = false;
function cacheIt(event) {
    ctrlPressed = event.ctrlKey;
}
document.onkeydown = cacheIt;
document.onkeyup = cacheIt;

Now, ctrlPressed should always reflect whether or not the control key is down.


When caching key presses, I always bind a function to the blur event of the window object which clears any cached key presses. Otherwise you get stuck keys if the window loses focus.

Here's an example using jQuery:

$(window).blur( function () {
  // Clear cached key presses here
})
0

精彩评论

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