views:

376

answers:

4

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). Thanks for any help.

+1  A: 

You can take a look at this StackOverflow Question -> jQuery Set Cursor Position in Text Area

GerManson
+4  A: 

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();
        }
    }
}
Sean Kinsey
Wanted to -1 as there's not enough jQuery :P, but seriously - thanks for the solution. I can move the caret around, but am interested in knowing if it can be done by firing a Keyboard event like "keydown/keyup/keypress". Reason being I am writing a script to replay user actions, and it becomes trivial if I can just record all event objects, and dispatch them at a later point. It's far simpler than trying to map each event to an appropriate action/function while replaying as in the above case where ← or → keys map to something that moves the caret.
Anurag
-1 Doesn't answer the question how to send events to a text input. The example just works to position the cursor to some specific position but not how to properly create an event and how to make the browser process it.
Aaron Digulla
@Aaron, when I answered the question could be interpreted to mean 'how to I move the cursor left', and this is a perfectly valid answer for that.
Sean Kinsey
+5  A: 
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

Raine
Thanks for the solution @Raine. I have tried a basic example with your code - http://jsfiddle.net/sZL3H/, but did not have any luck.
Anurag
A: 

As far as I can see, you can do:

var pos = document.getElementById("a").length; 
document.getElementById("a").setSelectionRange(pos-1, pos-1);
Vestel