WebKit 内核浏览器 initKeyboardEvent 函数原型

时间:2021-08-14 13:33:49

学习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, ); 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