/GUI/Cocoa/Events.py

https://bitbucket.org/alsh/pygui-mirror · Python · 158 lines · 138 code · 11 blank · 9 comment · 16 complexity · 748ed31c5c02fd00da2b7fbd3ade588b MD5 · raw file

  1. #
  2. # Python GUI - Events - PyObjC version
  3. #
  4. import AppKit
  5. from AppKit import NSEvent, \
  6. NSShiftKeyMask, NSControlKeyMask, NSCommandKeyMask, NSAlternateKeyMask
  7. import Globals
  8. from GEvents import Event as GEvent
  9. _ns_event_type_to_kind = {
  10. AppKit.NSLeftMouseDown: 'mouse_down',
  11. AppKit.NSLeftMouseUp: 'mouse_up',
  12. AppKit.NSRightMouseDown: 'mouse_down',
  13. AppKit.NSRightMouseUp: 'mouse_up',
  14. AppKit.NSOtherMouseDown: 'mouse_down',
  15. AppKit.NSOtherMouseUp: 'mouse_up',
  16. AppKit.NSMouseMoved: 'mouse_move',
  17. AppKit.NSLeftMouseDragged: 'mouse_drag',
  18. AppKit.NSRightMouseDragged: 'mouse_drag',
  19. AppKit.NSOtherMouseDragged: 'mouse_drag',
  20. AppKit.NSMouseEntered: 'mouse_enter',
  21. AppKit.NSMouseExited: 'mouse_leave',
  22. AppKit.NSKeyDown: 'key_down',
  23. AppKit.NSKeyUp: 'key_up',
  24. AppKit.NSFlagsChanged: 'flags_changed',
  25. AppKit.NSAppKitDefined: 'app_kit_defined',
  26. AppKit.NSSystemDefined: 'system_defined',
  27. AppKit.NSApplicationDefined: 'application_defined',
  28. AppKit.NSPeriodic: 'periodic',
  29. AppKit.NSCursorUpdate: 'cursor_update',
  30. }
  31. _ns_event_type_to_button = {
  32. AppKit.NSLeftMouseDown: 'left',
  33. AppKit.NSLeftMouseUp: 'left',
  34. AppKit.NSRightMouseDown: 'right',
  35. AppKit.NSRightMouseUp: 'right',
  36. AppKit.NSOtherMouseDown: 'middle',
  37. AppKit.NSOtherMouseUp: 'middle',
  38. AppKit.NSLeftMouseDragged: 'left',
  39. AppKit.NSRightMouseDragged: 'right',
  40. AppKit.NSOtherMouseDragged: 'middle',
  41. }
  42. _ns_keycode_to_keyname = {
  43. AppKit.NSUpArrowFunctionKey: 'up_arrow',
  44. AppKit.NSDownArrowFunctionKey: 'down_arrow',
  45. AppKit.NSLeftArrowFunctionKey: 'left_arrow',
  46. AppKit.NSRightArrowFunctionKey: 'right_arrow',
  47. AppKit.NSF1FunctionKey: 'f1',
  48. AppKit.NSF2FunctionKey: 'f2',
  49. AppKit.NSF3FunctionKey: 'f3',
  50. AppKit.NSF4FunctionKey: 'f4',
  51. AppKit.NSF5FunctionKey: 'f5',
  52. AppKit.NSF6FunctionKey: 'f6',
  53. AppKit.NSF7FunctionKey: 'f7',
  54. AppKit.NSF8FunctionKey: 'f8',
  55. AppKit.NSF9FunctionKey: 'f9',
  56. AppKit.NSF10FunctionKey: 'f10',
  57. AppKit.NSF11FunctionKey: 'f11',
  58. AppKit.NSF12FunctionKey: 'f12',
  59. AppKit.NSF13FunctionKey: 'f13',
  60. AppKit.NSF14FunctionKey: 'f14',
  61. AppKit.NSF15FunctionKey : 'f15',
  62. AppKit.NSDeleteFunctionKey: 'delete',
  63. AppKit.NSHomeFunctionKey: 'home',
  64. AppKit.NSEndFunctionKey: 'end',
  65. AppKit.NSPageUpFunctionKey: 'page_up',
  66. AppKit.NSPageDownFunctionKey: 'page_down',
  67. AppKit.NSClearLineFunctionKey: 'clear',
  68. #AppKit.NSHelpFunctionKey: 'help',
  69. AppKit.NSHelpFunctionKey: 'insert',
  70. "\x03": 'enter',
  71. }
  72. _mouse_events = [
  73. 'mouse_down', 'mouse_drag', 'mouse_up',
  74. 'mouse_move', 'mouse_enter', 'mouse_exit'
  75. ]
  76. _key_events = [
  77. 'key_down', 'key_up'
  78. ]
  79. _ns_screen_height = None
  80. class Event(GEvent):
  81. """Platform-dependent modifiers (boolean):
  82. command The Macintosh Command key.
  83. option The Macintosh Option key.
  84. """
  85. global_position = (0, 0)
  86. position = (0, 0)
  87. button = ''
  88. num_clicks = 0
  89. char = ""
  90. unichars = ""
  91. key = ''
  92. auto = False
  93. delta = (0, 0)
  94. def __init__(self, ns_event):
  95. self._ns_event = ns_event
  96. _ns_type = ns_event.type()
  97. kind = _ns_event_type_to_kind[_ns_type]
  98. self.kind = kind
  99. self.time = ns_event.timestamp()
  100. ns_window = ns_event.window()
  101. is_mouse_event = kind in _mouse_events
  102. if is_mouse_event:
  103. ns_win_pos = ns_event.locationInWindow()
  104. x, y = ns_window.convertBaseToScreen_(ns_win_pos)
  105. else:
  106. ns_last_mouse = Globals.ns_last_mouse_moved_event
  107. if ns_last_mouse:
  108. ns_window = ns_last_mouse.window()
  109. if ns_window:
  110. ns_win_pos = ns_last_mouse.locationInWindow()
  111. x, y = ns_window.convertBaseToScreen_(ns_win_pos)
  112. else:
  113. x, y = ns_last_mouse.locationInWindow()
  114. else:
  115. x, y = NSEvent.mouseLocation()
  116. h = Globals.ns_screen_height
  117. self.global_position = (x, h - y)
  118. if is_mouse_event:
  119. self.button = _ns_event_type_to_button.get(_ns_type, '')
  120. if kind == 'mouse_down':
  121. self.num_clicks = ns_event.clickCount()
  122. self.delta = (ns_event.deltaX(), ns_event.deltaY())
  123. ns_flags = ns_event.modifierFlags()
  124. self.shift = self.extend_contig = (ns_flags & NSShiftKeyMask) <> 0
  125. self.control = (ns_flags & NSControlKeyMask) <> 0
  126. self.command = self.extend_noncontig = (ns_flags & NSCommandKeyMask) <> 0
  127. self.option = (ns_flags & NSAlternateKeyMask) <> 0
  128. if kind in _key_events:
  129. self.auto = ns_event.isARepeat()
  130. ns_chars = ns_event.characters()
  131. #print "Event.__init__: ns_chars =", repr(ns_chars) ###
  132. self.unichars = ns_chars
  133. if len(ns_chars) == 1:
  134. if ns_chars == "\x19" and ns_event.keyCode() == 48:
  135. self.char = "\t"
  136. elif ns_chars <= "\x7e":
  137. self.char = str(ns_chars)
  138. elif ns_chars == "\x7f":
  139. self.char = "\x08"
  140. key = _ns_keycode_to_keyname.get(ns_chars, "")
  141. self.key = key
  142. if key == 'enter':
  143. self.char = "\r"
  144. elif key == 'delete':
  145. self.char = "\x7f"
  146. def _platform_modifiers_str(self):
  147. return " command:%s option:%s" % (self.command, self.option)