Created
August 24, 2016 21:05
-
-
Save raphaelchaib/9877e1df321e23b2f820f155a4a1c458 to your computer and use it in GitHub Desktop.
JavaScript: Detects what mouse button was used, left, right, middle, or middle scroll either direction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Better Javascript Mouse Events | |
| Author: Casey Childers | |
| https://jsfiddle.net/BNefn/ | |
| **/ | |
| (function(){ | |
| // use addEvent cross-browser shim: https://gist.github.com/dciccale/5394590/ | |
| var addEvent = function(a,b,c){try{a.addEventListener(b,c,!1)}catch(d){a.attachEvent('on'+b,c)}}; | |
| /* This function detects what mouse button was used, left, right, middle, or middle scroll either direction */ | |
| function GetMouseButton(e) { | |
| e = window.event || e; // Normalize event variable | |
| var button = ''; | |
| if (e.type == 'mousedown' || e.type == 'click' || e.type == 'contextmenu' || e.type == 'mouseup') { | |
| if (e.which == null) { | |
| button = (e.button < 2) ? "left" : ((e.button == 4) ? "middle" : "right"); | |
| } else { | |
| button = (e.which < 2) ? "left" : ((e.which == 2) ? "middle" : "right"); | |
| } | |
| } else { | |
| var direction = e.detail ? e.detail * (-120) : e.wheelDelta; | |
| switch (direction) { | |
| case 120: | |
| case 240: | |
| case 360: | |
| button = "up"; | |
| break; | |
| case -120: | |
| case -240: | |
| case -360: | |
| button = "down"; | |
| break; | |
| } | |
| } | |
| var type = e.type | |
| if(e.type == 'contextmenu') {type = "click";} | |
| if(e.type == 'DOMMouseScroll') {type = "mousewheel";} | |
| switch(button) { | |
| case 'contextmenu': | |
| case 'left': | |
| case 'middle': | |
| case 'up': | |
| case 'down': | |
| case 'right': | |
| if (document.createEvent) { | |
| event = new Event(type+':'+button); | |
| e.target.dispatchEvent(event); | |
| } else { | |
| event = document.createEventObject(); | |
| e.target.fireEvent('on'+type+':'+button, event); | |
| } | |
| break; | |
| } | |
| } | |
| addEvent(window, 'mousedown', GetMouseButton); | |
| addEvent(window, 'mouseup', GetMouseButton); | |
| addEvent(window, 'click', GetMouseButton); | |
| addEvent(window, 'contextmenu', GetMouseButton); | |
| /* One of FireFox's browser versions doesn't recognize mousewheel, we account for that in this line */ | |
| var MouseWheelEvent = (/Firefox/i.test(navigator.userAgent)) ? "DOMMouseScroll" : "mousewheel"; | |
| addEvent(window, MouseWheelEvent, GetMouseButton); | |
| })(); | |
| /* Better Mouse Click Events Example (uses jquery for simplicity, but the above will work cross browser and file the same event names, IE uses on before the names) */ | |
| $('#Test').on('mouseup',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mouseup:left',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mouseup:middle',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mouseup:right',function(e){$(this).append(e.type+'<br />');}) | |
| .on('click',function(e){$(this).append(e.type+'<br />');}) | |
| .on('click:left',function(e){$(this).append(e.type+'<br />');}) | |
| .on('click:middle',function(e){$(this).append(e.type+'<br />');}) | |
| .on('click:right',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mousedown',function(e){$(this).html('').append(e.type+'<br />');}) | |
| .on('mousedown:left',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mousedown:middle',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mousedown:right',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mousewheel',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mousewheel:up',function(e){$(this).append(e.type+'<br />');}) | |
| .on('mousewheel:down',function(e){$(this).append(e.type+'<br />');}) | |
| ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment