Chromium嵌入式框架绑定键按下 [英] Chromium Embedded Framework bind key press

查看:1507
本文介绍了Chromium嵌入式框架绑定键按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在官方论坛上看到这个主题的Chromium嵌入式框架,但似乎没有给出解决方案。说实话,我不喜欢C ++平台。



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



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();打破;
case VK_F4:HandlerF4();打破; // Use GetKeyState(VK_MENU)to check if ALT is down ...
}
}

这跟在 CefClient 项目之后,其中 ClientHandler implements CefKeyboardHandler 。检查 client_handler_win.cpp



更长版本如下...






查看此主题 - 键盘事件由浏览器吃掉 - 这突出显示:


当浏览器控件具有焦点时,任何键被按下似乎
被浏览器控制器吃掉,不管他们是否也可以是由浏览器控件处理的


现在有两个选项:


  1. 截取按键发送到CEF引擎,这将需要相当多的挖入CEF和平台特定。


  2. 捕获按键使用

  3. 在CEF引擎处理之前拦截按键




在本机应用程序级捕获按键



在Windows机器上,我尝试搜索 WM_KEYDOWN ,这是捕获关键事件的惯例(查看此处)。我无法得到任何点击在 CefClient 项目我运行,所以这是一个死胡同。



>在JS中捕获按键并回调到C ++

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

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

在JS和C ++之间通信是通过V8Extensions或者通过将一个函数绑定到CefContext来实现的。更多信息,请参见 Javascript集成



这有一些缺陷 - 你的事件捕获器只是另一个Javascript事件处理器,幸运的是,CEF有一个漂亮的小 CefKeyboardHandler 只是为了



使用 CefKeyboardHandler拦截按键



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

  //在将键盘事件发送到渲染器之前调用。 | event |包含
//关于键盘事件的信息。 | os_event |是操作系统
//事件消息,如果有的话。如果事件被处理,则返回true;否则返回
//。如果事件将作为键盘在OnKeyEvent()中处理
//快捷方式set | is_keyboard_shortcut |到true并返回false。

从这里,它很简单。 CefEventHandle 解析为一个平台特定的(可悲的是哦哦!)标准Windows MSG 。注意,Alt + F4是一个特殊的系统命令:


当你按下一个键时,具有键盘焦点的窗口会收到一个
以下消息。



WM_SYSKEYDOWN(或)WM_KEYDOWN



WM_SYSKEYDOWN讯息表示系统金钥, a
系统命令。有两种类型的系统键:ALT +任何键和F10


MSDN全文


I saw this thread on the official forum of Chromium Embedded Framework but it seems that there were no solutions given. To be honest, I'm not comfortable with C++ platform. Could you help me provide a snippet that binds CEF to the webapp.

I wanted to control the application using the default controls:

ALT+F4 - close
F5 - refresh browser

解决方案

Short Version: Implement CefKeyboardHandler, specifically 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...
    }
}

This follows the CefClient project, where ClientHandler implements CefKeyboardHandler. Check client_handler_win.cpp

Longer version follows...


Looking at this thread - Keyboard events "eaten" by browser - this stands out:

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.

There are now two options:

  1. Intercept the key-press before it is sent to the CEF engine, which would require quite a little bit of digging into CEF and be platform specific.

  2. Capture the key-press using a normal Javascript event handler, and callback to C++.

  3. Intercept the key-press before the CEF engine processes it if CEF has such interfaces - this would ideally be platform independant.

Capture Keypress at Native-App level

On a windows machine, I tried searching for WM_KEYDOWN which is the usual practice in capturing key events (See Here). I was unable to get any hits on the CefClient project I was running, so this was a dead end.

Anybody with further info on this, please edit and add to this.

Capture Keypress in JS and callback to C++

Once the keypress goes into CefBrowser, we could always use Javascript to capture the keypress we want and then call an app handler, like so:

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

Communicating between JS and C++ is done through either V8Extensions, or by binding a Function to the CefContext. Further info at Javascript Integration

This comes with some pitfalls - your event-capturer is 'just another Javascript event handler', and with that comes all the uncertainties of when it is called (before or after other event handlers) and so on. Thankfully, CEF has a nifty little CefKeyboardHandler just to do what you want!

Intercept Keypress using CefKeyboardHandler

See cef_keyboard_handler.h - the doc for OnPreKeyEvent() says:

// 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.

From here, it's pretty straightforward. The CefEventHandle resolves to a platform specific (sadly - oh well!) standard Windows MSG. Note that Alt+F4 is a special system command:

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

这篇关于Chromium嵌入式框架绑定键按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆