简体   繁体   English

JavaScript:将关键事件模拟​​到文本框/输入中

[英]JavaScript : Simulate Key Events into Textbox/Input

Despite many article on SO on how to simulate key presses (keydown/keypress) in JS, no one solution seems to be working with the browsers I'm using (Firefox ESR 17.0.7, Chrome 28.0.1500.72, IE 10). 尽管有很多关于如何在JS中模拟按键(keydown / keypress)的文章,但是没有一个解决方案似乎与我正在使用的浏览器一起工作(Firefox ESR 17.0.7,Chrome 28.0.1500.72,IE 10)。 The solutions I have tested were taken from here , here , and here . 我测试的解决方案来自这里这里这里

What I'm trying to do is to simulate ANY keystroke in a textarea / input. 我要做的是模拟textarea / input中的任何键击。 While I can append / delete characters directly changing "value", I see no option but input simulation for the keys like "Up", "Down", "Home", and some others. 虽然我可以追加/删除直接更改“值”的字符,但我看不到选项,只能输入“Up”,“Down”,“Home”等键的模拟。

According to the documentation , it should be simple. 根据文档 ,它应该很简单。 For example: 例如:

var e = document.createEvent("KeyboardEvent");
if (e.initKeyboardEvent) {  // Chrome, IE
    e.initKeyboardEvent("keydown", true, true, document.defaultView, "Enter", 0, "", false, "");
} else { // FF
    e.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, 13, 0);
}
document.getElementById("text").dispatchEvent(e);

indeed fires the "Enter" keydown event, and my handler can catch it. 确实触发了“Enter”keydown事件,我的处理程序可以捕获它。 However, it does not affect the textarea in any way - a new line does not appear. 但是,它不会以任何方式影响textarea - 不会出现新行。 Same for other key-codes: characters do not appear, arrows do not change the caret's location, etc. 其他键码相同:不显示字符,箭头不改变插入符号的位置等。

I have extended the code by Orwellophile and posted it to http://jsfiddle.net/npF3d/4/ , so anyone can play with the code. 我已经通过Orwellophile扩展了代码并将其发布到http://jsfiddle.net/npF3d/4/ ,所以任何人都可以使用代码。 In my browsers, no button produces any effect on the textarea in any condition. 在我的浏览器中,在任何情况下都没有按钮对textarea产生任何影响。

I would appreciate any help on this issue. 我将不胜感激这个问题的任何帮助。

I'm pretty certain that this is a "security" thing, as I've run into the same thing when trying to simulate key presses before. 我很确定这是一个“安全”的事情,因为我在尝试模拟按键之前遇到了同样的事情。

Q: How can I type programatically then? 问:如何以编程方式输入?
A: Getting/setting selectionStart , selectionEnd , etc, as well as using these in combination with String methods like slice to insert characters. 答:获取/设置selectionStartselectionEnd等,以及将这些与String方法结合使用,如slice插入字符。 (See HTMLTextAreaElement reference) (参见HTMLTextAreaElement参考)

Q: Why would you still use this kind of event then? 问:为什么你还会使用这种活动呢?
A: All of the event listeners will work as if it was a real key event. 答:所有的事件监听器都会像真正的关键事件一样工作。


Reduced functionality for arrows/home/end can be achieved thusly DEMO 因此,可以实现箭头/家庭/终端的减少的功能演示

function homeKey(elm) {
    elm.selectionEnd =
        elm.selectionStart =
            elm.value.lastIndexOf(
                '\n',
                elm.selectionEnd - 1
            ) + 1;
}

function endKey(elm) {
    var pos = elm.selectionEnd,
        i = elm.value.indexOf('\n', pos);
    if (i === -1) i = elm.value.length;
    elm.selectionStart = elm.selectionEnd = i;
}

function arrowLeft(elm) {
    elm.selectionStart = elm.selectionEnd -= 1;
}

function arrowRight(elm) {
    elm.selectionStart = elm.selectionEnd += 1;
}

function arrowDown(elm) {
    var pos = elm.selectionEnd,
        prevLine = elm.value.lastIndexOf('\n', pos),
        nextLine = elm.value.indexOf('\n', pos + 1);
    if (nextLine === -1) return;
    pos = pos - prevLine;
    elm.selectionStart = elm.selectionEnd = nextLine + pos;
}

function arrowUp(elm) {
    var pos = elm.selectionEnd,
        prevLine = elm.value.lastIndexOf('\n', pos),
        TwoBLine = elm.value.lastIndexOf('\n', prevLine - 1);
    if (prevLine === -1) return;
    pos = pos - prevLine;
    elm.selectionStart = elm.selectionEnd = TwoBLine + pos;
}

Q: Where does it go wrong? 问:哪里出错了?
A: If lines are long enough to be wrapped, it will treat them as if unwrapped. 答:如果线条足够长以便包裹,它会将它们视为未包装。

Sending keys to the browser can be achieved via Selenium: http://docs.seleniumhq.org/ 通过Selenium可以实现向浏览器发送密钥: http//docs.seleniumhq.org/

It provides a driver for each browser that can be programmed. 它为每个可编程的浏览器提供驱动程序。 It usually starts with opening a URL then your script will act as a remote control to the browser. 它通常从打开URL开始,然后您的脚本将充当浏览器的远程控制。 Thus allowing you to send actual keys rather than simulating them which is not possible programatically within the browser. 因此,允许您发送实际密钥而不是模拟它们,这在浏览器中以编程方式无法实现。

You could for instance achieve this using http://webdriver.io/ 例如,您可以使用http://webdriver.io/来实现此目的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM