mouse-change
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,6 +0,7 @@ | ||
require('../mouse-listen')(function(buttons, x, y) { | ||
require('../mouse-listen')(function(buttons, x, y, mods) { | ||
document.body.innerHTML = | ||
'<p>Buttons: 0b' + buttons.toString(2) + | ||
', x:' + x + | ||
', y:' + y + '</p>' | ||
', y:' + y + | ||
', mods:' + JSON.stringify(mods) + '</p>' | ||
}) |
@@ -14,4 +14,33 @@ 'use strict' | ||
var buttonState = 0, x = 0, y = 0 | ||
var buttonState = 0 | ||
var x = 0 | ||
var y = 0 | ||
var mods = { | ||
shift: false, | ||
alt: false, | ||
control: false, | ||
meta: false | ||
} | ||
function updateMods(ev) { | ||
var changed = false | ||
if('altKey' in ev) { | ||
changed = changed || ev.altKey !== mods.alt | ||
mods.alt = !!ev.altKey | ||
} | ||
if('shiftKey' in ev) { | ||
changed = changed || ev.shiftKey !== mods.shift | ||
mods.shift = !!ev.shiftKey | ||
} | ||
if('ctrlKey' in ev) { | ||
changed = changed || ev.ctrlKey !== mods.control | ||
mods.control = !!ev.ctrlKey | ||
} | ||
if('metaKey' in ev) { | ||
changed = changed || ev.metaKey !== mods.meta | ||
mods.meta = !!ev.meta | ||
} | ||
return changed | ||
} | ||
function handleEvent(nextButtons, ev) { | ||
@@ -25,7 +54,8 @@ var nextX = mouse.x(ev) | ||
nextX !== x || | ||
nextY !== y) { | ||
nextY !== y || | ||
updateMods(ev)) { | ||
buttonState = nextButtons|0 | ||
x = nextX||0 | ||
y = nextY||0 | ||
callback(buttonState, x, y) | ||
callback(buttonState, x, y, mods) | ||
} | ||
@@ -39,9 +69,23 @@ } | ||
function handleBlur() { | ||
if(buttonState || x || y) { | ||
if(buttonState || | ||
x || | ||
y || | ||
mods.shift || | ||
mods.alt || | ||
mods.meta || | ||
mods.control) { | ||
x = y = 0 | ||
buttonState = 0 | ||
callback(0, 0, 0) | ||
mods.shift = mods.alt = mods.control = mods.meta = false | ||
callback(0, 0, 0, mods) | ||
} | ||
} | ||
function handleMods(ev) { | ||
if(updateMods(ev)) { | ||
callback(buttonState, x, y, mods) | ||
} | ||
} | ||
element.addEventListener('mousemove', function(ev) { | ||
@@ -54,5 +98,7 @@ if(mouse.buttons(ev) === 0) { | ||
}) | ||
element.addEventListener('mousedown', function(ev) { | ||
handleEvent(buttonState | mouse.buttons(ev), ev) | ||
}) | ||
element.addEventListener('mouseup', function(ev) { | ||
@@ -66,4 +112,7 @@ handleEvent(buttonState & ~mouse.buttons(ev), ev) | ||
element.addEventListener('mouseover', clearState) | ||
element.addEventListener('keyup', handleMods) | ||
element.addEventListener('keydown', handleMods) | ||
element.addEventListener('keypress', handleMods) | ||
element.addEventListener('blur', handleBlur) | ||
if(element !== window) { | ||
@@ -70,0 +119,0 @@ window.addEventListener('blur', handleBlur) |
{ | ||
"name": "mouse-change", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Mouse state change listener", | ||
@@ -5,0 +5,0 @@ "main": "mouse-listen.js", |
@@ -26,9 +26,17 @@ mouse-change | ||
#### `require('mouse-change')([element,] onchange(buttons,x,y))` | ||
#### `require('mouse-change')([element,] onchange(buttons,x,y,mods))` | ||
Listens for any mouse state changes on the given element. | ||
* `element` is an optional element | ||
* `onchange(buttons,x,y)` is called every time that the mouse state changes inside `element` | ||
* `onchange(buttons,x,y,mods)` is called every time that the mouse state changes inside `element` | ||
+ `buttons` is the state of the mouse buttons | ||
+ `x` is the x coordinate of the mouse | ||
+ `y` is the y coordinate of the mouse | ||
+ `mods` is an object storing the state of any key modifiers | ||
* `mods.shift` is the state of the shift key | ||
* `mods.alt` is the state of then alt key | ||
* `mods.control` is the state of the control key | ||
* `mods.meta` is the state of the meta key | ||
# License | ||
(c) 2015 Mikola Lysenko. MIT License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5946
105
41