CefKeyboardHandler
Header: cef_keyboard_handler.h
Category: Browser Handlers
Overview
User-implemented handler interfaces returned from CefClient and registered with CefBrowserHost.
CefKeyboardHandler
Source File: include/cef_keyboard_handler.h
Process / Thread Context: Browser Process / UI Thread (class comment: "The
Purpose: User-implemented interface to handle keyboard events before they
Methods
bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event, bool* is_keyboard_shortcut)
Parameters:
browser: The browser receiving the event.event:CefKeyEventdescribing the keyboard event.os_event: The OS-specific event message (e.g.MSGon Windows), if any.is_keyboard_shortcut: (Out) Set totrueif the event will be handled inOnKeyEventas a keyboard shortcut. Only meaningful when returningfalse.
Return Value: Return true if your handler consumed the event (renderer will not see it). Return false to let the renderer handle it normally; if you set *is_keyboard_shortcut = true while returning false, CEF will route the event to OnKeyEvent afterward (deferred shortcut handling).
Usage Instruction: Use for global hotkeys / accelerator tables that should take precedence over page content (e.g. Ctrl+O for open file, F5 to refresh when the page doesn't already handle it). The is_keyboard_shortcut pattern lets you defer decision until after the page has had a chance to handle the key (so you don't double-process).
Threading Constraint: Called on the UI thread.
bool OnKeyEvent(CefRefPtr<CefBrowser> browser, const CefKeyEvent& event, CefEventHandle os_event)
Parameters:
browser: The browser receiving the event.event:CefKeyEventdescribing the keyboard event.os_event: The OS-specific event message, if any.
Return Value: Return true if your handler consumed the event. Return false for default handling.
Usage Instruction: Fires after the renderer and page JavaScript have had a chance to handle the event. Only called when OnPreKeyEvent returned false with *is_keyboard_shortcut = true (or as otherwise scheduled). Use to implement hotkeys that the page did not handle.
Threading Constraint: Called on the UI thread.
Usage Example
#include "include/cef_keyboard_handler.h"
#include "include/cef_client.h"
class MyKeyboardHandler : public CefKeyboardHandler {
public:
bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event,
bool* is_keyboard_shortcut) override {
// Pre-check: intercept Ctrl+L to focus the URL bar immediately.
if (event.type == KEYEVENT_RAWKEYDOWN &&
event.windows_key_code == 76 /* 'L' */ &&
(event.modifiers & EVENTFLAG_CONTROL_DOWN)) {
// FocusUrlBar(); // your code
return true;
}
// Defer F5 to OnKeyEvent so the page has a chance first.
if (event.type == KEYEVENT_RAWKEYDOWN &&
event.windows_key_code == 116 /* VK_F5 */) {
*is_keyboard_shortcut = true;
return false;
}
return false;
}
bool OnKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event) override {
if (event.type == KEYEVENT_RAWKEYDOWN &&
event.windows_key_code == 116 /* VK_F5 */) {
browser->Reload();
return true;
}
return false;
}
IMPLEMENT_REFCOUNTING(MyKeyboardHandler);
};
class MyClient : public CefClient {
public:
CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() override {
return new MyKeyboardHandler();
}
};