config_key.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. """
  2. Dialog for building Tkinter accelerator key bindings
  3. """
  4. from tkinter import Toplevel, Listbox, StringVar, TclError
  5. from tkinter.ttk import Frame, Button, Checkbutton, Entry, Label, Scrollbar
  6. from tkinter import messagebox
  7. from tkinter.simpledialog import _setup_dialog
  8. import string
  9. import sys
  10. FUNCTION_KEYS = ('F1', 'F2' ,'F3' ,'F4' ,'F5' ,'F6',
  11. 'F7', 'F8' ,'F9' ,'F10' ,'F11' ,'F12')
  12. ALPHANUM_KEYS = tuple(string.ascii_lowercase + string.digits)
  13. PUNCTUATION_KEYS = tuple('~!@#%^&*()_-+={}[]|;:,.<>/?')
  14. WHITESPACE_KEYS = ('Tab', 'Space', 'Return')
  15. EDIT_KEYS = ('BackSpace', 'Delete', 'Insert')
  16. MOVE_KEYS = ('Home', 'End', 'Page Up', 'Page Down', 'Left Arrow',
  17. 'Right Arrow', 'Up Arrow', 'Down Arrow')
  18. AVAILABLE_KEYS = (ALPHANUM_KEYS + PUNCTUATION_KEYS + FUNCTION_KEYS +
  19. WHITESPACE_KEYS + EDIT_KEYS + MOVE_KEYS)
  20. def translate_key(key, modifiers):
  21. "Translate from keycap symbol to the Tkinter keysym."
  22. mapping = {'Space':'space',
  23. '~':'asciitilde', '!':'exclam', '@':'at', '#':'numbersign',
  24. '%':'percent', '^':'asciicircum', '&':'ampersand',
  25. '*':'asterisk', '(':'parenleft', ')':'parenright',
  26. '_':'underscore', '-':'minus', '+':'plus', '=':'equal',
  27. '{':'braceleft', '}':'braceright',
  28. '[':'bracketleft', ']':'bracketright', '|':'bar',
  29. ';':'semicolon', ':':'colon', ',':'comma', '.':'period',
  30. '<':'less', '>':'greater', '/':'slash', '?':'question',
  31. 'Page Up':'Prior', 'Page Down':'Next',
  32. 'Left Arrow':'Left', 'Right Arrow':'Right',
  33. 'Up Arrow':'Up', 'Down Arrow': 'Down', 'Tab':'Tab'}
  34. key = mapping.get(key, key)
  35. if 'Shift' in modifiers and key in string.ascii_lowercase:
  36. key = key.upper()
  37. return f'Key-{key}'
  38. class GetKeysFrame(Frame):
  39. # Dialog title for invalid key sequence
  40. keyerror_title = 'Key Sequence Error'
  41. def __init__(self, parent, action, current_key_sequences):
  42. """
  43. parent - parent of this dialog
  44. action - the name of the virtual event these keys will be
  45. mapped to
  46. current_key_sequences - a list of all key sequence lists
  47. currently mapped to virtual events, for overlap checking
  48. """
  49. super().__init__(parent)
  50. self['borderwidth'] = 2
  51. self['relief'] = 'sunken'
  52. self.parent = parent
  53. self.action = action
  54. self.current_key_sequences = current_key_sequences
  55. self.result = ''
  56. self.key_string = StringVar(self)
  57. self.key_string.set('')
  58. # Set self.modifiers, self.modifier_label.
  59. self.set_modifiers_for_platform()
  60. self.modifier_vars = []
  61. for modifier in self.modifiers:
  62. variable = StringVar(self)
  63. variable.set('')
  64. self.modifier_vars.append(variable)
  65. self.advanced = False
  66. self.create_widgets()
  67. def showerror(self, *args, **kwargs):
  68. # Make testing easier. Replace in #30751.
  69. messagebox.showerror(*args, **kwargs)
  70. def create_widgets(self):
  71. # Basic entry key sequence.
  72. self.frame_keyseq_basic = Frame(self, name='keyseq_basic')
  73. self.frame_keyseq_basic.grid(row=0, column=0, sticky='nsew',
  74. padx=5, pady=5)
  75. basic_title = Label(self.frame_keyseq_basic,
  76. text=f"New keys for '{self.action}' :")
  77. basic_title.pack(anchor='w')
  78. basic_keys = Label(self.frame_keyseq_basic, justify='left',
  79. textvariable=self.key_string, relief='groove',
  80. borderwidth=2)
  81. basic_keys.pack(ipadx=5, ipady=5, fill='x')
  82. # Basic entry controls.
  83. self.frame_controls_basic = Frame(self)
  84. self.frame_controls_basic.grid(row=1, column=0, sticky='nsew', padx=5)
  85. # Basic entry modifiers.
  86. self.modifier_checkbuttons = {}
  87. column = 0
  88. for modifier, variable in zip(self.modifiers, self.modifier_vars):
  89. label = self.modifier_label.get(modifier, modifier)
  90. check = Checkbutton(self.frame_controls_basic,
  91. command=self.build_key_string, text=label,
  92. variable=variable, onvalue=modifier, offvalue='')
  93. check.grid(row=0, column=column, padx=2, sticky='w')
  94. self.modifier_checkbuttons[modifier] = check
  95. column += 1
  96. # Basic entry help text.
  97. help_basic = Label(self.frame_controls_basic, justify='left',
  98. text="Select the desired modifier keys\n"+
  99. "above, and the final key from the\n"+
  100. "list on the right.\n\n" +
  101. "Use upper case Symbols when using\n" +
  102. "the Shift modifier. (Letters will be\n" +
  103. "converted automatically.)")
  104. help_basic.grid(row=1, column=0, columnspan=4, padx=2, sticky='w')
  105. # Basic entry key list.
  106. self.list_keys_final = Listbox(self.frame_controls_basic, width=15,
  107. height=10, selectmode='single')
  108. self.list_keys_final.insert('end', *AVAILABLE_KEYS)
  109. self.list_keys_final.bind('<ButtonRelease-1>', self.final_key_selected)
  110. self.list_keys_final.grid(row=0, column=4, rowspan=4, sticky='ns')
  111. scroll_keys_final = Scrollbar(self.frame_controls_basic,
  112. orient='vertical',
  113. command=self.list_keys_final.yview)
  114. self.list_keys_final.config(yscrollcommand=scroll_keys_final.set)
  115. scroll_keys_final.grid(row=0, column=5, rowspan=4, sticky='ns')
  116. self.button_clear = Button(self.frame_controls_basic,
  117. text='Clear Keys',
  118. command=self.clear_key_seq)
  119. self.button_clear.grid(row=2, column=0, columnspan=4)
  120. # Advanced entry key sequence.
  121. self.frame_keyseq_advanced = Frame(self, name='keyseq_advanced')
  122. self.frame_keyseq_advanced.grid(row=0, column=0, sticky='nsew',
  123. padx=5, pady=5)
  124. advanced_title = Label(self.frame_keyseq_advanced, justify='left',
  125. text=f"Enter new binding(s) for '{self.action}' :\n" +
  126. "(These bindings will not be checked for validity!)")
  127. advanced_title.pack(anchor='w')
  128. self.advanced_keys = Entry(self.frame_keyseq_advanced,
  129. textvariable=self.key_string)
  130. self.advanced_keys.pack(fill='x')
  131. # Advanced entry help text.
  132. self.frame_help_advanced = Frame(self)
  133. self.frame_help_advanced.grid(row=1, column=0, sticky='nsew', padx=5)
  134. help_advanced = Label(self.frame_help_advanced, justify='left',
  135. text="Key bindings are specified using Tkinter keysyms as\n"+
  136. "in these samples: <Control-f>, <Shift-F2>, <F12>,\n"
  137. "<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.\n"
  138. "Upper case is used when the Shift modifier is present!\n\n" +
  139. "'Emacs style' multi-keystroke bindings are specified as\n" +
  140. "follows: <Control-x><Control-y>, where the first key\n" +
  141. "is the 'do-nothing' keybinding.\n\n" +
  142. "Multiple separate bindings for one action should be\n"+
  143. "separated by a space, eg., <Alt-v> <Meta-v>." )
  144. help_advanced.grid(row=0, column=0, sticky='nsew')
  145. # Switch between basic and advanced.
  146. self.button_level = Button(self, command=self.toggle_level,
  147. text='<< Basic Key Binding Entry')
  148. self.button_level.grid(row=2, column=0, stick='ew', padx=5, pady=5)
  149. self.toggle_level()
  150. def set_modifiers_for_platform(self):
  151. """Determine list of names of key modifiers for this platform.
  152. The names are used to build Tk bindings -- it doesn't matter if the
  153. keyboard has these keys; it matters if Tk understands them. The
  154. order is also important: key binding equality depends on it, so
  155. config-keys.def must use the same ordering.
  156. """
  157. if sys.platform == "darwin":
  158. self.modifiers = ['Shift', 'Control', 'Option', 'Command']
  159. else:
  160. self.modifiers = ['Control', 'Alt', 'Shift']
  161. self.modifier_label = {'Control': 'Ctrl'} # Short name.
  162. def toggle_level(self):
  163. "Toggle between basic and advanced keys."
  164. if self.button_level.cget('text').startswith('Advanced'):
  165. self.clear_key_seq()
  166. self.button_level.config(text='<< Basic Key Binding Entry')
  167. self.frame_keyseq_advanced.lift()
  168. self.frame_help_advanced.lift()
  169. self.advanced_keys.focus_set()
  170. self.advanced = True
  171. else:
  172. self.clear_key_seq()
  173. self.button_level.config(text='Advanced Key Binding Entry >>')
  174. self.frame_keyseq_basic.lift()
  175. self.frame_controls_basic.lift()
  176. self.advanced = False
  177. def final_key_selected(self, event=None):
  178. "Handler for clicking on key in basic settings list."
  179. self.build_key_string()
  180. def build_key_string(self):
  181. "Create formatted string of modifiers plus the key."
  182. keylist = modifiers = self.get_modifiers()
  183. final_key = self.list_keys_final.get('anchor')
  184. if final_key:
  185. final_key = translate_key(final_key, modifiers)
  186. keylist.append(final_key)
  187. self.key_string.set(f"<{'-'.join(keylist)}>")
  188. def get_modifiers(self):
  189. "Return ordered list of modifiers that have been selected."
  190. mod_list = [variable.get() for variable in self.modifier_vars]
  191. return [mod for mod in mod_list if mod]
  192. def clear_key_seq(self):
  193. "Clear modifiers and keys selection."
  194. self.list_keys_final.select_clear(0, 'end')
  195. self.list_keys_final.yview('moveto', '0.0')
  196. for variable in self.modifier_vars:
  197. variable.set('')
  198. self.key_string.set('')
  199. def ok(self):
  200. self.result = ''
  201. keys = self.key_string.get().strip()
  202. if not keys:
  203. self.showerror(title=self.keyerror_title, parent=self,
  204. message="No key specified.")
  205. return
  206. if (self.advanced or self.keys_ok(keys)) and self.bind_ok(keys):
  207. self.result = keys
  208. return
  209. def keys_ok(self, keys):
  210. """Validity check on user's 'basic' keybinding selection.
  211. Doesn't check the string produced by the advanced dialog because
  212. 'modifiers' isn't set.
  213. """
  214. final_key = self.list_keys_final.get('anchor')
  215. modifiers = self.get_modifiers()
  216. title = self.keyerror_title
  217. key_sequences = [key for keylist in self.current_key_sequences
  218. for key in keylist]
  219. if not keys.endswith('>'):
  220. self.showerror(title, parent=self,
  221. message='Missing the final Key')
  222. elif (not modifiers
  223. and final_key not in FUNCTION_KEYS + MOVE_KEYS):
  224. self.showerror(title=title, parent=self,
  225. message='No modifier key(s) specified.')
  226. elif (modifiers == ['Shift']) \
  227. and (final_key not in
  228. FUNCTION_KEYS + MOVE_KEYS + ('Tab', 'Space')):
  229. msg = 'The shift modifier by itself may not be used with'\
  230. ' this key symbol.'
  231. self.showerror(title=title, parent=self, message=msg)
  232. elif keys in key_sequences:
  233. msg = 'This key combination is already in use.'
  234. self.showerror(title=title, parent=self, message=msg)
  235. else:
  236. return True
  237. return False
  238. def bind_ok(self, keys):
  239. "Return True if Tcl accepts the new keys else show message."
  240. try:
  241. binding = self.bind(keys, lambda: None)
  242. except TclError as err:
  243. self.showerror(
  244. title=self.keyerror_title, parent=self,
  245. message=(f'The entered key sequence is not accepted.\n\n'
  246. f'Error: {err}'))
  247. return False
  248. else:
  249. self.unbind(keys, binding)
  250. return True
  251. class GetKeysWindow(Toplevel):
  252. def __init__(self, parent, title, action, current_key_sequences,
  253. *, _htest=False, _utest=False):
  254. """
  255. parent - parent of this dialog
  256. title - string which is the title of the popup dialog
  257. action - string, the name of the virtual event these keys will be
  258. mapped to
  259. current_key_sequences - list, a list of all key sequence lists
  260. currently mapped to virtual events, for overlap checking
  261. _htest - bool, change box location when running htest
  262. _utest - bool, do not wait when running unittest
  263. """
  264. super().__init__(parent)
  265. self.withdraw() # Hide while setting geometry.
  266. self['borderwidth'] = 5
  267. self.resizable(height=False, width=False)
  268. # Needed for winfo_reqwidth().
  269. self.update_idletasks()
  270. # Center dialog over parent (or below htest box).
  271. x = (parent.winfo_rootx() +
  272. (parent.winfo_width()//2 - self.winfo_reqwidth()//2))
  273. y = (parent.winfo_rooty() +
  274. ((parent.winfo_height()//2 - self.winfo_reqheight()//2)
  275. if not _htest else 150))
  276. self.geometry(f"+{x}+{y}")
  277. self.title(title)
  278. self.frame = frame = GetKeysFrame(self, action, current_key_sequences)
  279. self.protocol("WM_DELETE_WINDOW", self.cancel)
  280. frame_buttons = Frame(self)
  281. self.button_ok = Button(frame_buttons, text='OK',
  282. width=8, command=self.ok)
  283. self.button_cancel = Button(frame_buttons, text='Cancel',
  284. width=8, command=self.cancel)
  285. self.button_ok.grid(row=0, column=0, padx=5, pady=5)
  286. self.button_cancel.grid(row=0, column=1, padx=5, pady=5)
  287. frame.pack(side='top', expand=True, fill='both')
  288. frame_buttons.pack(side='bottom', fill='x')
  289. self.transient(parent)
  290. _setup_dialog(self)
  291. self.grab_set()
  292. if not _utest:
  293. self.deiconify() # Geometry set, unhide.
  294. self.wait_window()
  295. @property
  296. def result(self):
  297. return self.frame.result
  298. @result.setter
  299. def result(self, value):
  300. self.frame.result = value
  301. def ok(self, event=None):
  302. self.frame.ok()
  303. self.grab_release()
  304. self.destroy()
  305. def cancel(self, event=None):
  306. self.result = ''
  307. self.grab_release()
  308. self.destroy()
  309. if __name__ == '__main__':
  310. from unittest import main
  311. main('idlelib.idle_test.test_config_key', verbosity=2, exit=False)
  312. from idlelib.idle_test.htest import run
  313. run(GetKeysDialog)