javascript,模拟键盘事件,使用chrome(webkit) [英] javascript, simulate keyboard events, work on chrome(webkit)

查看:1694
本文介绍了javascript,模拟键盘事件,使用chrome(webkit)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FF中,我使用了这段代码:

 

if(keyCount == lineLimit){
// FF中的方法,不需要
var mock = document.createEvent(KeyboardEvent); //或KeysEvent
mock.initKeyEvent(keypress,true,true,null,false,false,false,false,14,0);
var x = document.getElementById('InputCategory');
//在输入
$(this).height(div_height + font_height + offset_height)之前上升高度;
//模拟输入
x.dispatchEvent(mock);
// init keyCount
keyCount = 0;
}

它可以工作,但在基于webkit的浏览器(如chrome)上无法生效。

所以我问谷歌,发现键盘事件是DOM Level 3事件之一,这里是一个aticle: http://www.w3.org/TR/DOM-Level-3-Events/



然后我知道/ * initKeyboardEvent /在chrome& safari,/ / initUIEvent * /我试过了,它也没有工作。

虚拟键盘事件reall可以在chrome上模拟吗? plesase帮助我:)

解决方案

这很有效,但它不会生成按键事件,而是生成文本插入事件。 / p>

  var te = document.createEvent('TextEvent'); 
te.initTextEvent('textInput',true,true,window,'test');
< element> .dispatchEvent(te);

在输入末尾插入'test'用 \\\


替换

in FF, i've used this code:


if (keyCount == lineLimit) {
    // method in FF, no Chrome
    var mock = document.createEvent("KeyboardEvent"); // or KeysEvent
    mock.initKeyEvent("keypress",true,true,null,false,false,false,false,14,0);
    var x = document.getElementById('InputCategory');
    // rise height before Enter
    $(this).height(div_height + font_height + offset_height);
    // mock Enter
    x.dispatchEvent(mock);
    // init keyCount
    keyCount = 0;
}

it works, but could not be effective on webkit-based browsers like chrome.

so i asked google and found keyboard event is one of the DOM Level 3 events,here is an aticle: http://www.w3.org/TR/DOM-Level-3-Events/

then i knew /* initKeyboardEvent / is not supported on chrome & safari, / initUIEvent */ i've tried, it didn't work also.

Do virtual keyboard events reall can be simulated on chrome ? plesase help me :)

解决方案

This works, but it's not generating a keypress-event, rather a text-insert event.

var te = document.createEvent('TextEvent');
te.initTextEvent('textInput', true, true, window, 'test');
<element>.dispatchEvent(te);

That inserts the word 'test' at the end of the input (in your case you'd probably replace that by \n.

这篇关于javascript,模拟键盘事件,使用chrome(webkit)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆