def on_key_press(self, event): # pprint(event.__dict__) event = self._parse_tkevent(event) try: print(self.captured) event_clock(event, timevar=self.time, timemax=self.time_out, entries=self.captured, maxentries=self.max_keys) except (MaxInput, TimedOut) as err: # Reset condition Reached self.var.set('') self.time = [] self.original_keys = [event.keysym] event = self._custom_input(event) self.captured = [event.keysym] msg = 'Field reset - {0}'.format( err) #move this to the subclassed function self.max_timed_err_hook(msg) else: self.original_keys.append(event.keysym) event = self._custom_input(event) self.captured.append(event.keysym) # if the user holds down a key, prevents it from spammin in multiple times with ignored(IndexError): if self.captured[-1] == self.captured[-2]: del self.captured[-1] self.duplicate_err_hook(event) return 'break' x = self.format_start + ' %s %s ' % ( self.captured[-1], self.format_end ) #the last added char is formated self.var.set(self.var.get() + x) return 'break'
def validate(self, hk_grabber, hotkey, previous_hotkey): # This part does the automatic popping and unpopping behavior, # makes use of the indent_or unindent function hotkey = HotKeyGrabber.uniform_key(hotkey) previous_hotkey = HotKeyGrabber.uniform_key(previous_hotkey) try: if not self.indent_or_unindent(hotkey, hk_grabber.form_name, previous_hotkey): #~ print('Failure') hk_grabber.config(state='disabled') if not self.automatic_disable: return with ignored(AttributeError): exec('hk_grabber.' + self.no_disabled_entries_attrib + ' = False') if len(self.DISABLED[hotkey] ) == 2: # Disabling the original key instance original_index = self.DISABLED[hotkey].index( eval('hk_grabber.' + self.grabber_name_attrib)) other_index = not original_index setting = self.DISABLED[hotkey][other_index] widget = self.get_grabber_from_name(setting) if widget: widget.config(state='disabled') else: #~ print('success') hk_grabber.config(state='normal') if not self.automatic_enable: return with ignored(KeyError ): # Unpoping the last blocked key if its alone if len(self.DISABLED[previous_hotkey]) == 1: setting = self.DISABLED[previous_hotkey] widget = self.get_grabber_from_name(setting[0]) if widget: widget.config(state='normal') self.NORMAL[previous_hotkey] == setting if not self.get_disabled_states(): with ignored(AttributeError): exec('hk_grabber.' + self.no_disabled_entries_attrib + ' = True') except SystemError: # used to break out of the indent_or_unindent function return
def indent_or_unindent(self, hotkey, setting, previous_hotkey): '''Retursn True or False if the button should be indented or unindented''' def hotkey_disabled(): try: disabled_settings = self.DISABLED[hotkey] if disabled_settings != []: #~ print(9) return True #~ print(11) return False except KeyError: #~ print(10) return False with ignored(KeyError): del self.NORMAL[ previous_hotkey] # Previous hotkey has been changed so remove it from list with ignored( KeyError, ValueError): # Freeup the previous hotkey if it was disabled index = self.DISABLED[previous_hotkey].index(setting) del self.DISABLED[previous_hotkey][index] print('HOTKEY', hotkey, not hotkey) if not self.disable_blanks and not hotkey: raise SystemError value_assigned = self.NORMAL.get(hotkey) if not value_assigned and not hotkey_disabled(): #~ print(1) self.NORMAL[hotkey] = setting return True elif value_assigned: # Handling a conflict #~ print(3) del self.NORMAL[hotkey] self.DISABLED[hotkey] = [value_assigned, setting] return False else: #~ print(4) disabled_settings = self.DISABLED[hotkey] self.DISABLED[hotkey].append(setting) return False
def reset(self, event=None): self.var.set('') self.time = [] self.captured = [] self.original_keys = [] with ignored(AttributeError): del self._MOD_BEFORE_KEY del self._MOD_AFTER_KEY del self._KEYS_BEFORE del self._KEYS_AFTER # Undisable on reset (user doesn't have to wait until clicking out) if self.validate_multiple_grabbers: self.config(state='normal')
def __init__(self, parent=None, app=None): self.time = [] # holds time between key presses self.captured = [] # holds results self.original_keys = [] # the original tkinter key_sym self.tk_modifiers = tk_modifiers self.format_start, self.format_end = ('<', '>') self.text = 'Click to enter a hot key..' self.conf = {'width': 25} self.reset_on_click = True self.reset_on_focus = True self.time_out = 3000 self.max_keys = 4 self.max_mod_after_key = 0 self.max_mod_before_key = 3 self.max_key_after_mod = 1 self.max_key_before_mod = 1 self.reset_on_validate_fail = 1 self.validate_multiple_grabbers = False self.capture_mouse = [] self.previous_entry = [ ] # Previous hk entry before the user focused into the widget self.default_case = None #~ tk.Frame.__init__(self, parent, relief='flat',borderwidth=0) #~ self.pack(expand=1, fill='both') self.app = app self.parent = parent self.start() self.make_widgets() with ignored(AttributeError): #~ self.ent.config(**self.conf) self.config(**self.conf) if self.validate_multiple_grabbers: self.bind( '<FocusOut>', lambda event: self.validate_multiple_grabbers.validate( self, self.get(), self.previous_entry), '+') self.bind( '<FocusIn>', lambda event: self.validate_multiple_grabbers.validate( self, self.get(), self.previous_entry), '+') self.finish()
def _custom_input(self, event): with ignored(KeyError, AttributeError): new_keysym = self.custom_input[event.keysym] event.keysym = new_keysym return event