代码之家  ›  专栏  ›  技术社区  ›  eyelidlessness

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

  •  2
  • eyelidlessness  · 技术社区  · 16 年前

    当在Air应用程序中使用时,OS X中的一些标准击键要么被忽略,要么生成“神秘”字符,而不是按预期的方式操作。示例:

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

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

    1 回复  |  直到 8 年前
        1
  •  1
  •   eyelidlessness    16 年前

    我在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组件,取决于原型:

    $(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);
                        }
                    }
                }
            });
        }
    });