JS mouse side button monitoring (there are also left, middle and right buttons)

Time:2020-11-22
No beep, straight dry.
JS code:
document.onmousedown = function(event) {
                // var event = event ||  window.event// Compatible with lower version of IE
                if (event.button == 0) {
                    //Left mouse button 
                    Alert ("you click the left mouse button!");
                } else if (event.button == 2) {
                    //Right mouse button 
                    Alert ("you click the right mouse button!");
                } else if (event.button == 1) {
                    //Middle mouse button 
                    Alert ("you click the middle mouse button!");
                } else if (event.button == 3) {
                    //Mouse side key 3 
                    Back (you click the mouse button);
                } else if (event.button == 4) {
                    //Mouse side key 4 
                    Alert ("you click the mouse side button (forward)");
                }
            }

JQuery code (listen to the body, in fact, it doesn’t matter, mainly to see evevt.button The value of, in which code, is the same:

$("body").mousedown(function(event){ 
                        if(event.button == 0){ 
                          //Left mouse button 
                              Alert ("you click the left mouse button!"); 
                        }else if(event.button == 2){ 
                          //Right mouse button 
                              Alert ("you click the right mouse button!"); 
                        }else if(event.button == 1){ 
                          //Middle mouse button 
                              Alert ("you click the middle mouse button!"); 
                        }else if(event.button == 3){ 
                          //Mouse side key 3 
                              Back (you click the mouse button); 
                        }else if(event.button == 4){ 
                          //Mouse side key 4 
                              Alert ("you click the mouse side button (forward)"); 
                        }
                  });

Work time sudden fantasy, mouse side key can monitor? Then try Baidu, the results are only left, middle and right three key, but according to the rule of speculation out of the side key, and then test, can, so share. I only tried two mouse side buttons, if you have any questions, please contact me. Though I may not be able to see it.

Recommended Today

Explain module, import and export in JavaScript

Author: Tania rascia Crazy technology house Original text:https://www.taniarascia.com/j… In the era of the Internet, websites are mainly developed with HTML and CSS. If you load JavaScript into a page, it usually provides effects and interactions in the form of small fragments. Generally, all JavaScript code is written in a file and loaded into a filescriptTag. […]