views:

251

answers:

1

I have a custom CMS and would like to add a "shortcuts menu" triggered by the pressing of the Ctrl key twice within, say, 300 milliseconds. I use prototype, so my starting point obviously is:

Event.observe(document, 'keypress', function(event)
  { if(event.keyCode == Event.KEY_XYZ) { show_shortcuts});

My approach at the moment would be populating a global variable with the current time in milliseconds, and checking on each keypress whether a keypress has happened less than 300 milliseconds ago.

But maybe there is a more elegant solution?

+1  A: 

This should work. Maybe add some further checking if not some other key like Alt or Shift are pressed at the same time. Hope it is self explanatory, if not just ask and I provide clarification.

var dblCtrlKey = 0;
Event.observe(document, 'keydown', function(event) {
  if (dblCtrlKey != 0 && event.keyCode == 17) {
    alert("Ok double ctrl");
  } else {
    dblCtrlKey = setTimeout('dblCtrlKey = 0;', 300);
  }
});
jitter
It works, however it fires also when the Ctrl key remains pressed for 300 milliseconds. I will have to add a "keyup" checker but this will serve as a basis for it. Cheers!
Pekka
It doesn't fire for me when key remains pressed. Might be browser specific (I'm using Opera)
jitter