5

as we know there is a lot of events will be triggered when we typing.

such as keyup, keydown, keypress or something else.

Is there any other event will be triggered only when the content in the text field is changed? and if there is not,how to write some javasrcipt code to accomplish this feature

1
  • Would you mind elaborating more on your use case? perhaps only way to achieve that is providing a web-based input methods your self. Commented Jun 1, 2012 at 6:53

3 Answers 3

2

The change event might help?

Sign up to request clarification or add additional context in comments.

2 Comments

No,because the change does detect whether the content in the field is changed or not,but it won't be triggered unless you press enter or the field lost focus
Before you commit your string ( such as press "enter"), the string is actually stored in the preedit buffer of input method. i.e. Browser won't know what exactly what you type before you commit the string.
0

I've created a simple fiddle to determine what happens when a text node in a contenteditable element is modified by a key press. As I was doing that, I decided to check what happens when using ibus because I use ibus for my own work. I determined that when ibus is used, the only keyboard event generated is a keyup event with a which value which is useless.

Here is the fiddle I created:

http://jsfiddle.net/lddubeau/jWnL3/

  1. Go to that fiddle.

  2. Turn on your Javascript console.

  3. Click run.

  4. Click between the 2dn and third character of the word "toto".

  5. Type something using ibus. For instance, type 我.

  6. The output varies depending on your browser. On Chrome 29 each keystroke is recorded so if I type 我 using the tonepy method I get keyup and keydown for each of the "w" "o" "3" and "1". (The pinyin, the tone and then hitting 1 to select the first choice.) The final event is:

    event type: keyup which: 229 keyCode: 229 charCode: 0 node value: to我to 

    The events before this one all have the same values except that some are of the "keydown" type and of course the node value shows "toto" until the last event.

    On Firefox 23 only one event is registered:

    "event type:" "keyup" "which:" 49 "keyCode:" 49 "charCode:" 0 "node value:" " to我to " 

    No keydown or keypress events are generated. (When typing characters within the ascii range, you get keydown, keypress and keyup for each key.)

The values of which and keyCode do not seem to correspond to anything sensible. I've examined the event object passed to the event handler and did not see any field which would indicate that the user just typed 我.

Comments

0

If you handle Chinese input and are not interested in intermediate input which triggers change events while entering Pinyin etc. then you may want to look into the compositionstart and compositionend events. These are fired at the beginning and end of composition.

Even latin characters can trigger a composition. For example on iOS when writing `, the backtick character is displayed underlined, indicating that a second character is needed to complete the entry. Selecting e triggers a compositionend event with event.data set to è.

There is also a isComposing attribute on keyboard events such as keydown and keyup that indicates whether the input is part of a composition or not.

Note that you may have to combine different events to get the effect that you want. Perhaps by using compositionstart and compositionend to keep track of whether a composition is happening and ignoring change events that are triggered within compositions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.