最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Programmatically press "Left" key in a text input - Stack Overflow

programmeradmin1浏览0评论

I am trying to programmatically fire a key event to go left in a text box, but not having any luck.

The input element has focus and the cursor is at the end. I'm trying to get the cursor to move left one step - before the letter "F" *programmatically by firing a Keyboard event (keydown/keyup/keypress) with the corresponding keystroke ← or → targeted at the input box.

ABCDEF|

Here's the code so far:

HTML

<input id="a" type="text" />

Javascript

var keyEvent = document.createEvent("KeyboardEvent");

var keyLocation = '0x00';
var keyIdentifier = "Left";

keyEvent.initKeyboardEvent("keypress",
                        true,
                        true,
                        window,
                        keyIdentifier,
                        keyLocation,
                        false);

$("a").dispatchEvent(keyEvent);

Saved a quick demo on jsfiddle if you want to see the whole code - /

I am not interested in making this cross-browser (just get it working in Chrome).

I am trying to programmatically fire a key event to go left in a text box, but not having any luck.

The input element has focus and the cursor is at the end. I'm trying to get the cursor to move left one step - before the letter "F" *programmatically by firing a Keyboard event (keydown/keyup/keypress) with the corresponding keystroke ← or → targeted at the input box.

ABCDEF|

Here's the code so far:

HTML

<input id="a" type="text" />

Javascript

var keyEvent = document.createEvent("KeyboardEvent");

var keyLocation = '0x00';
var keyIdentifier = "Left";

keyEvent.initKeyboardEvent("keypress",
                        true,
                        true,
                        window,
                        keyIdentifier,
                        keyLocation,
                        false);

$("a").dispatchEvent(keyEvent);

Saved a quick demo on jsfiddle if you want to see the whole code - http://jsfiddle.net/Vsafv/

I am not interested in making this cross-browser (just get it working in Chrome).

Share Improve this question edited Oct 18, 2019 at 16:44 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked May 14, 2010 at 3:35 AnuragAnurag 142k37 gold badges222 silver badges261 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10 +75
e = jQuery.Event("keydown"); // define this once in global scope
e.which = 37; // Some key value
$("input").trigger(e);

where "input" is your textarea

37 - left
38 - up
39 - right
40 - down

So when you record your "events" you record the values for the keys pressed.
I'm sure you already figured out a way to do this but just in case, here's an idea of how i would tackle it:

var keysPressed = new Array(); // somewhere in the global scope
$("input").keydown(function (e) {
    keysPressed.push(e.which); //adding values to the end of array
});

Hope this helps

And for those not viewing jQuery as the solution to everything :)

From http://blog.josh420.com/archives/2007/10/setting-cursor-position-in-a-textbox-or-textarea-with-javascript.aspx

function setCaretPosition(elemId, caretPos) {
    var elem = document.getElementById(elemId);

    if(elem != null) {
        if(elem.createTextRange) {
            var range = elem.createTextRange();
            range.move('character', caretPos);
            range.select();
        }
        else {
            if(elem.selectionStart) {
                elem.focus();
                elem.setSelectionRange(caretPos, caretPos);
            }
            else
                elem.focus();
        }
    }
}

As far as I can see, you can do:

var pos = document.getElementById("a").length; 
document.getElementById("a").setSelectionRange(pos-1, pos-1);
发布评论

评论列表(0)

  1. 暂无评论