Skip to content
Dominique B edited this page Feb 2, 2016 · 2 revisions

KeyboardHandler callbacks

This handler allows you to handle events related to keyboard input and is called only for the main frame.

For an example of how to implement handler see cefpython.CreateBrowser(). For a list of all handler interfaces see API > Client handlers.

CEF 3

bool OnPreKeyEvent(Browser browser, KeyEvent event, MSG*|GdkEvent*|NSEvent* eventHandle, list& isKeyboardShortcutOut)

Called before a keyboard event is sent to the renderer. |event| contains information about the keyboard event. |eventHandle| 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 isKeyboardShortcutOut[0] to true and return false.

KeyEvent is a dictionary with the following keys:

  • "type" (KeyEventType) - The type of keyboard event
  • "modifiers" (uint32) - Bit flags describing any pressed modifier keys. See KeyEventFlags for values.
  • "windows_key_code" (int) - The Windows key code for the key event. This value is used by the DOM specification. Sometimes it comes directly from the event (i.e. on Windows) and sometimes it's determined using a mapping function. See "WebCore/platform/chromium/KeyboardCodes.h" for the list of values.
  • "native_key_code" (int) - The actual key code genenerated by the platform.
  • "is_system_key" (int) - Indicates whether the event is considered a "system key" event (see http://msdn.microsoft.com/en-us/library/ms646286(VS.85).aspx for details). This value will always be false on non-Windows platforms.
  • "character" (wchar_t or unsigned short) - The character generated by the keystroke.
  • "unmodified_character" (wchar_t or unsigned short) - Same as |character| but unmodified by any concurrently-held modifiers (except shift). This is useful for working out shortcut keys.
  • "focus_on_editable_field" (bool) - True if the focus is currently on an editable field on the page. This is useful for determining if standard key events should be intercepted.

KeyEventType is one of:

cefpython.KEYEVENT_RAWKEYDOWN

cefpython.KEYEVENT_KEYDOWN
cefpython.KEYEVENT_KEYUP
cefpython.KEYEVENT_CHAR
KeyEventFlags constants ("modifiers" key):
cefpython.EVENTFLAG_NONE
cefpython.EVENTFLAG_CAPS_LOCK_ON
cefpython.EVENTFLAG_SHIFT_DOWN
cefpython.EVENTFLAG_CONTROL_DOWN
cefpython.EVENTFLAG_ALT_DOWN
cefpython.EVENTFLAG_LEFT_MOUSE_BUTTON
cefpython.EVENTFLAG_MIDDLE_MOUSE_BUTTON
cefpython.EVENTFLAG_RIGHT_MOUSE_BUTTON
// Mac OS-X command key.
cefpython.EVENTFLAG_COMMAND_DOWN
cefpython.EVENTFLAG_NUM_LOCK_ON
cefpython.EVENTFLAG_IS_KEY_PAD
cefpython.EVENTFLAG_IS_LEFT
cefpython.EVENTFLAG_IS_RIGHT

bool OnKeyEvent(Browser browser, KeyEvent event, MSG*|GdkEvent*|NSEvent* eventHandle)

Called after the renderer and JavaScript in the page has had a chance to
handle the event. |event| contains information about the keyboard event.
|os_event| is the operating system event message, if any. Return true if
the keyboard event was handled or false otherwise.
Description of the KeyEvent type is in the OnPreKeyEvent() callback.

CEF 1

bool OnKeyEvent(Browser browser, int eventType, VirtualKey keyCode, int modifiers, bool isSystemKey, bool isAfterJavascript)

Called when the browser component receives a keyboard event. This method
is called both before the event is passed to the renderer and after
javascript in the page has had a chance to handle the event. |type| is the type of keyboard event, |code| is the windows scan-code for the event, |modifiers| is a set of bit- flags describing any pressed modifier keys and |isSystemKey| is true if Windows considers this a WM_SYSKEYDOWN message.
If |isAfterJavaScript| is true then javascript in the page has had a chance to handle the event and has chosen not to. Only KEYEVENT_RAWKEYDOWN, KEYEVENT_KEYDOWN and
KEYEVENT_CHAR events will be sent with |isAfterJavaScript| set to true. Return true if the keyboard event was handled or false to allow continued handling of the event by the renderer.
|eventType| constants:
cefpython.KEYEVENT_RAWKEYDOWN
cefpython.KEYEVENT_KEYDOWN
cefpython.KEYEVENT_KEYUP
cefpython.KEYEVENT_CHAR
Example of implementing KeyboardHandler:
	def OnKeyEvent(browser, eventType, keyCode, modifiers, isSystemKey, isAfterJavascript):
# Let's bind developer tools to F12 key.
if keyCode == cefpython.VK_F12 and eventType == cefpython.KEYEVENT_RAWKEYDOWN and cefpython.IsKeyModifier(cefpython.KEY_NONE, modifiers) and not isSystemKey:
browser.ShowDevTools()
return True
# Bind F5 to refresh browser window.
if keyCode == cefpython.VK_F5 and eventType == cefpython.KEYEVENT_RAWKEYDOWN and cefpython.IsKeyModifier(cefpython.KEY_NONE, modifiers) and not isSystemKey:
browser.ReloadIgnoreCache()
return True
return False
To check against |modifiers| you can use function cefpython.IsKeyModifier(KEY_*, modifiers) where KEY_* is one of:
cefpython.KEY_NONE # neither of alt/shift/ctrl is pressed
cefpython.KEY_SHIFT
cefpython.KEY_CTRL
cefpython.KEY_ALT
cefpython.KEY_META
cefpython.KEY_KEYPAD

Clone this wiki locally