AIR应用程序中的OS X键绑定

时间:2008-10-15 18:20:40

标签: macos keyboard-shortcuts air key-bindings

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

  • 选项 + 左箭头应该将插入符号向后移动一个单词,它会打印出一个神秘的字符
  • ctrl + h 应向后删除一个字符,打印h

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

1 个答案:

答案 0 :(得分: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);
                    }
                }
            }
        });
    }
});