赞 | 39 |
VIP | 0 |
好人卡 | 0 |
积分 | 35 |
经验 | 0 |
最后登录 | 2024-10-30 |
在线时间 | 293 小时 |
Lv3.寻梦者
- 梦石
- 0
- 星屑
- 3476
- 在线时间
- 293 小时
- 注册时间
- 2020-1-27
- 帖子
- 190
|
回帖奖励 +4
本帖最后由 zths 于 2021-5-26 15:28 编辑
- //KeyEvent 事件属性 或者一个按键字(仅限 a-z 0-9)
- function fireKeyEv(keyOrOption,time){
- return new Promise(function(r,rj){
- var opt;
- if (typeof keyOrOption === 'string' || keyOrOption instanceof String){
- var keycode = keyOrOption.toUpperCase().charCodeAt(0);
- opt = {
- key: keyOrOption[0],
- which: keycode,
- code: "Key"+keyOrOption.toUpperCase()[0],
- keyCode: keycode
- };
- }else{
- opt = keyOrOption;
- }
- if(time === undefined){
- time = 300;
- }
- var downEv = new KeyboardEvent("keydown", opt);
- var pressEv = new KeyboardEvent("keypress", opt);
- var upEv = new KeyboardEvent("keyup", opt);
- document.dispatchEvent(downEv);
- var inv = setInterval(function(){
- document.dispatchEvent(downEv);
- document.dispatchEvent(pressEv);
- }, 500);
- setTimeout(function(){
- r();
- clearInterval(inv);
- document.dispatchEvent(pressEv);
- document.dispatchEvent(upEv);
- }, time)
- });
- }
- (async function(){
- await fireKeyEv("x");
- await fireKeyEv({keyCode: 40});//{key:"ArrowDown", which:40, keyCode:40, code:"ArrowDown"}
- await fireKeyEv("z");
- await fireKeyEv("z");
- })();
复制代码
试试. |
评分
-
查看全部评分
|