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

265
Views
How to detect if the event that is generated belongs to Keyboard, Pointer or Form Event?

I have developing an application that records the events and replays them. For that I need to identify what kind of event is being generated because mouse, keyboard and form events behave differently from each other.

Right now i am trying to use: e instanceof KeyboardEvent but this doesn't seems to be working. What is the better way of identifying to which event family it belongs to?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

A basic example for a mouse and keyboard event. You just have to add an eventListener to your desired dom element. And then you have to check if the triggered event e is an instance of MouseEvent or if it is a KeyboardEvent.

const button = document.getElementById('mouse');

button.addEventListener('mousedown', (e) => {
  if (e instanceof MouseEvent) {
    console.log('a mouse event');
  }
});

const inputField = document.getElementById('keyboard');

inputField.addEventListener('keydown', (e) => {
  if (e instanceof KeyboardEvent) {
    console.log('a keyboard event');
  }
});
<button id="mouse">MouseButton</button>
<input id="keyboard">

about 4 years ago · Juan Pablo Isaza Report

0

Using the event.detail allow you to determine if the event was a keypress or mouse event

if (event.detail === 0) {
    // keypress event
} else {
    // mouse event
}

Read more here - https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/detail

about 4 years ago · Juan Pablo Isaza 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!