Skip to content

Commit 8a20fdb

Browse files
committed
Fix and handle more keyboard shortcuts on Mac (cztomczak#161)
Handle: copy, paste, cut, undo, redo, select all.
1 parent cc56957 commit 8a20fdb

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

src/handlers/keyboard_handler.pyx

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Project website: https://github.com/cztomczak/cefpython
44

55
include "../cefpython.pyx"
6+
include "../browser.pyx"
67

78
# noinspection PyUnresolvedReferences
89
cimport cef_types
@@ -72,39 +73,67 @@ cdef public cpp_bool KeyboardHandler_OnPreKeyEvent(
7273
(exc_type, exc_value, exc_trace) = sys.exc_info()
7374
sys.excepthook(exc_type, exc_value, exc_trace)
7475

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+
75115
cdef public cpp_bool KeyboardHandler_OnKeyEvent(
76116
CefRefPtr[CefBrowser] cefBrowser,
77117
const cef_types.CefKeyEvent& cefEvent,
78118
cef_types.CefEventHandle cefEventHandle
79119
) except * with gil:
80-
cdef PyBrowser pyBrowser
81-
cdef dict pyEvent
120+
cdef PyBrowser browser
121+
cdef dict event
82122
cdef py_bool returnValue
83123
cdef object callback
84124
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")
88128
if callback:
89129
returnValue = callback(
90-
browser=pyBrowser,
91-
event=pyEvent,
130+
browser=browser,
131+
event=event,
92132
event_handle=<object>PyLong_FromVoidPtr(cefEventHandle))
93133
# If returnValue is False then handle copy/paste on Mac
94134
if returnValue:
95135
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))
108137
except:
109138
(exc_type, exc_value, exc_trace) = sys.exc_info()
110139
sys.excepthook(exc_type, exc_value, exc_trace)

0 commit comments

Comments
 (0)