首页 > Web开发 > 详细

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

时间:2019-03-13 16:07:38      阅读:368      评论:0      收藏:0      [点我收藏+]

 

技术分享图片

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;
            }
        });

  

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

原文:https://www.cnblogs.com/liuliangliang/p/10523700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!