X Tutup
Skip to content

Commit 112daba

Browse files
CzarekCzarek
authored andcommitted
Implemented keyboard handler in CEF Python 3.
1 parent 6a8abe2 commit 112daba

File tree

15 files changed

+305
-8
lines changed

15 files changed

+305
-8
lines changed

cefpython/browser.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,12 @@ cdef class PyBrowser:
200200
cpdef py_void SetClientCallback_CEF3(self,
201201
py_string name, object callback):
202202
if not self.allowedClientCallbacks:
203-
# CefDisplayHandler.
203+
# CefDisplayHandler
204204
self.allowedClientCallbacks += ["OnLoadingStateChange",
205205
"OnAddressChange", "OnTitleChange", "OnTooltip",
206206
"OnStatusMessage", "OnConsoleMessage"]
207+
# CefKeyboardHandler
208+
self.allowedClientCallbacks += ["OnPreKeyEvent", "OnKeyEvent"];
207209
if name not in self.allowedClientCallbacks:
208210
raise Exception("Browser.SetClientCallback() failed: unknown "
209211
"callback: %s" % name)

cefpython/cef3/client_handler/client_handler.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,43 @@ bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
299299
return DisplayHandler_OnConsoleMessage(browser, message, source, line);
300300
// return false;
301301
}
302+
303+
// ----------------------------------------------------------------------------
304+
// CefKeyboardHandler
305+
// ----------------------------------------------------------------------------
306+
307+
///
308+
// Implement this interface to handle events related to keyboard input. The
309+
// methods of this class will be called on the UI thread.
310+
///
311+
312+
// Called before a keyboard event is sent to the renderer. |event| contains
313+
// information about the keyboard event. |os_event| is the operating system
314+
// event message, if any. Return true if the event was handled or false
315+
// otherwise. If the event will be handled in OnKeyEvent() as a keyboard
316+
// shortcut set |is_keyboard_shortcut| to true and return false.
317+
/*--cef()--*/
318+
bool ClientHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
319+
const CefKeyEvent& event,
320+
CefEventHandle os_event,
321+
bool* is_keyboard_shortcut) {
322+
REQUIRE_UI_THREAD();
323+
return KeyboardHandler_OnPreKeyEvent(browser, event, os_event,
324+
is_keyboard_shortcut);
325+
// Default: return false;
326+
}
327+
328+
///
329+
// Called after the renderer and JavaScript in the page has had a chance to
330+
// handle the event. |event| contains information about the keyboard event.
331+
// |os_event| is the operating system event message, if any. Return true if
332+
// the keyboard event was handled or false otherwise.
333+
///
334+
/*--cef()--*/
335+
bool ClientHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser,
336+
const CefKeyEvent& event,
337+
CefEventHandle os_event) {
338+
REQUIRE_UI_THREAD();
339+
return KeyboardHandler_OnKeyEvent(browser, event, os_event);
340+
// Default: return false;
341+
}

cefpython/cef3/client_handler/client_handler.h

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
class ClientHandler :
1414
public CefClient,
1515
public CefLifeSpanHandler,
16-
public CefDisplayHandler
16+
public CefDisplayHandler,
17+
public CefKeyboardHandler
1718
{
1819
public:
1920
ClientHandler(){}
@@ -93,7 +94,7 @@ class ClientHandler :
9394
///
9495
/*--cef()--*/
9596
virtual CefRefPtr<CefKeyboardHandler> GetKeyboardHandler() OVERRIDE {
96-
return NULL;
97+
return this;
9798
}
9899

99100
///
@@ -322,6 +323,37 @@ class ClientHandler :
322323
const CefString& source,
323324
int line) OVERRIDE;
324325

326+
// --------------------------------------------------------------------------
327+
// CefKeyboardHandler
328+
// --------------------------------------------------------------------------
329+
330+
///
331+
// Implement this interface to handle events related to keyboard input. The
332+
// methods of this class will be called on the UI thread.
333+
///
334+
335+
// Called before a keyboard event is sent to the renderer. |event| contains
336+
// information about the keyboard event. |os_event| is the operating system
337+
// event message, if any. Return true if the event was handled or false
338+
// otherwise. If the event will be handled in OnKeyEvent() as a keyboard
339+
// shortcut set |is_keyboard_shortcut| to true and return false.
340+
/*--cef()--*/
341+
virtual bool OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
342+
const CefKeyEvent& event,
343+
CefEventHandle os_event,
344+
bool* is_keyboard_shortcut) OVERRIDE;
345+
346+
///
347+
// Called after the renderer and JavaScript in the page has had a chance to
348+
// handle the event. |event| contains information about the keyboard event.
349+
// |os_event| is the operating system event message, if any. Return true if
350+
// the keyboard event was handled or false otherwise.
351+
///
352+
/*--cef()--*/
353+
virtual bool OnKeyEvent(CefRefPtr<CefBrowser> browser,
354+
const CefKeyEvent& event,
355+
CefEventHandle os_event) OVERRIDE;
356+
325357
private:
326358

327359
// Include the default reference counting implementation.

cefpython/cef3/linux/binaries_32bit/wxpython.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,28 @@ <h3>Display handler</h3>
185185
print("line = %s" % line)
186186
</pre>
187187

188+
189+
190+
191+
<h3>Keyboard handler</h3>
192+
<p>
193+
Click anywhere in the window first to set keyboard focus.
194+
Next press F5 to reload the page.
195+
</p>
196+
<pre>
197+
def OnPreKeyEvent(self, browser, event, eventHandle,
198+
isKeyboardShortcutOut):
199+
print("ClientHandler::OnPreKeyEvent()")
200+
201+
def OnKeyEvent(self, browser, event, eventHandle):
202+
print("ClientHandler::OnKeyEvent()")
203+
print("native_key_code = %s" % event["native_key_code"])
204+
if platform.system() == "Linux":
205+
# F5 = 71
206+
if event["native_key_code"] == 71:
207+
print("F5 pressed! Reloading page..")
208+
browser.ReloadIgnoreCache()
209+
</pre>
210+
188211
</body>
189212
</html>

cefpython/cef3/linux/binaries_32bit/wxpython.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import time
1818
import re
1919
import uuid
20+
import platform
2021

2122
# Which method to use for message loop processing.
2223
# EVT_IDLE - wx application has priority (default)
@@ -198,9 +199,9 @@ def OnTitleChange(self, browser, title):
198199
print("ClientHandler::OnTitleChange()")
199200
print("title = %s" % title)
200201

201-
def OnTooltip(self, browser, text):
202+
def OnTooltip(self, browser, textOut):
202203
print("ClientHandler::OnTooltip()")
203-
print("text = %s")
204+
print("text = %s" % textOut[0])
204205
# OnTooltip seems not to work on Linux, reported bug on the CEF forum:
205206
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10898
206207

@@ -214,6 +215,22 @@ def OnConsoleMessage(self, browser, message, source, line):
214215
print("source = %s" % source)
215216
print("line = %s" % line)
216217

218+
# -------------------------------------------------------------------------
219+
# KeyboardHandler
220+
# -------------------------------------------------------------------------
221+
def OnPreKeyEvent(self, browser, event, eventHandle,
222+
isKeyboardShortcutOut):
223+
print("ClientHandler::OnPreKeyEvent()")
224+
225+
def OnKeyEvent(self, browser, event, eventHandle):
226+
print("ClientHandler::OnKeyEvent()")
227+
print("native_key_code = %s" % event["native_key_code"])
228+
if platform.system() == "Linux":
229+
# F5 = 71
230+
if event["native_key_code"] == 71:
231+
print("F5 pressed! Reloading page..")
232+
browser.ReloadIgnoreCache()
233+
217234
class MyApp(wx.App):
218235
timer = None
219236
timerID = 1

cefpython/cef3/linux/binaries_64bit/wxpython.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,5 +185,28 @@ <h3>Display handler</h3>
185185
print("line = %s" % line)
186186
</pre>
187187

188+
189+
190+
191+
<h3>Keyboard handler</h3>
192+
<p>
193+
Click anywhere in the window first to set keyboard focus.
194+
Next press F5 to reload the page.
195+
</p>
196+
<pre>
197+
def OnPreKeyEvent(self, browser, event, eventHandle,
198+
isKeyboardShortcutOut):
199+
print("ClientHandler::OnPreKeyEvent()")
200+
201+
def OnKeyEvent(self, browser, event, eventHandle):
202+
print("ClientHandler::OnKeyEvent()")
203+
print("native_key_code = %s" % event["native_key_code"])
204+
if platform.system() == "Linux":
205+
# F5 = 71
206+
if event["native_key_code"] == 71:
207+
print("F5 pressed! Reloading page..")
208+
browser.ReloadIgnoreCache()
209+
</pre>
210+
188211
</body>
189212
</html>

cefpython/cef3/linux/binaries_64bit/wxpython.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import time
1818
import re
1919
import uuid
20+
import platform
2021

2122
# Which method to use for message loop processing.
2223
# EVT_IDLE - wx application has priority (default)
@@ -198,9 +199,9 @@ def OnTitleChange(self, browser, title):
198199
print("ClientHandler::OnTitleChange()")
199200
print("title = %s" % title)
200201

201-
def OnTooltip(self, browser, text):
202+
def OnTooltip(self, browser, textOut):
202203
print("ClientHandler::OnTooltip()")
203-
print("text = %s")
204+
print("text = %s" % textOut[0])
204205
# OnTooltip seems not to work on Linux, reported bug on the CEF forum:
205206
# http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10898
206207

@@ -214,6 +215,22 @@ def OnConsoleMessage(self, browser, message, source, line):
214215
print("source = %s" % source)
215216
print("line = %s" % line)
216217

218+
# -------------------------------------------------------------------------
219+
# KeyboardHandler
220+
# -------------------------------------------------------------------------
221+
def OnPreKeyEvent(self, browser, event, eventHandle,
222+
isKeyboardShortcutOut):
223+
print("ClientHandler::OnPreKeyEvent()")
224+
225+
def OnKeyEvent(self, browser, event, eventHandle):
226+
print("ClientHandler::OnKeyEvent()")
227+
print("native_key_code = %s" % event["native_key_code"])
228+
if platform.system() == "Linux":
229+
# F5 = 71
230+
if event["native_key_code"] == 71:
231+
print("F5 pressed! Reloading page..")
232+
browser.ReloadIgnoreCache()
233+
217234
class MyApp(wx.App):
218235
timer = None
219236
timerID = 1

cefpython/cef3/linux/setup/cefpython.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ __PYX_EXTERN_C DL_IMPORT(void) DisplayHandler_OnTitleChange(CefRefPtr<CefBrowser
2424
__PYX_EXTERN_C DL_IMPORT(bool) DisplayHandler_OnTooltip(CefRefPtr<CefBrowser>, CefString &);
2525
__PYX_EXTERN_C DL_IMPORT(void) DisplayHandler_OnStatusMessage(CefRefPtr<CefBrowser>, CefString const &);
2626
__PYX_EXTERN_C DL_IMPORT(bool) DisplayHandler_OnConsoleMessage(CefRefPtr<CefBrowser>, CefString const &, CefString const &, int);
27+
__PYX_EXTERN_C DL_IMPORT(bool) KeyboardHandler_OnPreKeyEvent(CefRefPtr<CefBrowser>, CefKeyEvent const &, CefEventHandle, bool *);
28+
__PYX_EXTERN_C DL_IMPORT(bool) KeyboardHandler_OnKeyEvent(CefRefPtr<CefBrowser>, CefKeyEvent const &, CefEventHandle);
2729

2830
#endif /* !__PYX_HAVE_API__cefpython_py27 */
2931

cefpython/cefpython.pyx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ include "javascript_bindings.pyx"
124124

125125
IF CEF_VERSION == 1:
126126
include "load_handler.pyx"
127-
include "keyboard_handler.pyx"
127+
include "keyboard_handler_cef1.pyx"
128128
include "virtual_keys.pyx"
129129
include "request.pyx"
130130
include "web_request.pyx"
@@ -154,6 +154,7 @@ IF CEF_VERSION == 3:
154154
include "python_callback_cef3.pyx"
155155
include "lifespan_handler_cef3.pyx"
156156
include "display_handler_cef3.pyx"
157+
include "keyboard_handler_cef3.pyx"
157158

158159
# Try not to run any of the CEF code until Initialize() is called.
159160
# Do not allocate any memory on the heap until Initialize() is called,

cefpython/cython_includes/cef_linux.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ cdef extern from "include/internal/cef_linux.h":
2020
cdef cppclass CefMainArgs(CefStructBase):
2121
CefMainArgs()
2222
CefMainArgs(int argc_arg, char** argv_arg)
23+

0 commit comments

Comments
 (0)
X Tutup