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

  • 选项 + 左箭头 应该将插入符号向后移动一个字,它会打印一个神秘字符
  • 控制键 + H 应该向后删除一个字符,它会打印 h

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

有帮助吗?

解决方案

我在 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);
                    }
                }
            }
        });
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top