programmatic firing of keydown event

Discussion in 'Javascript' started by runner7, Sep 8, 2006.

  1. runner7

    runner7 Guest

    Can anyone help me with how to programmatically fire a keydown event in
    Firefox?
    runner7, Sep 8, 2006
    #1
    1. Advertising

  2. runner7

    Guest

    runner7 wrote:
    > Can anyone help me with how to programmatically fire a keydown event in
    > Firefox?



    I don't think it's possible to cause a keyDown event for security
    reasons, or if you can I don't think you can set the keyCode. I might
    be wrong though, so don't take this as a final answer.
    , Sep 8, 2006
    #2
    1. Advertising

  3. runner7

    runner7 Guest

    As best as I can tell from "developer.mozilla" and "w3c" sites, the
    following code should work in Firefox, but I cannot get it to work:

    function keyFire(e) {
    var e1=document.createEvent("KeyboardEvent");
    e1.initKeyboardEvent("keydown",true,true,window,"U+0009",
    DOM_KEY_LOCATION_STANDARD,null);
    document.forms[0].elements[0].dispatchEvent(e1);
    e.preventDefault();
    e.stopPropagation();
    }

    "U+0009" happens to be for the tab key. Has anyone got a clue?
    runner7, Sep 9, 2006
    #3
  4. runner7 wrote:

    > As best as I can tell from "developer.mozilla" and "w3c" sites, the
    > following code should work in Firefox, but I cannot get it to work:
    >
    > function keyFire(e) {
    > var e1=document.createEvent("KeyboardEvent");
    > e1.initKeyboardEvent("keydown",true,true,window,"U+0009",
    > DOM_KEY_LOCATION_STANDARD,null);
    > document.forms[0].elements[0].dispatchEvent(e1);
    > e.preventDefault();
    > e.stopPropagation();
    > }
    >
    > "U+0009" happens to be for the tab key. Has anyone got a clue?


    <body onKeyDown="
    if ( window.event ) {
    alert( window.event.keyCode );
    }
    else
    if ( event ) {
    alert( event.which );
    }
    ">

    --
    Bart
    Bart Van der Donck, Sep 9, 2006
    #4
  5. runner7

    gmgj Guest

    gmgj, Sep 9, 2006
    #5
  6. runner7

    runner7 Guest

    Thanks guys for your answers, but I think I need to clarify what I am
    trying to do. I am not trying to detect a keystroke or what key was
    pressed. I am trying to programmatically send the equivalent of a
    keystroke to the Firefox browser.

    In IE, the problem is trivial, because window.event.keyCode can be set,
    but in Firefox the event.which property appears to be read only.
    Thanks to anyone for any further insight on this matter.
    runner7, Sep 9, 2006
    #6
  7. runner7 wrote:

    > Thanks guys for your answers, but I think I need to clarify what I am
    > trying to do. I am not trying to detect a keystroke or what key was
    > pressed. I am trying to programmatically send the equivalent of a
    > keystroke to the Firefox browser.


    That equivalent is actually just the decimal code point of the
    character.

    alert('W'.charCodeAt(0));

    should show you '87', the same cp as when pressing 'W' in my previous
    snippet. There are some things you should be aware of though (e.g.
    upper/lower case), but you can send/retrieve such values wherever you
    want.

    > In IE, the problem is trivial, because window.event.keyCode can be set,
    > but in Firefox the event.which property appears to be read only.


    I don't think this is correct. AFAIK, keyCode should be read-only
    too.

    Your problem is more fundamental. You want to make javascript think the
    user pressed a key, while he actually didn't press it. It's spoofing by
    definition - browsers are right to complain about this kind of
    trickery. My gut feeling is that you should give up your plan and
    rethink your coding strategy.

    And honestly I can't think of any good reason why you would want to
    fake a keystroke, besides theoretical or hacking motives.

    --
    Bart
    Bart Van der Donck, Sep 10, 2006
    #7
  8. runner7

    runner7 Guest

    Bart Van der Donck wrote:
    > > In IE, the problem is trivial, because window.event.keyCode can be set,
    > > but in Firefox the event.which property appears to be read only.

    >
    > I don't think this is correct. AFAIK, keyCode should be read-only
    > too.


    (AFAIK==as far as I know?)

    The following code works in IE to replace an "enter" keystroke with a
    "tab" keystroke in the first text box:

    <html>
    <head><title></title>
    <script type="text/javascript">
    function replace(e) {
    if (e.keyCode==13) e.keyCode=9
    }
    </script>
    </head>
    <body>
    <form>
    <input onkeydown="replace(event)" /><br />
    <input />
    </form>
    </body>
    </html>

    If you replace the "keyCode" property with the "which" property for
    Firefox, the code does not work in Firefox.

    Bart Van der Donck wrote:
    > Your problem is more fundamental. You want to make javascript think the
    > user pressed a key, while he actually didn't press it. It's spoofing by
    > definition - browsers are right to complain about this kind of
    > trickery. My gut feeling is that you should give up your plan and
    > rethink your coding strategy.
    >
    > And honestly I can't think of any good reason why you would want to
    > fake a keystroke, besides theoretical or hacking motives.


    The purpose is for a data entry application, such as when using the
    numeric keypad to enter data. Typically, the "enter" key will work
    like a "tab" key until the last field on the form. Using
    String.fromCharCode(9) to append to the fields's value only appends a
    "tab" character just like in a word processing application, but does
    not cause the cursor to move to the next field. Any ideas on how to
    make "enter" work like "tab" in Firefox?
    runner7, Sep 10, 2006
    #8
  9. runner7 wrote:
    > Bart Van der Donck wrote:

    <snip>
    >> And honestly I can't think of any good reason why you
    >> would want to fake a keystroke, besides theoretical or
    >> hacking motives.

    >
    > The purpose is for a data entry application, such as when
    > using the numeric keypad to enter data. Typically, the
    > "enter" key will work like a "tab" key until the last
    > field on the form.


    Is that typical of web browsers? That has not been my experience.

    > Using String.fromCharCode(9) to append to the fields's
    > value only appends a "tab" character just like in a
    > word processing application, but does not cause the cursor
    > to move to the next field. Any ideas on how to
    > make "enter" work like "tab" in Firefox?


    While fixating on the insoluble key event problem you are falling to
    notice that what you want to do is focus the next field in the tabbing
    sequence in response to a key event. That can certainly be done (on the
    limited number of browsers you seem interested in) by identifying the
    significant key event, cancelling it, identifying the next filed and
    explicitly calling its - focus - method. It may not be as simple as
    changing the key event, or faking them, but when neither of those will
    do it is a viable alternative.

    Richard.
    Richard Cornford, Sep 10, 2006
    #9
  10. runner7

    runner7 Guest

    > > runner7 wrote:
    > > The purpose is for a data entry application, such as when
    > > using the numeric keypad to enter data. Typically, the
    > > "enter" key will work like a "tab" key until the last
    > > field on the form.

    >
    > Richard Cornford wrote:
    > Is that typical of web browsers? That has not been my experience.


    I meant desktop data-entry applications. This one happens to have a
    browser interface.

    Richard Cornford wrote:
    > That can certainly be done (on the
    > limited number of browsers you seem interested in) by identifying the
    > significant key event, cancelling it, identifying the next filed and
    > explicitly calling its - focus - method.


    Thank you. That is the solution. I wish I could say I had thought of
    it. I am still wondering about the DOM 3 "KeyboardEvent" though, which
    developer.mozilla.org says is supposed to be supported by Firefox's
    Gecko layout engine.
    runner7, Sep 11, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DotNetJunkies User

    Programmatic CheckBox Event Not Firing

    DotNetJunkies User, Oct 21, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    3,474
    =?Utf-8?B?QW5kcmV3?=
    Oct 21, 2004
  2. mee-shell

    Keydown Event and Focus Problem

    mee-shell, Oct 23, 2004, in forum: Python
    Replies:
    0
    Views:
    573
    mee-shell
    Oct 23, 2004
  3. Claudia Fong

    keydown event

    Claudia Fong, Sep 17, 2007, in forum: ASP .Net
    Replies:
    5
    Views:
    1,958
    Claudia Fong
    Sep 18, 2007
  4. Replies:
    1
    Views:
    663
    cfps.Christian
    May 23, 2008
  5. jaspal singh

    trap keypress and keydown event in asp.net mobile form

    jaspal singh, Nov 24, 2006, in forum: ASP .Net Mobile
    Replies:
    1
    Views:
    1,424
Loading...

Share This Page