Blocking mouse wheel in browser when mouse over screen

if("Mouse in screen")
Application.Call("disableWheel")
else
Application.Call("enableWheel")


//Add this javscript to the web page


function disableWheel() {
    /* Gecko */
    addHandler(window, 'DOMMouseScroll', wheel);
    /* Opera */
    addHandler(window, 'mousewheel', wheel);
    /* IE */
    addHandler(document, 'mousewheel', wheel);
}

function enableWheel() {
    /* Gecko */
    removeHandler(window, 'DOMMouseScroll', wheel);
    /* Opera */
    removeHandler(window, 'mousewheel', wheel);
    /* IE */
    removeHandler(document, 'mousewheel', wheel);
}

//function for add envent handler
function addHandler(object, event, handler, useCapture) {
    if (object.addEventListener) {
        object.addEventListener(event, handler, useCapture ? useCapture : false);
    } else if (object.attachEvent) {
        object.attachEvent('on' + event, handler);
    } else alert("Add handler is not supported");
}

//function for remove envent handler
function removeHandler(object, event, handler) {
    if (object.removeEventListener) {
        object.removeEventListener(event, handler, false);
    } else if (object.detachEvent) {
        object.detachEvent('on' + event, handler);
    } else alert("Remove handler is not supported");
}

// Wheel event handler
function wheel(event) {
    var delta; // Scroll direction 
    // -1 - scroll down 
    // 1  - scroll up 
    event = event || window.event;
    // Opera  IE works with property wheelDelta 
    if (event.wheelDelta) {
        delta = event.wheelDelta / 120;
        // In Опере value of wheelDelta the same but with opposite sign 
        if (window.opera) delta = -delta;
        // Gecko uses property detail 
    } else if (event.detail) {
        delta = -event.detail / 3;
    }
    // Disables processing events
    if (event.preventDefault) event.preventDefault();
    event.returnValue = false;
    return delta;
}

Great Script!!

I think though that it is Application.ExternalCall()

How do you detect when the mouse is on Screen?

I’ve detect with making a new Rect(0,0,Screen.width,Screen.height) and then
use the Contains(Input.mousePosition) but I wonder if there is an easier way…

Ou! Thank you for this solution, it’s really needfull :slight_smile:

Building on Russian Madman code I made this one that you just need to run once, is Javascript only, and does not require any call from the Unity player:

// Call this after your unityObject.embedUnity (...) command
disableBrowserWheelUnity ();

// Functions
// Disable wheel function when mouse is over Unity div
function disableBrowserWheelUnity () {
	var myUnity = document.getElementById ( 'unityPlayer' ); // Replace with the id of the div containing your player
	if ( myUnity === undefined || myUnity === null ) {
		// Come back later if embed not ready yet
		setTimeout ( disableBrowserWheelUnity, 2000 );
		return;
	}
	/* Gecko */
	addHandler ( myUnity, 'DOMMouseScroll', wheel );
	/* Opera and IE? Not tested for IE */
	addHandler ( myUnity, 'mousewheel', wheel );
}

/ /function to add event handler
function addHandler ( object, event, handler, useCapture ) {
	if ( object.addEventListener ) {
		object.addEventListener ( event, handler, useCapture ? useCapture : false );
	} else if ( object.attachEvent ) {
		object.attachEvent ( 'on' + event, handler );
	} else alert ( "Add handler is not supported" );
}

// Wheel event handler
function wheel ( event ) {
	var delta; // Scroll direction 
	// -1 - scroll down 
	// 1  - scroll up 
	event = event || window.event;
	// Opera  IE works with property wheelDelta 
	if (event.wheelDelta) {
		delta = event.wheelDelta / 120;
		// In value of wheelDelta the same but with opposite sign 
		if ( window.opera ) delta = -delta;
		// Gecko uses property detail 
	} else if ( event.detail ) {
		delta = -event.detail / 3;
	}
	// Disables processing events
	if ( event.preventDefault ) event.preventDefault ();
	event.returnValue = false;
	return delta;
}