Detecting double keystroke Ctrl in JS

I have a custom CMS and I would like to add a “shortcut menu” called by double-clicking Ctrlfor, say, 300 milliseconds. I am using a prototype, so my starting point is obviously this:

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

At the moment, my approach is to populate the global variable with the current time in milliseconds and check each time the key is pressed, whether the key was pressed less than 300 milliseconds ago.

But maybe there is a more elegant solution?

+4
source share
1 answer

. , , - , Alt Shift. , , , .

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);
  }
});
+6

Source: https://habr.com/ru/post/1722440/


All Articles