keysyms.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # -*- coding: utf-8 -*-
  2. # *****************************************************************************
  3. # Copyright (C) 2003-2006 Gary Bishop.
  4. # Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
  5. # Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
  6. #
  7. # Distributed under the terms of the BSD License. The full license is in
  8. # the file COPYING, distributed as part of this software.
  9. # *****************************************************************************
  10. from ctypes import windll
  11. from . import winconstants as c32
  12. from .common import KeyPress
  13. # table for translating virtual keys to X windows key symbols
  14. code2sym_map = {
  15. c32.VK_CANCEL: "cancel",
  16. c32.VK_BACK: "backspace",
  17. c32.VK_TAB: "tab",
  18. c32.VK_CLEAR: "clear",
  19. c32.VK_RETURN: "return",
  20. c32.VK_SHIFT: "shift_l",
  21. c32.VK_CONTROL: "control_l",
  22. c32.VK_MENU: "alt_l",
  23. c32.VK_PAUSE: "pause",
  24. c32.VK_CAPITAL: "caps_lock",
  25. c32.VK_ESCAPE: "escape",
  26. c32.VK_SPACE: "space",
  27. c32.VK_PRIOR: "prior",
  28. c32.VK_NEXT: "next",
  29. c32.VK_END: "end",
  30. c32.VK_HOME: "home",
  31. c32.VK_LEFT: "left",
  32. c32.VK_UP: "up",
  33. c32.VK_RIGHT: "right",
  34. c32.VK_DOWN: "down",
  35. c32.VK_SELECT: "select",
  36. c32.VK_PRINT: "print",
  37. c32.VK_EXECUTE: "execute",
  38. c32.VK_SNAPSHOT: "snapshot",
  39. c32.VK_INSERT: "insert",
  40. c32.VK_DELETE: "delete",
  41. c32.VK_HELP: "help",
  42. c32.VK_F1: "f1",
  43. c32.VK_F2: "f2",
  44. c32.VK_F3: "f3",
  45. c32.VK_F4: "f4",
  46. c32.VK_F5: "f5",
  47. c32.VK_F6: "f6",
  48. c32.VK_F7: "f7",
  49. c32.VK_F8: "f8",
  50. c32.VK_F9: "f9",
  51. c32.VK_F10: "f10",
  52. c32.VK_F11: "f11",
  53. c32.VK_F12: "f12",
  54. c32.VK_F13: "f13",
  55. c32.VK_F14: "f14",
  56. c32.VK_F15: "f15",
  57. c32.VK_F16: "f16",
  58. c32.VK_F17: "f17",
  59. c32.VK_F18: "f18",
  60. c32.VK_F19: "f19",
  61. c32.VK_F20: "f20",
  62. c32.VK_F21: "f21",
  63. c32.VK_F22: "f22",
  64. c32.VK_F23: "f23",
  65. c32.VK_F24: "f24",
  66. c32.VK_NUMLOCK: "num_lock,",
  67. c32.VK_SCROLL: "scroll_lock",
  68. c32.VK_APPS: "vk_apps",
  69. c32.VK_PROCESSKEY: "vk_processkey",
  70. c32.VK_ATTN: "vk_attn",
  71. c32.VK_CRSEL: "vk_crsel",
  72. c32.VK_EXSEL: "vk_exsel",
  73. c32.VK_EREOF: "vk_ereof",
  74. c32.VK_PLAY: "vk_play",
  75. c32.VK_ZOOM: "vk_zoom",
  76. c32.VK_NONAME: "vk_noname",
  77. c32.VK_PA1: "vk_pa1",
  78. c32.VK_OEM_CLEAR: "vk_oem_clear",
  79. c32.VK_NUMPAD0: "numpad0",
  80. c32.VK_NUMPAD1: "numpad1",
  81. c32.VK_NUMPAD2: "numpad2",
  82. c32.VK_NUMPAD3: "numpad3",
  83. c32.VK_NUMPAD4: "numpad4",
  84. c32.VK_NUMPAD5: "numpad5",
  85. c32.VK_NUMPAD6: "numpad6",
  86. c32.VK_NUMPAD7: "numpad7",
  87. c32.VK_NUMPAD8: "numpad8",
  88. c32.VK_NUMPAD9: "numpad9",
  89. c32.VK_DIVIDE: "divide",
  90. c32.VK_MULTIPLY: "multiply",
  91. c32.VK_ADD: "add",
  92. c32.VK_SUBTRACT: "subtract",
  93. c32.VK_DECIMAL: "vk_decimal",
  94. }
  95. VkKeyScan = windll.user32.VkKeyScanA
  96. def char_to_keyinfo(char, control=False, meta=False, shift=False):
  97. k = KeyPress()
  98. vk = VkKeyScan(ord(char))
  99. if vk & 0xFFFF == 0xFFFF:
  100. print('VkKeyScan("%s") = %x' % (char, vk))
  101. raise ValueError("bad key")
  102. if vk & 0x100:
  103. k.shift = True
  104. if vk & 0x200:
  105. k.control = True
  106. if vk & 0x400:
  107. k.meta = True
  108. k.char = chr(vk & 0xFF)
  109. return k
  110. def make_KeyPress(char, state, keycode):
  111. control = (state & (4 + 8)) != 0
  112. meta = (state & (1 + 2)) != 0
  113. shift = (state & 0x10) != 0
  114. if control and not meta: # Matches ctrl- chords should pass keycode as char
  115. char = chr(keycode)
  116. elif control and meta: # Matches alt gr and should just pass on char
  117. control = False
  118. meta = False
  119. try:
  120. keyname = code2sym_map[keycode]
  121. except KeyError:
  122. keyname = ""
  123. out = KeyPress(char, shift, control, meta, keyname)
  124. return out
  125. if __name__ == "__main__":
  126. import startup