📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials jQuery in Practice Keyboard Events

Keyboard Events

4 min read
Listen for keyboard events with on('keydown', fn). Use e.key for the key name like 'Enter' or 'Escape'. Combine with e.ctrlKey or e.shiftKey for shortcuts. Avoid the deprecated e.which and e.keyCode properties.

Keyboard Events

$(document).on('keydown', function(e) {
  console.log(e.key, e.keyCode);
  if (e.key === 'Escape') closeModal();
  if (e.ctrlKey && e.key === 's') saveForm();
});

$('input').on('keyup', function() {
  // live search
  const q = $(this).val();
  filterList(q);
});