3838- Performance is still not perfect, see Issue #324 for further details
3939- Keyboard modifiers that are not yet handled in this example:
4040 ctrl, marking text inputs with the shift key.
41- - Backspace key doesn't work on Mac
4241- Dragging with mouse not implemented
4342- Window size is fixed, cannot be resized
4443
@@ -96,7 +95,19 @@ def die(msg):
9695 " To install type: pip install Pillow" )
9796
9897
98+ if sys .platform == 'darwin' :
99+ try :
100+ import AppKit
101+ except ImportError :
102+ die ("ERROR: pyobjc package not found\n "
103+ " To install type: pip install pyobjc" )
104+
105+
99106def main ():
107+ """
108+ Parses input, initializes everything and then runs the main loop of the
109+ program, which handles input and draws the scene.
110+ """
100111 parser = argparse .ArgumentParser (
101112 description = 'PySDL2 / cefpython example' ,
102113 add_help = True
@@ -159,6 +170,14 @@ def main():
159170 }
160171 cef .Initialize (settings = {"windowless_rendering_enabled" : True },
161172 switches = switches )
173+
174+ if sys .platform == 'darwin' :
175+ # On MacOS, the NSApplication created in the cefpython initialization
176+ # will be hidden if windowless is specified. In order for SDL to receive
177+ # propper input events and for the application to show up in the
178+ # command-tab list, the application must be made "regular".
179+ AppKit .NSApplication .sharedApplication ().setActivationPolicy_ (
180+ AppKit .NSApplicationActivationPolicyRegular )
162181 logging .debug ("cef initialised" )
163182 window_info = cef .WindowInfo ()
164183 window_info .SetAsOffscreen (0 )
@@ -207,6 +226,7 @@ def main():
207226 # viewport size is available and that OnPaint may be called.
208227 browser .SendFocusEvent (True )
209228 browser .WasResized ()
229+
210230 # Begin the main rendering loop
211231 running = True
212232 # FPS debug variables
@@ -324,8 +344,11 @@ def main():
324344 key_event = {
325345 "type" : cef .KEYEVENT_RAWKEYDOWN ,
326346 "windows_key_code" : keycode ,
327- "character" : keycode ,
328- "unmodified_character" : keycode ,
347+ "native_key_code" : get_native_key (keycode ),
348+ # For raw key events, the character and unmodified
349+ # character codes should be 0.
350+ "character" : 0 ,
351+ "unmodified_character" : 0 ,
329352 "modifiers" : cef .EVENTFLAG_NONE
330353 }
331354 browser .SendKeyEvent (key_event )
@@ -348,6 +371,11 @@ def main():
348371 key_event = {
349372 "type" : cef .KEYEVENT_KEYUP ,
350373 "windows_key_code" : keycode ,
374+ "native_key_code" : get_native_key (keycode ),
375+ # On raw key up events, the character and unmodified
376+ # character need to be defined, otherwise pressing
377+ # one of the above-listed keys will eat the next
378+ # normal keypress.
351379 "character" : keycode ,
352380 "unmodified_character" : keycode ,
353381 "modifiers" : cef .EVENTFLAG_NONE
@@ -413,6 +441,34 @@ def get_key_code(key):
413441 return None
414442
415443
444+ # The key events on MacOS have different native keycode than on other operating
445+ # systems. This table is a translation from Windows-keycodes to MacOS ones.
446+ MACOS_TRANSLATION_TABLE = {
447+ # Backspace
448+ 0x08 : 0x33 ,
449+
450+ # Left arrow
451+ 0x25 : 0x7B ,
452+ # Up arrow
453+ 0x26 : 0x7E ,
454+ # Right arrow
455+ 0x27 : 0x7C ,
456+ # Down arrow
457+ 0x28 : 0x7D ,
458+ }
459+
460+
461+ def get_native_key (key ):
462+ """
463+ Helper function for returning the correct native key map for the operating
464+ system.
465+ """
466+ if sys .platform == 'darwin' :
467+ return MACOS_TRANSLATION_TABLE .get (key , key )
468+
469+ return key
470+
471+
416472class LoadHandler (object ):
417473 """Simple handler for loading URLs."""
418474
0 commit comments