![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
event-from
Advanced tools
Determine if a browser event was caused by mouse
, touch
, or key
input. Can be used to:
mouse
events caused by touch
input.focus
was initiated from the keyboard (to know when to add focus styles).click
event was from mouse
, touch
, or key
input.Code is in the /demo
folder.
npm install --save event-from
import { eventFrom } from 'event-from';
const handleEvent = (event) => {
// call eventFrom in the event handler and pass in the event
// eventFrom will return 1 of 3 strings: 'mouse' | 'touch' | 'key'
const eventFromValue = eventFrom(event);
// ...your logic using the eventFromValue
};
mouse
events caused by touch
inputNote that a touch interaction will fire Touch Events as the interaction is in progress (touch on the screen), and will fire Mouse Events during a long press (extended touch on the screen), or after the touch interaction has finished (after the touch is removed from the screen) to support sites that only listen for Mouse Events.
import { eventFrom } from 'event-from';
const handleMouseEvent = (e) => {
// early return to ignore mouse events not from mouse input
if (eventFrom(e) !== 'mouse') return;
// code for handling mouse events from mouse input
};
element.addEventListener('mouseenter', handleMouseEvent, false);
focus
event was from key
input to add focus stylesimport { eventFrom } from 'event-from';
const handleFocusEvent = (e) => {
if (eventFrom(e) === 'key') {
// add focus styles when focus is from keyboard input
}
};
element.addEventListener('focus', handleFocusEvent, false);
click
event was from mouse
, touch
, or key
inputimport { eventFrom } from 'event-from';
const handleClickEvent = (e) => {
switch (eventFrom(e)) {
case 'mouse':
// click event from mouse
break;
case 'touch':
// click event from touch
break;
case 'key':
// click event from key
break;
}
};
element.addEventListener('click', handleClickEvent, false);
setEventFrom(value)
value: 'mouse' | 'touch' | 'key'
Temporarily set the return value for eventFrom(e)
. This is useful when manually generating events, for example calling el.focus()
or el.click()
, and you want eventFrom(e)
to treat that event as occurring from a specific input.
import { eventFrom, setEventFrom } from 'event-from';
const handleFocusEvent = (e) => {
if (eventFrom(e) === 'key') {
// add focus styles when focus is from keyboard input
}
};
const element = document.getElementById('focus-example');
element.addEventListener('focus', handleFocusEvent, false);
// somewhere in your code where you want to call focus on the element
// and have it be treated as an event from 'key' input,
// now the call to eventFrom(e) in handleFocusEvent will return 'key'
setEventFrom('key');
element.focus();
Event From sets passive capture phase event listeners on the document
and window
and tracks the recent event history to know what input type is responsible for the event that's passed to eventFrom(event)
.
The listeners that Event From sets are all low frequency event listeners (enter/leave/down/up/focus/etc). Event From does not set any high frequency listeners such as move
or scroll
listeners.
FAQs
Determine if an event was caused by mouse, touch or key input.
We found that event-from demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.