Skip to content

CefFocusHandler

Header: cef_focus_handler.h
Category: Browser Handlers

Overview

User-implemented handler interfaces returned from CefClient and registered with CefBrowserHost.

CefFocusHandler

Source File: include/cef_focus_handler.h

Process / Thread Context: Browser Process / UI Thread (class comment: "The

Purpose: User-implemented interface to handle focus events — when the

Methods

void OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next)

Parameters:

  • browser: The browser component about to lose focus (e.g. focus was on the last HTML element and the user pressed TAB).
  • next: true if focus is moving to the next component; false if moving to the previous component.

Return Value: None.

Usage Instruction: Use to move focus out of the browser to another widget in your host UI in the appropriate direction (forward/backward tab order).

Threading Constraint: Called on the UI thread.

bool OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source)

Parameters:

  • browser: The browser requesting focus.
  • source: cef_focus_source_t indicating where the focus request originated.

Return Value: Return false to allow the focus to be set on the browser. Return true to cancel setting the focus.

Usage Instruction: Use to suppress programmatic focus grabs (e.g. suppress focus on navigation so the browser doesn't steal focus from another widget).

Threading Constraint: Called on the UI thread.

void OnGotFocus(CefRefPtr<CefBrowser> browser)

Parameters:

  • browser: The browser that has received focus.

Return Value: None.

Usage Instruction: Use to update host UI state when the browser gains focus (e.g. show a caret, change styling, or route focus to a parent window).

Threading Constraint: Called on the UI thread.

Usage Example

cpp
#include "include/cef_focus_handler.h"
#include "include/cef_client.h"

class MyFocusHandler : public CefFocusHandler {
 public:
  void OnTakeFocus(CefRefPtr<CefBrowser> browser, bool next) override {
    // Move focus to the next/previous native widget in your app.
  }

  bool OnSetFocus(CefRefPtr<CefBrowser> browser, FocusSource source) override {
    // Allow focus to be set.
    return false;
  }

  void OnGotFocus(CefRefPtr<CefBrowser> browser) override {
    // Update host UI to reflect that the browser now has focus.
  }

  IMPLEMENT_REFCOUNTING(MyFocusHandler);
};

class MyClient : public CefClient {
 public:
  CefRefPtr<CefFocusHandler> GetFocusHandler() override {
    return new MyFocusHandler();
  }
};

Derived from the CEF C++ headers — © Marshall A. Greenblatt, Google Inc. & contributors (BSD-style license). Not an official CEF project.