如何全局禁用鼠标右键

  • Creator 版本:

  • 目标平台:

  • 重现方式:如何全局禁用鼠标右键

  • 首个报错:

  • 之前哪个版本是正常的:

  • 手机型号:

  • 手机浏览器:

  • 编辑器操作系统:

  • 重现概率:

????????

如果你發佈出來是網頁,你用網頁禁右鍵的方法就可以了

打包H5,没办法想html那样禁止右键

右键走的也是touch事件呐

一个页面当然可以,全局的就比较麻烦了

不知道你的具体需求,找了下源码没有这样的开关,可以试试在监听的地方,改改源码处理下。或者绕过去。

cocos按键,无论左键右键都能触发,要求只能左键触发,右键不管用

用鼠标事件来监听点击就好了,传入的参数可以判断点击的按键

怎么应用到全局呢,一个按键好弄,那么多久不好弄了

https://docs.cocos.com/creator/2.4/manual/zh/scripting/events.html
这个没有全局,看这个文档就懂了。写个脚本在顶级(如canvas)下加个脚本监听你的鼠标点击事件; 或者源码级去改。

顶级节点:

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

private _touchID: number = null;
onEnable() {
    this.node.on(cc.Node.EventType.MOUSE_DOWN, this._touchStart, this, true);
    this.node.on(cc.Node.EventType.MOUSE_UP, this._touchEnd, this, true);
}

onDisable() {
    this.node.off(cc.Node.EventType.MOUSE_DOWN, this._touchStart, this, true);
    this.node.off(cc.Node.EventType.MOUSE_UP, this._touchEnd, this, true);
}

private _touchStart(event:cc.Event.EventMouse) {
    let mouseT = event.getButton();
    cc.log("_touchStart鼠标按键-" + mouseT);
    if(mouseT != 0){
        cc.log("吞了");
        event.stopPropagation()
        return;
    }
}


private _touchEnd(event) {
    let mouseT = event.getButton();
    cc.log("_touchEnd鼠标按键-" + mouseT);
    if(mouseT != 0){
        cc.log("吞了");
        event.stopPropagation()
        return;
    }
}}

验证脚本: canvas下节点随便挂一个

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

@property(cc.Label)
label: cc.Label = null;

@property
text: string = 'hello';

// LIFE-CYCLE CALLBACKS:

onLoad () {
    this.node.on(cc.Node.EventType.MOUSE_DOWN, this._touchStart, this, true);
}

private _touchStart(event:cc.Event.EventMouse) {
  cc.log("下级节点");
}
// update (dt) {}}

楼上的方法就可以,内置的全局事件只包含键盘和重力感应,鼠标事件只能在节点绑定监听

没拦住

周末前最后一天,哪位大佬指点两句

1赞

image

image

把B放在最前层先监听一下事件,然后进行拦截就是了,我看了下上面的代码,node.on()最后一位的参数true为开启捕获阶段,官方文档有实例:image 如果B,C,D同时注册,不开启捕获情况下,事件冒泡顺序为B=>C=>D,如果D,C开启捕获后顺序为D=>C=>B,你的拦截不到应该就是这个导致的

let prototype = Input.prototype;

    prototype._ori_simulateEventTouch = prototype._simulateEventTouch;

    prototype._simulateEventTouch = function (eventMouse: EventMouse) {

        //@ts-ignore

        if (eventMouse._button == 0) { //屏蔽鼠标中键和右键

            this._ori_simulateEventTouch(eventMouse);

        }

    };

回答也许会迟到,但从不缺席 :call_me_hand: