zoukankan      html  css  js  c++  java
  • JavaScript_js模拟键盘输入

    function fireKeyEvent(el, evtType, keyCode) {
                var evtObj;
                if (document.createEvent) {
                    if (window.KeyEvent) {//firefox 浏览器下模拟事件
                        evtObj = document.createEvent('KeyEvents');
                        evtObj.initKeyEvent(evtType, true, true, window, true, false, false, false, keyCode, 0);
                    } else {//chrome 浏览器下模拟事件
                        evtObj = document.createEvent('UIEvents');
                        evtObj.initUIEvent(evtType, true, true, window, 1);
    
                        delete evtObj.keyCode;
                        if (typeof evtObj.keyCode === "undefined") {//为了模拟keycode
                            Object.defineProperty(evtObj, "keyCode", { value: keyCode });                       
                        } else {
                            evtObj.key = String.fromCharCode(keyCode);
                        }
    
                        if (typeof evtObj.ctrlKey === 'undefined') {//为了模拟ctrl键
                            Object.defineProperty(evtObj, "ctrlKey", { value: true });
                        } else {
                            evtObj.ctrlKey = true;
                        }
                    }
                    el.dispatchEvent(evtObj);
    
                } else if (document.createEventObject) {//IE 浏览器下模拟事件
                    evtObj = document.createEventObject();
                    evtObj.keyCode = keyCode
                    el.fireEvent('on' + evtType, evtObj);
                }
            }
            
            var testPassword = "181818";
            var tp;
            var cCode;
            var testss = document.getElementById("input_txt_50531_740884");
            for(var i=0;i<testPassword.length;i++){
                cCode = testPassword.charCodeAt(i);
                fireKeyEvent(testss, "keydown", cCode);
                fireKeyEvent(testss, "keypress", cCode);
                fireKeyEvent(testss, "keyup", cCode);
            }
            
            
            
  • 相关阅读:
    samdump2获取虚拟机密码
    PHP执行cmd方法若干
    Aircrack-ng学习笔记之无AP破解WiFi密码
    Python基础:extend与append的区别
    Python基础:dictionary
    UVALive 3177 长城守卫
    UVALive 3902 网络
    UVA 11520 填充正方形
    UVALive 3635 分派
    UVALive 3971 组装电脑
  • 原文地址:https://www.cnblogs.com/gisblogs/p/5821665.html
Copyright © 2011-2022 走看看