Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Whether an Event is Triggered by User and not programmatically

Tags:

Well this question has been asked before but in context of jQuery. In jQuery we can check it by originalEvent property of event Object (link) which tells whether its a manual or programmed event.

In my case I am using Javascript Event listeners and triggers. Can we differentiate between the two kind of events (programmed and manual) in this case?

If not then any workarounds?

My Listeners:

   function setUpListeners(){
       _cellViewWrapper.addEventListener('mousedown',mouseDownHandler,false);
       _cellViewWrapper.addEventListener('mouseover',mouseEnter,false);
       _cellViewWrapper.addEventListener('blur',blurHandler,true);
       _cellViewWrapper.addEventListener('focus',focusEventHandler,true);
   }`

Trigger use Cases:

  1. if(!IE_FLAG) hidePicker();
               //if browser is internet explorer
               else{
                   //if blur is allowed then hide Picker
                   if(_ieBlurAllowed) hidePicker();
                   //if blur is not allowed -- keep focus on picker input
                  //triggering the focus event here
                   else blurredElement.focus(); /
             }
    
  2. if((inputElem !== _focussedInput)) setTimeout(function(){ inputElem.focus(); },10);

and many more...

like image 755
bhavya_w Avatar asked Apr 22 '15 12:04

bhavya_w


People also ask

How do you know if an event is triggered?

To check if event is triggered by a human with JavaScript, we can use the isTrusted property. to check if the event is triggered by a human in our event handler callback. e is the event object and it had the isTrusted property. If isTrusted is true , then the event is triggered by a human.

How do you check if an event has been triggered in JavaScript?

$('button'). click(function(event, wasTriggered) { if (wasTriggered) { alert('triggered in code'); } else { alert('triggered by mouse'); } }); $('button').

What is event originalEvent?

event. originalEvent is usually just the native event (also described here). However, if the browser is compatible, and the event was a touch event then that API will be exposed through event. originalEvent . The short answer is that event.

What is the difference between event and trigger?

A Trigger is a set of conditions that cause an interaction to run. An event is a user- or administrator-initiated action that an administrator defines that must be met before an interaction is created. Triggers listen for events, and once that event occurs, can initiate an interaction defined within Journey Builder.


2 Answers

In latest browsers, Event.isTrusted is available for this particular use-case. As per its MDN document:

The isTrusted read-only property of the Event interface is a boolean that is true when the event was generated by a user action, and false when the event was created or modified by a script or dispatched via dispatchEvent.

You can simply check inside any event handler:

if (e.isTrusted) {
 /* The event is trusted. */
} else {
 /* The event is not trusted. */
}

Browser Support: Chrome 46.0, Firefox (latest), Opera 33, Safari & IE (no support)

like image 164
Zahin Alwa Avatar answered Sep 30 '22 05:09

Zahin Alwa


[Workaround] Javascript: Check if event.screenX & event.screenY are non-zero.

var button = document.getElementsByTagName('button')[0];

button.onclick = function(e) {
  if(e.screenX && e.screenX != 0 && e.screenY && e.screenY != 0){
         alert("real button click");
       }   
}

When the user clicks on button, it would have some real screenX and screenY based on the button position. But when you do it from code - it would be zero

like image 29
Vijay Avatar answered Sep 30 '22 06:09

Vijay