1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  3. All Unity Pro perpetual license customers: your special offer to subscribe is ready! Click here.
    Dismiss Notice
  4. Unity 5.5 is now released.
    Dismiss Notice
  5. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  6. Unity 5.6 beta is now available for download.
    Dismiss Notice
  7. Get prepared for the Tizen Mobile App Incentive Program! Read more about the upcoming program here.
    Dismiss Notice
  8. Enter the Microsoft Developer Challenge for a chance to win prizes. Read more about it here.
    Dismiss Notice

Blocking mouse wheel in browser when mouse over screen

Discussion in 'Scripting' started by Russian Madman, Dec 24, 2009.

  1. Russian Madman

    Russian Madman

    Joined:
    Apr 20, 2009
    Messages:
    29
    Code (csharp):
    1.  
    2. if("Mouse in screen")
    3. Application.Call("disableWheel")
    4. else
    5. Application.Call("enableWheel")
    6.  
    7.  
    8. //Add this javscript to the web page
    9.  
    10.  
    11. function disableWheel() {
    12.     /* Gecko */
    13.     addHandler(window, 'DOMMouseScroll', wheel);
    14.     /* Opera */
    15.     addHandler(window, 'mousewheel', wheel);
    16.     /* IE */
    17.     addHandler(document, 'mousewheel', wheel);
    18. }
    19.  
    20. function enableWheel() {
    21.     /* Gecko */
    22.     removeHandler(window, 'DOMMouseScroll', wheel);
    23.     /* Opera */
    24.     removeHandler(window, 'mousewheel', wheel);
    25.     /* IE */
    26.     removeHandler(document, 'mousewheel', wheel);
    27. }
    28.  
    29. //function for add envent handler
    30. function addHandler(object, event, handler, useCapture) {
    31.     if (object.addEventListener) {
    32.         object.addEventListener(event, handler, useCapture ? useCapture : false);
    33.     } else if (object.attachEvent) {
    34.         object.attachEvent('on' + event, handler);
    35.     } else alert("Add handler is not supported");
    36. }
    37.  
    38. //function for remove envent handler
    39. function removeHandler(object, event, handler) {
    40.     if (object.removeEventListener) {
    41.         object.removeEventListener(event, handler, false);
    42.     } else if (object.detachEvent) {
    43.         object.detachEvent('on' + event, handler);
    44.     } else alert("Remove handler is not supported");
    45. }
    46.  
    47. // Wheel event handler
    48. function wheel(event) {
    49.     var delta; // Scroll direction
    50.     // -1 - scroll down
    51.     // 1  - scroll up
    52.     event = event || window.event;
    53.     // Opera  IE works with property wheelDelta
    54.     if (event.wheelDelta) {
    55.         delta = event.wheelDelta / 120;
    56.         // In Опере value of wheelDelta the same but with opposite sign
    57.         if (window.opera) delta = -delta;
    58.         // Gecko uses property detail
    59.     } else if (event.detail) {
    60.         delta = -event.detail / 3;
    61.     }
    62.     // Disables processing events
    63.     if (event.preventDefault) event.preventDefault();
    64.     event.returnValue = false;
    65.     return delta;
    66. }
    67.  
     
  2. chchrist

    chchrist

    Joined:
    Oct 31, 2009
    Messages:
    5
    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...
     
  3. 3dever

    3dever

    Joined:
    Mar 2, 2009
    Messages:
    71
    Ou! Thank you for this solution, it's really needfull :)
     
  4. xavierlepretre

    xavierlepretre

    Joined:
    Jul 14, 2011
    Messages:
    1
    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:

    Code (csharp):
    1. // Call this after your unityObject.embedUnity (...) command
    2. disableBrowserWheelUnity ();
    3.  
    4. // Functions
    5. // Disable wheel function when mouse is over Unity div
    6. function disableBrowserWheelUnity () {
    7.     var myUnity = document.getElementById ( 'unityPlayer' ); // Replace with the id of the div containing your player
    8.     if ( myUnity === undefined || myUnity === null ) {
    9.         // Come back later if embed not ready yet
    10.         setTimeout ( disableBrowserWheelUnity, 2000 );
    11.         return;
    12.     }
    13.     /* Gecko */
    14.     addHandler ( myUnity, 'DOMMouseScroll', wheel );
    15.     /* Opera and IE? Not tested for IE */
    16.     addHandler ( myUnity, 'mousewheel', wheel );
    17. }
    18.  
    19. / /function to add event handler
    20. function addHandler ( object, event, handler, useCapture ) {
    21.     if ( object.addEventListener ) {
    22.         object.addEventListener ( event, handler, useCapture ? useCapture : false );
    23.     } else if ( object.attachEvent ) {
    24.         object.attachEvent ( 'on' + event, handler );
    25.     } else alert ( "Add handler is not supported" );
    26. }
    27.  
    28. // Wheel event handler
    29. function wheel ( event ) {
    30.     var delta; // Scroll direction
    31.     // -1 - scroll down
    32.     // 1  - scroll up
    33.     event = event || window.event;
    34.     // Opera  IE works with property wheelDelta
    35.     if (event.wheelDelta) {
    36.         delta = event.wheelDelta / 120;
    37.         // In value of wheelDelta the same but with opposite sign
    38.         if ( window.opera ) delta = -delta;
    39.         // Gecko uses property detail
    40.     } else if ( event.detail ) {
    41.         delta = -event.detail / 3;
    42.     }
    43.     // Disables processing events
    44.     if ( event.preventDefault ) event.preventDefault ();
    45.     event.returnValue = false;
    46.     return delta;
    47. }