def configure_keys(self):
        """
        This function locates the keycodes corresponding to special groups of
        keys and creates data structures of them for use by the PyKeyboardEvent
        instance; including the keypad keys and the modifiers.

        The keycodes pertaining to the keyboard modifiers are assigned by the
        modifier name in a dictionary. This dictionary can be accessed in the
        following manner:
            self.modifier_keycodes['Shift']  # All keycodes for Shift Masking

        It also assigns certain named modifiers (Alt, Num_Lock, Super), which
        may be dynamically assigned to Mod1 - Mod5 on different platforms. This
        should generally allow the user to do the following lookups on any
        system:
            self.modifier_keycodes['Alt']  # All keycodes for Alt Masking
            self.modifiers['Alt']  # State of Alt mask, non-zero if "ON"
        """
        modifier_mapping = self.display.get_modifier_mapping()
        all_mod_keycodes = []
        mod_keycodes = {}
        mod_index = [('Shift', X.ShiftMapIndex), ('Lock', X.LockMapIndex),
                     ('Control', X.ControlMapIndex), ('Mod1', X.Mod1MapIndex),
                     ('Mod2', X.Mod2MapIndex), ('Mod3', X.Mod3MapIndex),
                     ('Mod4', X.Mod4MapIndex), ('Mod5', X.Mod5MapIndex)]
        #This gets the list of all keycodes per Modifier, assigns to name
        for name, index in mod_index:
            codes = [v for v in list(modifier_mapping[index]) if v]
            mod_keycodes[name] = codes
            all_mod_keycodes += codes

        def lookup_keycode(string):
            keysym = self.string_to_keysym[string]
            return self.display.keysym_to_keycode(keysym)

        #Dynamically assign Lock to Caps_Lock, Shift_Lock, Alt, Num_Lock, Super,
        #and mode switch. Set in both mod_keycodes and self.modifier_bits

        #Try to assign Lock to Caps_Lock or Shift_Lock
        shift_lock_keycode = lookup_keycode('Shift_Lock')
        caps_lock_keycode = lookup_keycode('Caps_Lock')

        if shift_lock_keycode in mod_keycodes['Lock']:
            mod_keycodes['Shift_Lock'] = [shift_lock_keycode]
            self.modifier_bits['Shift_Lock'] = self.modifier_bits['Lock']
            self.lock_meaning = 'Shift_Lock'
        elif caps_lock_keycode in mod_keycodes['Lock']:
            mod_keycodes['Caps_Lock'] = [caps_lock_keycode]
            self.modifier_bits['Caps_Lock'] = self.modifier_bits['Lock']
            self.lock_meaning = 'Caps_Lock'
        else:
            self.lock_meaning = None
        #print('Lock is bound to {0}'.format(self.lock_meaning))

        #Need to find out which Mod# to use for Alt, Num_Lock, Super, and
        #Mode_switch
        num_lock_keycodes = [lookup_keycode('Num_Lock')]
        alt_keycodes = [lookup_keycode(i) for i in ['Alt_L', 'Alt_R']]
        super_keycodes = [lookup_keycode(i) for i in ['Super_L', 'Super_R']]
        mode_switch_keycodes = [lookup_keycode('Mode_switch')]

        #Detect Mod number for Alt, Num_Lock, and Super
        for name, keycodes in list(mod_keycodes.items()):
            for alt_key in alt_keycodes:
                if alt_key in keycodes:
                    mod_keycodes['Alt'] = keycodes
                    self.modifier_bits['Alt'] = self.modifier_bits[name]
            for num_lock_key in num_lock_keycodes:
                if num_lock_key in keycodes:
                    mod_keycodes['Num_Lock'] = keycodes
                    self.modifier_bits['Num_Lock'] = self.modifier_bits[name]
            for super_key in super_keycodes:
                if super_key in keycodes:
                    mod_keycodes['Super'] = keycodes
                    self.modifier_bits['Super'] = self.modifier_bits[name]
            for mode_switch_key in mode_switch_keycodes:
                if mode_switch_key in keycodes:
                    mod_keycodes['Mode_switch'] = keycodes
                    self.modifier_bits['Mode_switch'] = self.modifier_bits[name]

        #Assign the mod_keycodes to a local variable for access
        self.modifier_keycodes = mod_keycodes
        self.all_mod_keycodes = all_mod_keycodes

        #TODO: Determine if this might fail, perhaps iterate through the mapping
        #and identify all keycodes with registered keypad keysyms?

        #Acquire the full list of keypad keycodes
        self.keypad_keycodes = []
        keypad = ['Space', 'Tab', 'Enter', 'F1', 'F2', 'F3', 'F4', 'Home',
                  'Left', 'Up', 'Right', 'Down', 'Prior', 'Page_Up', 'Next',
                  'Page_Down', 'End', 'Begin', 'Insert', 'Delete', 'Equal',
                  'Multiply', 'Add', 'Separator', 'Subtract', 'Decimal',
                  'Divide', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        for keyname in keypad:
            keypad_keycode = self.lookup_character_keycode('KP_' + keyname)
            self.keypad_keycodes.append(keypad_keycode)
def configure_keys ( self ) : modifier_mapping = self . display . get_modifier_mapping ( ) all_mod_keycodes = [ ] mod_keycodes = { } mod_index = [ ( 'Shift' , X . ShiftMapIndex ) , ( 'Lock' , X . LockMapIndex ) , ( 'Control' , X . ControlMapIndex ) , ( 'Mod1' , X . Mod1MapIndex ) , ( 'Mod2' , X . Mod2MapIndex ) , ( 'Mod3' , X . Mod3MapIndex ) , ( 'Mod4' , X . Mod4MapIndex ) , ( 'Mod5' , X . Mod5MapIndex ) ] for name , index in mod_index : codes = [ v for v in list ( modifier_mapping [ index ] ) if v ] mod_keycodes [ name ] = codes all_mod_keycodes += codes def lookup_keycode ( string ) : keysym = self . string_to_keysym [ string ] return self . display . keysym_to_keycode ( keysym ) shift_lock_keycode = lookup_keycode ( 'Shift_Lock' ) caps_lock_keycode = lookup_keycode ( 'Caps_Lock' ) if shift_lock_keycode in mod_keycodes [ 'Lock' ] : mod_keycodes [ 'Shift_Lock' ] = [ shift_lock_keycode ] self . modifier_bits [ 'Shift_Lock' ] = self . modifier_bits [ 'Lock' ] self . lock_meaning = 'Shift_Lock' elif caps_lock_keycode in mod_keycodes [ 'Lock' ] : mod_keycodes [ 'Caps_Lock' ] = [ caps_lock_keycode ] self . modifier_bits [ 'Caps_Lock' ] = self . modifier_bits [ 'Lock' ] self . lock_meaning = 'Caps_Lock' else : self . lock_meaning = None num_lock_keycodes = [ lookup_keycode ( 'Num_Lock' ) ] alt_keycodes = [ lookup_keycode ( i ) for i in [ 'Alt_L' , 'Alt_R' ] ] super_keycodes = [ lookup_keycode ( i ) for i in [ 'Super_L' , 'Super_R' ] ] mode_switch_keycodes = [ lookup_keycode ( 'Mode_switch' ) ] for name , keycodes in list ( mod_keycodes . items ( ) ) : for alt_key in alt_keycodes : if alt_key in keycodes : mod_keycodes [ 'Alt' ] = keycodes self . modifier_bits [ 'Alt' ] = self . modifier_bits [ name ] for num_lock_key in num_lock_keycodes : if num_lock_key in keycodes : mod_keycodes [ 'Num_Lock' ] = keycodes self . modifier_bits [ 'Num_Lock' ] = self . modifier_bits [ name ] for super_key in super_keycodes : if super_key in keycodes : mod_keycodes [ 'Super' ] = keycodes self . modifier_bits [ 'Super' ] = self . modifier_bits [ name ] for mode_switch_key in mode_switch_keycodes : if mode_switch_key in keycodes : mod_keycodes [ 'Mode_switch' ] = keycodes self . modifier_bits [ 'Mode_switch' ] = self . modifier_bits [ name ] self . modifier_keycodes = mod_keycodes self . all_mod_keycodes = all_mod_keycodes self . keypad_keycodes = [ ] keypad = [ 'Space' , 'Tab' , 'Enter' , 'F1' , 'F2' , 'F3' , 'F4' , 'Home' , 'Left' , 'Up' , 'Right' , 'Down' , 'Prior' , 'Page_Up' , 'Next' , 'Page_Down' , 'End' , 'Begin' , 'Insert' , 'Delete' , 'Equal' , 'Multiply' , 'Add' , 'Separator' , 'Subtract' , 'Decimal' , 'Divide' , '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ] for keyname in keypad : keypad_keycode = self . lookup_character_keycode ( 'KP_' + keyname ) self . keypad_keycodes . append ( keypad_keycode )
This function locates the keycodes corresponding to special groups of
        keys and creates data structures of them for use by the PyKeyboardEvent
        instance; including the keypad keys and the modifiers.

        The keycodes pertaining to the keyboard modifiers are assigned by the
        modifier name in a dictionary. This dictionary can be accessed in the
        following manner:
            self.modifier_keycodes['Shift']  # All keycodes for Shift Masking

        It also assigns certain named modifiers (Alt, Num_Lock, Super), which
        may be dynamically assigned to Mod1 - Mod5 on different platforms. This
        should generally allow the user to do the following lookups on any
        system:
            self.modifier_keycodes['Alt']  # All keycodes for Alt Masking
            self.modifiers['Alt']  # State of Alt mask, non-zero if "ON"