zoukankan      html  css  js  c++  java
  • WebKit 内核浏览器 initKeyboardEvent 函数原型

    学习JS发送自定义键盘(KeyboardEvent)事件的过程中,遇到了一个小难题:单个按键Tab可以正常发送,焦点能够转移到下一个元素,但想实现Shift+Tab,反向移动焦点时,却被DOM3的浏览器兼容性问题难到了。

    void               initKeyboardEvent(in DOMString typeArg, 
                                           in boolean canBubbleArg, 
                                           in boolean cancelableArg, 
                                           in views::AbstractView viewArg, 
                                           in DOMString keyIdentifierArg, 
                                           in unsigned long keyLocationArg, 
                                           in DOMString modifiersList);

    根据资料1的经验,使用DOM3标准来调用,结果还是失败。

    • 无奈之下,只好下载Webkit源代码直接寻找该核心实现的源代码函数原型,结果如下:
    void KeyboardEvent::initKeyboardEvent(const AtomicString& type, bool canBubble, bool cancelable, AbstractView* view,
                                          const String &keyIdentifier, unsigned location,
                                          bool ctrlKey, bool altKey, bool shiftKey, bool metaKey, bool altGraphKey)
    {
        if (dispatched())
            return;
    
        initUIEvent(type, canBubble, cancelable, view, 0);
    
        m_keyIdentifier = keyIdentifier;
        m_location = location;
        m_ctrlKey = ctrlKey;
        m_shiftKey = shiftKey;
        m_altKey = altKey;
        m_metaKey = metaKey;
        m_altGraphKey = altGraphKey;
    }

    按此原型再次调用,终于成功!(PS:开源就是好啊!)

    模拟Shift+Tab代码如下:

    var evt = document.createEvent("KeyboardEvent");
    evt.initKeyboardEvent("keydown", true, false, window, 'U+0009',0,false,false,true,false,false); 
    e.currentTarget.dispatchEvent(evt);

     By:Asion Tang
    AT:2013年9月20日 23:58:40

    作者:Asion Tang
    凡是没有注明[转载]的文章,本Blog发表的文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    基于jquery实现的文字淡入淡出效果
    js截取小数点后几位的写法
    JS获取IP、MAC和主机名的五种方法
    php checkbox复选框值的获取与checkbox默认值输出方法
    php验证复选框有效性的示例
    【Trapping Rain Water】cpp
    【Valid Sudoku】cpp
    【Permutation Sequence】cpp
    【Next Permutation】cpp
    【Remove Elements】cpp
  • 原文地址:https://www.cnblogs.com/AsionTang/p/3330917.html
Copyright © 2011-2022 走看看