├── .gitignore ├── LICENSE ├── README.md ├── example └── example.js ├── mouse.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | lib-cov 2 | *.seed 3 | *.log 4 | *.csv 5 | *.dat 6 | *.out 7 | *.pid 8 | *.gz 9 | 10 | pids 11 | logs 12 | results 13 | 14 | npm-debug.log 15 | node_modules/* 16 | *.DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2015 Mikola Lysenko 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # mouse-event 2 | Provides a normalized, cross-browser, garbage-collection-free API for reading out the state of a mouse event. 3 | 4 | ### Why is this needed? 5 | Because it is 2015 and somehow every major browser still disagrees on even the most basic details of MouseEvents. Seriously guys. 6 | 7 | # Example 8 | 9 | ```javascript 10 | var mouse = require('mouse-event') 11 | 12 | window.addEventListener('mousemove', function(ev) { 13 | document.body.innerHTML = 14 | '

Buttons: ' + mouse.buttons(ev) + 15 | ' x:' + mouse.x(ev) + 16 | ' y:' + mouse.y(ev) + '

' 17 | }) 18 | ``` 19 | 20 | [Try this in your browser](https://mikolalysenko.github.io/mouse-event) 21 | 22 | # Install 23 | 24 | ``` 25 | npm i mouse-event 26 | ``` 27 | 28 | # API 29 | 30 | ```javascript 31 | var mouse = require('mouse-event') 32 | ``` 33 | 34 | #### `mouse.buttons(event)` 35 | Returns a bit vector, similar to `event.which` in WebKit encoding the state of the mouse buttons. 36 | 37 | * `event` is a mouse event 38 | 39 | **Returns** A bit vector with the following interpretation for the flags: 40 | * `1` - left mouse 41 | * `2` - right mouse 42 | * `4` - middle mouse 43 | * `8` - button 4 44 | * `16` - button 5 45 | * ... 46 | * `1<Buttons: ' + mouse.buttons(ev) + 13 | ' x:' + mouse.x(ev) + 14 | ' y:' + mouse.y(ev) + '

' 15 | } 16 | 17 | window.oncontextmenu = function(ev) { 18 | ev.stopPropagation() 19 | ev.preventDefault() 20 | return false 21 | } -------------------------------------------------------------------------------- /mouse.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function mouseButtons(ev) { 4 | if(typeof ev === 'object') { 5 | if('buttons' in ev) { 6 | return ev.buttons 7 | } else if('which' in ev) { 8 | var b = ev.which 9 | if(b === 2) { 10 | return 4 11 | } else if(b === 3) { 12 | return 2 13 | } else if(b > 0) { 14 | return 1<<(b-1) 15 | } 16 | } else if('button' in ev) { 17 | var b = ev.button 18 | if(b === 1) { 19 | return 4 20 | } else if(b === 2) { 21 | return 2 22 | } else if(b >= 0) { 23 | return 1<