如何使用Javascript检查当前鼠标按钮状态 [英] How to check the current mouse button state Using Javascript

查看:113
本文介绍了如何使用Javascript检查当前鼠标按钮状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望鼠标处于关闭状态关闭状态

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;

function mouseMove(ev) {

    mouseState="";
    //How can I know the state button of mouse button from here 
    if(mouseState=='down') {
        console.log('mouse down state')
    }

    if(mouseState=='up')  {
        console.log('mouse up state')
    }
}

function mouseDown(ev) {
    console.log('Down State you can now start dragging');
    //do not write any code here in this function
}

function mouseUp(ev) {
    console.log('up state you cannot drag now because you are not holding your mouse')
    //do not write any code here in this function
} 

当我移动鼠标时,程序应该在控制台上显示所需的鼠标状态值,现在

When I moved my mouse, program should show the required value of mouseState up or down on console for now

推荐答案

您可以查看 MouseEvent.which 属性。

function mouseMove(ev) {
    if(ev.which==1) {
        console.log('mouse down state with left click');
    } else if(ev.which==3)  {
        console.log('mouse down state with right click');
    } else {
        console.log('mouse update');
    } 
}

这篇关于如何使用Javascript检查当前鼠标按钮状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆