Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
¿Puede jQuery .keypress() detectar más de una tecla al mismo tiempo?

¿Hay alguna forma de que jQuery detecte que se presionó más de una tecla al mismo tiempo?

¿Existe alguna alternativa que permita que se detecte la pulsación de dos teclas a la vez?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Para detectar varias teclas que se mantienen presionadas, use los eventos keydown y keyup .

 var keys = {}; $(document).keydown(function (e) { keys[e.which] = true; }); $(document).keyup(function (e) { delete keys[e.which]; });

He creado una demostración aquí: http://jsfiddle.net/gFcuU/ . Es un poco divertido, aunque noté que mi teclado solo puede detectar un máximo de 6 teclas.

over 4 years ago · Santiago Trujillo Report

0

Depende. Para las teclas "normales", eso significa Non- Shift , Ctrl , ALT , ( CMD ), la respuesta es no, el controlador de eventos se activará en una cola, uno tras otro.

Para las teclas modificadoras que mencioné anteriormente, hay una propiedad en el objeto de evento.

Ejemplo:

 $(document).bind('keypress', function(event) { if( event.which === 65 && event.shiftKey ) { alert('you pressed SHIFT+A'); } });

Demostración de jsfiddle .

Otras propiedades son:

  • event.ctrlKey
  • event.altKey
  • event.metaKey
over 4 years ago · Santiago Trujillo Report

0

Si solo desea activar un controlador cuando se presionan varias teclas en serie, intente algo como:

 jQuery.multipress = function (keys, handler) { 'use strict'; if (keys.length === 0) { return; } var down = {}; jQuery(document).keydown(function (event) { down[event.keyCode] = true; }).keyup(function (event) { // Copy keys array, build array of pressed keys var remaining = keys.slice(0), pressed = Object.keys(down).map(function (num) { return parseInt(num, 10); }), indexOfKey; // Remove pressedKeys from remainingKeys jQuery.each(pressed, function (i, key) { if (down[key] === true) { down[key] = false; indexOfKey = remaining.indexOf(key); if (indexOfKey > -1) { remaining.splice(indexOfKey, 1); } } }); // If we hit all the keys, fire off handler if (remaining.length === 0) { handler(event); } }); };

Por ejemplo, para disparar en st,

 jQuery.multipress([83, 84], function () { alert('You pressed s-t'); })
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!