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发表的文章版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    免费分享各大快递接口代码
    js 拖动div
    前端开发入门学习笔记(一)
    2017年2月14日
    html表格中的tr td th用法
    转:上传图片并给图片加水印(公共类积累)
    通用防SQL注入代码ASP版
    Firebug 在IE Opera Safari 下这样使用
    [正则表达式]文本框输入内容控制
    图片上传添加水印类
  • 原文地址:https://www.cnblogs.com/AsionTang/p/3330917.html
Copyright © 2011-2022 走看看