0

I'm implementing a keyboard handler that writes a text node, and i'm having difficulties understanding how to handle the case of special characters like accentuated ones such as å or á or even è, usually what i would use depending on the browser would be something like this :

function stringFromKeyPress(event) {
    if (event.which === null || event.which === undefined) {
        return String.fromCharCode(event.keyCode); // IE
    }
    if (event.which !== 0 && event.charCode !== 0) {
        return String.fromCharCode(event.which);   // the rest
    }
    return null; // special key
}

and then from the returned value i would insert the character into the text node, it works perfectly fine for normal characters cause the keyCode or charCode that some browsers return translate perfectly for the Unicode characters that String.fromCharCode(code) accepts. But the problem i have is with the dead-keys or when i have to make a composite combo, for example when i press the grave accent (´) and then press the key (a) i'm expecting the result to be á however the browser fires up one keyup and down event for the (´) with keyCode 221 and then when i press (a) it fires up the keydown->keypress->keyup normal sequence with the (a) code 65 for keydown/up and 97 for keypress.

My question is how do i handle the dead-key composition combo? How do i tell the browser that when i press the grave accent to wait for a possibly valid key that's coming and if it does to input it?

I hope I am clear in my explanation, if you need additional details just ask.

John
  • 2,820
  • 3
  • 30
  • 50

1 Answers1

0

Ok i have found a workaround, it's not the best solution but it works for now until the UIEvents (http://www.w3.org/TR/DOM-Level-3-Events/) are widely implemented and more support for this type of issue comes from the browsers.

What i did was create a text area with opacity set to 0, capture the keyup events on that textarea and then on each of those events get the value from the textarea and put it where i wanted (in my node in the DOM tree) and after that clear the value of the textarea to "".

While it's not the best solution out there it works for what i need it to while the UIEvents aren't here.