Simulate keypress

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christian

    Simulate keypress

    Hello,

    As the subject says it, is there a way to simulate a special keypress in JS
    ? In my case, on loading an HTML page, i'd like to set the cursor at the end
    of the input text of an <input type=text ...> object.
    focus() sets the cusor at the beginning

    Any idea ?

    Christian.


  • Raul Carrillo a.k.a. Metsuke

    #2
    Re: Simulate keypress

    First of all sorry about my poor English, i´ll try to do the best.

    Well, i´m having a similar problem because i wan´t to select certaint
    characters from a text box. Maybe this routine i´ve just created , solves
    your problem.

    This routine, selects in a textbox characters from StartIndex to EndIndex.
    If you gives to the routine both parameters with textbox content lenght, i
    think it puts the cursor in the desired position.

    function selectFromTo (textInput, charStartIndex, charEndIndex) {

    /* We´ll calculate the appropiate value to the different moves. */
    strTextInputVal ue = "" + textInput.value ;

    charStartIndex = (parseInt(charS tartIndex) - 1);

    numEndIndex = parseInt(charEn dIndex);
    numEndIndex -= strTextInputVal ue.length;
    charEndIndex = numEndIndex.toS tring();

    if (textInput.crea teTextRange) {
    var range = textInput.creat eTextRange();
    range.moveStart ('character', charStartIndex );
    range.moveEnd(' character', charEndIndex);
    range.select();
    }
    }

    Raul Carrillo a.k.a Metsuke


    "Christian" <cgregoir99@yah oo.com> escribió en el mensaje
    news:bod5kv$8m6 $1@reader1.imag inet.fr...[color=blue]
    > Hello,
    >
    > As the subject says it, is there a way to simulate a special keypress in[/color]
    JS[color=blue]
    > ? In my case, on loading an HTML page, i'd like to set the cursor at the[/color]
    end[color=blue]
    > of the input text of an <input type=text ...> object.
    > focus() sets the cusor at the beginning
    >
    > Any idea ?
    >
    > Christian.
    >
    >[/color]


    Comment

    • Christian

      #3
      Re: Simulate keypress

      "Raul Carrillo a.k.a. Metsuke <(^_^)>" <rcarrillo@cole ccionalo.com> wrote in
      message news:9irqb.8800 70$uj6.2223837@ telenews.teleli ne.es...[color=blue]
      > First of all sorry about my poor English, i´ll try to do the best.
      >
      > Well, i´m having a similar problem because i wan´t to select certaint
      > characters from a text box. Maybe this routine i´ve just created , solves
      > your problem.
      >
      > This routine, selects in a textbox characters from StartIndex to EndIndex.
      > If you gives to the routine both parameters with textbox content lenght, i
      > think it puts the cursor in the desired position.
      >
      > function selectFromTo (textInput, charStartIndex, charEndIndex) {
      >
      > /* We´ll calculate the appropiate value to the different moves. */
      > strTextInputVal ue = "" + textInput.value ;
      >
      > charStartIndex = (parseInt(charS tartIndex) - 1);
      >
      > numEndIndex = parseInt(charEn dIndex);
      > numEndIndex -= strTextInputVal ue.length;
      > charEndIndex = numEndIndex.toS tring();
      >
      > if (textInput.crea teTextRange) {
      > var range = textInput.creat eTextRange();
      > range.moveStart ('character', charStartIndex );
      > range.moveEnd(' character', charEndIndex);
      > range.select();
      > }
      > }
      >
      > Raul Carrillo a.k.a Metsuke
      >
      >
      > "Christian" <cgregoir99@yah oo.com> escribió en el mensaje
      > news:bod5kv$8m6 $1@reader1.imag inet.fr...[color=green]
      > > Hello,
      > >
      > > As the subject says it, is there a way to simulate a special keypress in[/color]
      > JS[color=green]
      > > ? In my case, on loading an HTML page, i'd like to set the cursor at the[/color]
      > end[color=green]
      > > of the input text of an <input type=text ...> object.
      > > focus() sets the cusor at the beginning
      > >
      > > Any idea ?
      > >
      > > Christian.
      > >
      > >[/color]
      >[/color]

      Thanks for the reply. I eventually found a similar answer on google, using
      createTextRange . That will solve my problem, though i doubt this will work
      on every browser.

      For a more general matter, is there a way to simulate a keypress ?


      Comment

      Working...