c – Chromium Embedded Framework绑定按键

我在Chromium Embedded Framework的官方论坛上看到了这个thread,但似乎没有给出解决方案.说实话,我对C平台感到不舒服.你能帮我提供一个将CEF绑定到webapp的片段吗?

我想使用默认控件来控制应用程序:

ALT F4 – 关闭
F5 – 刷新浏览器

解决方法:

简短版本:实现CefKeyboardHandler,特别是OnPreKeyEvent()

ClientHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
    const CefKeyEvent& event,
    CefEventHandle os_event,
    bool* is_keyboard_shortcut) {

    if (os_event && os_event->message == WM_SYSKEYDOWN) {
      case VK_F10: HandleF10(); break;
      case VK_F4: HandlerF4(); break; //Use GetKeyState(VK_MENU) to check if ALT is down...
    }
}

这是在CefClient项目之后,其中ClientHandler实现了CefKeyboardHandler.检查client_handler_win.cpp

更长的版本如下……

看看这个主题 – Keyboard events “eaten” by browser – 这个突出:

When the browser control has the focus, any keys being pressed seem to
be eaten by the browser control, no matter whether they also can be
handled by the browser control or not.

现在有两种选择:

>在发送到CEF引擎之前拦截按键,这需要相当多的挖掘CEF并且是特定于平台的.
>使用普通的Javascript事件处理程序捕获按键,并回调到C.
>如果CEF具有此类接口,则在CEF引擎处理它之前拦截按键 – 理想情况下,这将是平*立的.

在Native-App级别捕获Keypress

在Windows机器上,我尝试搜索WM_KEYDOWN,这是捕获关键事件的常用做法(See Here).我无法对我运行的CefClient项目进行任何点击,所以这是一个死胡同.

任何有关此进一步信息的人,请编辑并添加到此.

在JS中捕获Keypress并回调到C

一旦按键进入CefBrowser,我们总是可以使用Javascript来捕获我们想要的按键,然后调用app处理程序,如下所示:

$(document).keypress(function (e) {
  ...
  NativeAppFunction();
  //Or NativeAppExtension.Function();
}

JS和C之间的通信是通过V8Extensions或通过将Function绑定到CefContext来完成的.更多信息,请访问Javascript Integration

这带来了一些陷阱 – 你的事件捕获器是’只是另一个Javascript事件处理程序’,并且随之而来的是它被调用的时间(在其他事件处理程序之前或之后)的所有不确定性等等.值得庆幸的是,CEF有一个漂亮的小CefKeyboardHandler只是为了做你想要的!

使用CefKeyboardHandler拦截Keypress

请参阅cef_keyboard_handler.h – OnPreKeyEvent()的文档说:

// Called before a keyboard event is sent to the renderer. |event| contains
// information about the keyboard event. |os_event| is the operating system
// event message, if any. Return true if the event was handled or false
// otherwise. If the event will be handled in OnKeyEvent() as a keyboard
// shortcut set |is_keyboard_shortcut| to true and return false.

从这里开始,它非常简单. CefEventHandle解析为特定平台(遗憾的是 – 哦!)标准的Windows MSG.请注意,Alt F4是一个特殊的系统命令:

When you press a key, the window that has keyboard focus receives one
of the following messages.

WM_SYSKEYDOWN (or) WM_KEYDOWN

The WM_SYSKEYDOWN message indicates a system key, which is a key stroke that invokes a
system command. There are two types of system key: ALT + any key and F10

Full text at MSDN

上一篇:javascript – jQuery事件Keypress:按下了哪个键?


下一篇:Sql中联合查询中的”子查询返回的值不止一个“的问题