m2m模型翻译
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

203 lines
7.0 KiB

6 months ago
  1. # -*- coding: utf-8 -*-
  2. #*****************************************************************************
  3. # Copyright (C) 2003-2006 Gary Bishop.
  4. # Copyright (C) 2006 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
  5. #
  6. # Distributed under the terms of the BSD License. The full license is in
  7. # the file COPYING, distributed as part of this software.
  8. #*****************************************************************************
  9. from __future__ import print_function, unicode_literals, absolute_import
  10. import System
  11. from .common import validkey, KeyPress, make_KeyPress_from_keydescr
  12. c32 = System.ConsoleKey
  13. Shift = System.ConsoleModifiers.Shift
  14. Control = System.ConsoleModifiers.Control
  15. Alt = System.ConsoleModifiers.Alt
  16. # table for translating virtual keys to X windows key symbols
  17. code2sym_map = {#c32.CANCEL: 'Cancel',
  18. c32.Backspace: 'BackSpace',
  19. c32.Tab: 'Tab',
  20. c32.Clear: 'Clear',
  21. c32.Enter: 'Return',
  22. # c32.Shift: 'Shift_L',
  23. # c32.Control: 'Control_L',
  24. # c32.Menu: 'Alt_L',
  25. c32.Pause: 'Pause',
  26. # c32.Capital: 'Caps_Lock',
  27. c32.Escape: 'Escape',
  28. # c32.Space: 'space',
  29. c32.PageUp: 'Prior',
  30. c32.PageDown: 'Next',
  31. c32.End: 'End',
  32. c32.Home: 'Home',
  33. c32.LeftArrow: 'Left',
  34. c32.UpArrow: 'Up',
  35. c32.RightArrow: 'Right',
  36. c32.DownArrow: 'Down',
  37. c32.Select: 'Select',
  38. c32.Print: 'Print',
  39. c32.Execute: 'Execute',
  40. # c32.Snapshot: 'Snapshot',
  41. c32.Insert: 'Insert',
  42. c32.Delete: 'Delete',
  43. c32.Help: 'Help',
  44. c32.F1: 'F1',
  45. c32.F2: 'F2',
  46. c32.F3: 'F3',
  47. c32.F4: 'F4',
  48. c32.F5: 'F5',
  49. c32.F6: 'F6',
  50. c32.F7: 'F7',
  51. c32.F8: 'F8',
  52. c32.F9: 'F9',
  53. c32.F10: 'F10',
  54. c32.F11: 'F11',
  55. c32.F12: 'F12',
  56. c32.F13: 'F13',
  57. c32.F14: 'F14',
  58. c32.F15: 'F15',
  59. c32.F16: 'F16',
  60. c32.F17: 'F17',
  61. c32.F18: 'F18',
  62. c32.F19: 'F19',
  63. c32.F20: 'F20',
  64. c32.F21: 'F21',
  65. c32.F22: 'F22',
  66. c32.F23: 'F23',
  67. c32.F24: 'F24',
  68. # c32.Numlock: 'Num_Lock,',
  69. # c32.Scroll: 'Scroll_Lock',
  70. # c32.Apps: 'VK_APPS',
  71. # c32.ProcesskeY: 'VK_PROCESSKEY',
  72. # c32.Attn: 'VK_ATTN',
  73. # c32.Crsel: 'VK_CRSEL',
  74. # c32.Exsel: 'VK_EXSEL',
  75. # c32.Ereof: 'VK_EREOF',
  76. # c32.Play: 'VK_PLAY',
  77. # c32.Zoom: 'VK_ZOOM',
  78. # c32.Noname: 'VK_NONAME',
  79. # c32.Pa1: 'VK_PA1',
  80. c32.OemClear: 'VK_OEM_CLEAR',
  81. c32.NumPad0: 'NUMPAD0',
  82. c32.NumPad1: 'NUMPAD1',
  83. c32.NumPad2: 'NUMPAD2',
  84. c32.NumPad3: 'NUMPAD3',
  85. c32.NumPad4: 'NUMPAD4',
  86. c32.NumPad5: 'NUMPAD5',
  87. c32.NumPad6: 'NUMPAD6',
  88. c32.NumPad7: 'NUMPAD7',
  89. c32.NumPad8: 'NUMPAD8',
  90. c32.NumPad9: 'NUMPAD9',
  91. c32.Divide: 'Divide',
  92. c32.Multiply: 'Multiply',
  93. c32.Add: 'Add',
  94. c32.Subtract: 'Subtract',
  95. c32.Decimal: 'VK_DECIMAL'
  96. }
  97. # function to handle the mapping
  98. def make_keysym(keycode):
  99. try:
  100. sym = code2sym_map[keycode]
  101. except KeyError:
  102. sym = ''
  103. return sym
  104. sym2code_map = {}
  105. for code,sym in code2sym_map.items():
  106. sym2code_map[sym.lower()] = code
  107. def key_text_to_keyinfo(keytext):
  108. '''Convert a GNU readline style textual description of a key to keycode with modifiers'''
  109. if keytext.startswith('"'): # "
  110. return keyseq_to_keyinfo(keytext[1:-1])
  111. else:
  112. return keyname_to_keyinfo(keytext)
  113. def char_to_keyinfo(char, control=False, meta=False, shift=False):
  114. vk = (ord(char))
  115. if vk & 0xffff == 0xffff:
  116. print('VkKeyScan("%s") = %x' % (char, vk))
  117. raise ValueError('bad key')
  118. if vk & 0x100:
  119. shift = True
  120. if vk & 0x200:
  121. control = True
  122. if vk & 0x400:
  123. meta = True
  124. return (control, meta, shift, vk & 0xff)
  125. def keyname_to_keyinfo(keyname):
  126. control = False
  127. meta = False
  128. shift = False
  129. while 1:
  130. lkeyname = keyname.lower()
  131. if lkeyname.startswith('control-'):
  132. control = True
  133. keyname = keyname[8:]
  134. elif lkeyname.startswith('ctrl-'):
  135. control = True
  136. keyname = keyname[5:]
  137. elif lkeyname.startswith('meta-'):
  138. meta = True
  139. keyname = keyname[5:]
  140. elif lkeyname.startswith('alt-'):
  141. meta = True
  142. keyname = keyname[4:]
  143. elif lkeyname.startswith('shift-'):
  144. shift = True
  145. keyname = keyname[6:]
  146. else:
  147. if len(keyname) > 1:
  148. return (control, meta, shift, sym2code_map.get(keyname.lower()," "))
  149. else:
  150. return char_to_keyinfo(keyname, control, meta, shift)
  151. def keyseq_to_keyinfo(keyseq):
  152. res = []
  153. control = False
  154. meta = False
  155. shift = False
  156. while 1:
  157. if keyseq.startswith('\\C-'):
  158. control = True
  159. keyseq = keyseq[3:]
  160. elif keyseq.startswith('\\M-'):
  161. meta = True
  162. keyseq = keyseq[3:]
  163. elif keyseq.startswith('\\e'):
  164. res.append(char_to_keyinfo('\033', control, meta, shift))
  165. control = meta = shift = False
  166. keyseq = keyseq[2:]
  167. elif len(keyseq) >= 1:
  168. res.append(char_to_keyinfo(keyseq[0], control, meta, shift))
  169. control = meta = shift = False
  170. keyseq = keyseq[1:]
  171. else:
  172. return res[0]
  173. def make_keyinfo(keycode, state):
  174. control = False
  175. meta =False
  176. shift = False
  177. return (control, meta, shift, keycode)
  178. def make_KeyPress(char, state, keycode):
  179. shift = bool(int(state) & int(Shift))
  180. control = bool(int(state) & int(Control))
  181. meta = bool(int(state) & int(Alt))
  182. keyname = code2sym_map.get(keycode, "").lower()
  183. if control and meta: #equivalent to altgr so clear flags
  184. control = False
  185. meta = False
  186. elif control:
  187. char = str(keycode)
  188. return KeyPress(char, shift, control, meta, keyname)