2

在 AIR 应用程序中使用时,OS X 中的一些标准击键要么被忽略,要么产生“神秘”字符,而不是按预期运行。例子:

  • option+left arrow应该将插入符号向后移动一个单词,它会打印一个神秘字符
  • ctrl+h应该向后删除一个字符,它会打印 h

我如何支持 OS X 默认按键(在此处列出:http ://www.hcs.harvard.edu/~jrus/Site/System%20Bindings.html )?

4

1 回答 1

1

我在 Webkit 源代码中找到了一些线索来启用<mx:HTML>视图支持,这里:http ://www.opensource.apple.com/darwinsource/10.5/WebCore-5523.10.3/editing/Editor.cpp

以及有关 Webkit 预期语法的线索:http: //lists.macosforge.org/pipermail/webkit-unassigned/2007-May/038737.html

请注意,此解决方案不完整,不适用于 Flex/Flash 组件,并且取决于 Prototype:

$(document).observe('dom:loaded', function() {
    // Defined in AIR as !!NativeApplication.supportsDockIcon
    if(window.Air.OperatingSystem == 'mac') {
        var keyBindings = {
            altKey: {
                // Up (should be 38)
                14: {
                    dir: -1,
                    gran: 'paragraph'
                },
                // Down (should be 40)
                15: {
                    dir: 1,
                    gran: 'paragraph'
                },
                // Left (should be 37)
                1: {
                    dir: -1,
                    gran: 'word'
                },
                // Right (should be 39)
                2: {
                    dir: 1,
                    gran: 'word'
                },
                // Backspace
                8: {
                    dir: -1,
                    gran: 'word',
                    del: true
                },
                // Delete
                46: {
                    dir: 1,
                    gran: 'word',
                    del: true
                }
            },
            ctrlKey: {
                // Left
                37: {
                    dir: -1,
                    gran: 'lineBoundary'
                },
                // Right
                39: {
                    dir: 1,
                    gran: 'lineBoundary'
                },
                // a
                65: {
                    dir: -1,
                    gran: 'paragraphBoundary'
                },
                // b
                66: {
                    dir: -1,
                    gran: 'character'
                },
                // d
                68: {
                    dir: 1,
                    gran: 'character',
                    del: true
                },
                // e
                69: {
                    dir: 1,
                    gran: 'paragraphBoundary'
                },
                // f
                70: {
                    dir: 1,
                    gran: 'character'
                },
                // h
                72: {
                    dir: -1,
                    gran: 'character',
                    del: true
                },
                // k
                75: {
                    dir: 1,
                    gran: 'paragraphBoundary',
                    del: true
                },
                // n
                78: {
                    dir: 1,
                    gran: 'line'
                },
                // p
                80: {
                    dir: -1,
                    gran: 'line'
                }
            }
        };
        $(document).observe('keydown', function(e) {
            var target = e.element();
            if(
                target.match('input') ||
                target.match('textarea') ||
                target.readAttribute('contenteditable') == 'true'
            ) {
                if(
                    (e.keyCode in keyBindings.altKey && e.altKey) ||
                    (e.keyCode in keyBindings.ctrlKey && e.ctrlKey)
                ) {
                    e.stop();
                    var selection = window.getSelection();
                    var keystroke = keyBindings[(e.altKey ? 'altKey' : 'ctrlKey')][e.keyCode];
                    if(
                        (e.keyCode != 8 && e.keyCode != 46) ||
                        selection.isCollapsed
                    ) {
                        selection.modify(
                            keystroke.del || e.shiftKey ?
                                'extend' :
                                'move',
                            keystroke.dir == -1 ? 'backward' : keystroke.dir == 1 ? 'forward' : null,
                            keystroke.gran
                        );
                    }
                    if(keystroke.del && !selection.isCollapsed) {
                        document.execCommand('delete', false, null);
                    }
                }
            }
        });
    }
});
于 2008-10-15T18:24:18.407 回答