|
3 | 3 | # Project website: https://github.com/cztomczak/cefpython |
4 | 4 |
|
5 | 5 | include "../cefpython.pyx" |
| 6 | +include "../browser.pyx" |
6 | 7 |
|
7 | 8 | # noinspection PyUnresolvedReferences |
8 | 9 | cimport cef_types |
@@ -72,39 +73,67 @@ cdef public cpp_bool KeyboardHandler_OnPreKeyEvent( |
72 | 73 | (exc_type, exc_value, exc_trace) = sys.exc_info() |
73 | 74 | sys.excepthook(exc_type, exc_value, exc_trace) |
74 | 75 |
|
| 76 | + |
| 77 | +cdef py_bool HandleKeyboardShortcuts(PyBrowser browser, dict event): |
| 78 | + """Default implementation for keyboard shortcuts on Mac (Issue #161).""" |
| 79 | + if platform.system() == "Darwin": |
| 80 | + if event["modifiers"] == 128 \ |
| 81 | + and event["type"] != KEYEVENT_RAWKEYDOWN: |
| 82 | + # Copy and paste was handled in RAWKEYDOWN, return True |
| 83 | + if event["native_key_code"] in [8, 9]: |
| 84 | + return True |
| 85 | + if event["modifiers"] == 128 \ |
| 86 | + and event["type"] == KEYEVENT_RAWKEYDOWN: |
| 87 | + # Select all: command + a |
| 88 | + if event["native_key_code"] == 0: |
| 89 | + browser.GetMainFrame().SelectAll() |
| 90 | + return True |
| 91 | + # Copy: command + c |
| 92 | + elif event["native_key_code"] == 8: |
| 93 | + browser.GetMainFrame().Copy() |
| 94 | + return True |
| 95 | + # Paste: command + v |
| 96 | + elif event["native_key_code"] == 9: |
| 97 | + browser.GetMainFrame().Paste() |
| 98 | + return True |
| 99 | + # Cut: command + x |
| 100 | + elif event["native_key_code"] == 7: |
| 101 | + browser.GetMainFrame().Cut() |
| 102 | + return True |
| 103 | + # Undo: command + z |
| 104 | + elif event["native_key_code"] == 6: |
| 105 | + browser.GetMainFrame().Undo() |
| 106 | + return True |
| 107 | + elif event["modifiers"] == 130 \ |
| 108 | + and event["type"] == KEYEVENT_RAWKEYDOWN: |
| 109 | + # Redo: command + shift + z |
| 110 | + if event["native_key_code"] == 6: |
| 111 | + browser.GetMainFrame().Redo() |
| 112 | + return True |
| 113 | + return False |
| 114 | + |
75 | 115 | cdef public cpp_bool KeyboardHandler_OnKeyEvent( |
76 | 116 | CefRefPtr[CefBrowser] cefBrowser, |
77 | 117 | const cef_types.CefKeyEvent& cefEvent, |
78 | 118 | cef_types.CefEventHandle cefEventHandle |
79 | 119 | ) except * with gil: |
80 | | - cdef PyBrowser pyBrowser |
81 | | - cdef dict pyEvent |
| 120 | + cdef PyBrowser browser |
| 121 | + cdef dict event |
82 | 122 | cdef py_bool returnValue |
83 | 123 | cdef object callback |
84 | 124 | try: |
85 | | - pyBrowser = GetPyBrowser(cefBrowser, "OnKeyEvent") |
86 | | - pyEvent = CefToPyKeyEvent(cefEvent) |
87 | | - callback = pyBrowser.GetClientCallback("OnKeyEvent") |
| 125 | + browser = GetPyBrowser(cefBrowser, "OnKeyEvent") |
| 126 | + event = CefToPyKeyEvent(cefEvent) |
| 127 | + callback = browser.GetClientCallback("OnKeyEvent") |
88 | 128 | if callback: |
89 | 129 | returnValue = callback( |
90 | | - browser=pyBrowser, |
91 | | - event=pyEvent, |
| 130 | + browser=browser, |
| 131 | + event=event, |
92 | 132 | event_handle=<object>PyLong_FromVoidPtr(cefEventHandle)) |
93 | 133 | # If returnValue is False then handle copy/paste on Mac |
94 | 134 | if returnValue: |
95 | 135 | return bool(returnValue) |
96 | | - if platform.system() == "Darwin": |
97 | | - # Handle copy: command + c |
98 | | - if pyEvent["modifiers"] == 128 \ |
99 | | - and pyEvent["native_key_code"] == 8: |
100 | | - pyBrowser.GetFocusedFrame().Copy() |
101 | | - return True |
102 | | - # Handle paste: command + v |
103 | | - elif pyEvent["modifiers"] == 128 \ |
104 | | - and pyEvent["native_key_code"] == 9: |
105 | | - pyBrowser.GetFocusedFrame().Paste() |
106 | | - return True |
107 | | - return False |
| 136 | + return bool(HandleKeyboardShortcuts(browser, event)) |
108 | 137 | except: |
109 | 138 | (exc_type, exc_value, exc_trace) = sys.exc_info() |
110 | 139 | sys.excepthook(exc_type, exc_value, exc_trace) |
0 commit comments