• Joel Martin's avatar
    Refactor keyboard event handling. · c96f9003
    Joel Martin authored
    This is part of addressing issue #21 - non-US keyboard layouts.
    
    There are several challenges when dealing with keyboard events:
      - The meaning and use of keyCode, charCode and which depends on
        both the browser and the event type (keyDown/Up vs keyPress).
      - We cannot automatically determine the keyboard layout
      - The keyDown and keyUp events have a keyCode value that has not
        been translated by modifier keys.
      - The keyPress event has a translated (for layout and modifiers)
        character code but the attribute containing it differs. keyCode
        contains the translated value in WebKit (Chrome/Safari), Opera
        11 and IE9. charCode contains the value in WebKit and Firefox.
        The which attribute contains the value on WebKit, Firefox and
        Opera 11.
      - The keyDown/Up keyCode value indicates (sort of) the physical
        key was pressed but only for standard US layout. On a US
        keyboard, the '-' and '_' characters are on the same key and
        generate a keyCode value of 189. But on an AZERTY keyboard even
        though they are different physical keys they both still
        generate a keyCode of 189!
      - To prevent a key event from propagating to the browser and
        causing unwanted default actions (such as closing a tab,
        opening a menu, shifting focus, etc) we must suppress this
        event in both keyDown and keyPress because not all key strokes
        generate on a keyPress event. Also, in WebKit and IE9
        suppressing the keyDown prevents a keyPress but other browsers
        still generated a keyPress even if keyDown is suppressed.
    
    For safe key events, we wait until the keyPress event before
    reporting a key down event. For unsafe key events, we report a key
    down event when the keyDown event fires and we suppress any further
    actions (including keyPress).
    
    In order to report a key up event that matches what we reported
    for the key down event, we keep a list of keys that are currently
    down. When the keyDown event happens, we add the key event to the
    list. If it is a safe key event, then we update the which attribute
    in the most recent item on the list when we received a keyPress
    event (keyPress should immediately follow keyDown). When we
    received a keyUp event we search for the event on the list with
    a matching keyCode and we report the character code using the value
    in the 'which' attribute that was stored with that key.
    
    For character codes above 255 we use a character code to keysym lookup
    table. This is generated using the util/u2x11 script contributed by
    Colin Dean (xvpsource.org).
    c96f9003
input.html 2.39 KB