Js和webview交互,实现模拟上下左右功能

 

JS端代码

HTMLElement.prototype.pressKey = function(code) {
        var evt = document.createEvent("UIEvents");
        evt.keyCode = code;
        evt.initEvent("keydown", true, true);
        this.dispatchEvent(evt);
    }

    HTMLElement.prototype.upkey = function(code) {
        var evt = document.createEvent("UIEvents");
        evt.keyCode = code;
        evt.initEvent("keyup", true, true);
        this.dispatchEvent(evt);
    }



    HTMLElement.prototype.moudown = function(x,y) {
        //alert("prototypeX:"+x);
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("mousedown",true,true,window,0,x,y,x,y,false,false,false,false,0,null);
        //alert("prototype:event"+evt);
        //alert("prototype:down"+evt.pageX);
        this.dispatchEvent(evt);
    }

    HTMLElement.prototype.mouup = function(x,y) {
        //var evt = document.createEvent("MouseEvents");
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("mouseup",true,true,window,0,x,y,x,y,false,false,false,false,0,this.camera);
        //alert("prototype:event"+evt);
        //alert("prototype:down"+evt.pageX);
        this.dispatchEvent(evt);
    }

    HTMLElement.prototype.moumove = function(x,y) {
        var evt = document.createEvent("MouseEvents");
        evt.initMouseEvent("mousemove",true,true,window,0,x,y,x,y,false,false,false,false,0,null);
        //alert("prototype:event"+evt);
        //alert("prototype:down"+evt.pageX);
        this.dispatchEvent(evt);
    }

    function spaceDown() {
        document.body.pressKey(32);
    }

    function spacenUp(){
        document.body.upkey(32)
    }

    function pressA(){
        document.body.pressKey(37);
    }
    function upA(){
        document.body.upkey(37)
    }

    function   pressW(){
        document.body.pressKey(38);
    }

    function   upW(){
        document.body.upkey(38)
    }

    function  pressD(){
        document.body.pressKey(39);
    }


    function upD(){
        document.body.upkey(39);
    }


    function pressS(){
        document.body.pressKey(40);
    }

    function upS(){
        document.body.upkey(40);
    }
    function clickDown(x,y){
        document.body.moudown(x,y);
    }
    function clickUp(x,y){
        document.body.mouup(x,y);
    }

    function clickMove(x,y){
        document.body.moumove(x,y);
    }

    function isLeft(isLeftButton){
        pressLeftButton = isLeftButton;
    }

  

Android端代码

  //上 38
        bt_up.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        webview.loadUrl("javascript:pressW()");
                        break;

                    case MotionEvent.ACTION_UP:
                        webview.loadUrl("javascript:upW()");
                        break;
                }
                return true;
            }
        });

  

posted on 2019-03-13 15:54  爱学习的亮亮  阅读(252)  评论(0)    收藏  举报

导航