def _changeMode (self, key): """ Switches on or off the mode indicated by the given key. """ remap = self._remap oldModes = self._modes newModes = oldModes.copy() if key.value: if key.string in oldModes: # Mode already set, nothing to do return newModes.add(key.string) if not key.value: if key.string not in oldModes: # Nothing to do here, either return newModes.discard(key.string) # We need to go through all the currently pressed keys, and check if # any output needs to be changed for the new mode. for key in self._pressed.values(): oldMapping = remap([key], modes=oldModes) newMapping = remap([key], modes=newModes) if oldMapping == newMapping: # No change continue # Something's changed, so we need to stop the old output... if oldMapping is None: # We use key.string because key will probably have a value # which we don't want. oldMapping = mapping.ReMapping(key.string).output self._stopOutput(oldMapping) # ...and start the new if newMapping is None: newMapping = mapping.ReMapping(key.string).output self._startOutput(newMapping) # Now the chord of all keys oldMapping = remap(self._pressed.values(), modes=oldModes) newMapping = remap(self._pressed.values(), modes=newModes) if oldMapping != newMapping: if oldMapping: self._stopOutput(oldMapping) if newMapping: self._startOutput(newMapping) self._modes = newModes
def _stopEvent(self, key, modes=None): """ Stops, or rather stops repeating, events which rely on the given key being active. A list of modes (eg. const.LED_*s) can be supplied, otherwise the current mode-state of the device will be used. """ remap = self._remap if modes is None: modes = self._modes # ABS values aren't kept in _pressed, so make sure the key's in there. self._pressed[key.string] = key # Stop any chording relying on this key if len(self._pressed) > 1: withKey = remap(self._pressed.values(), modes=modes) del (self._pressed[key.string]) if withKey: withoutKey = remap(self._pressed.values(), modes=modes) if not withoutKey or withKey != withoutKey: # This key was a part of a chord; no longer self._stopOutput(withKey) else: del (self._pressed[key.string]) # Now we stop the key's own output output = remap([key], modes=modes) if output is None: output = mapping.ReMapping(key.string).output self._stopOutput(output)
def _stopEvent(self, key): """ Stops, or rather stops repeating, events which rely on the given key being active. """ remap = self._remap # ABS values aren't kept in _pressed, so make sure the key's in there. self._pressed[key.string] = key # Stop any chording relying on this key if len(self._pressed) > 1: withKey = remap(self._pressed.values()) del (self._pressed[key.string]) if withKey: withoutKey = remap(self._pressed.values()) if not withoutKey or withKey != withoutKey: # This key was a part of a chord; no longer self._stopOutput(withKey) else: del (self._pressed[key.string]) # Now we stop the key's own output output = remap([key]) if output is None: output = mapping.ReMapping(key).output self._stopOutput(output)
def _startEvent(self, key, modes=None): """ Starts whatever needs to be started with the additional key given. A list of modes (eg. const.LED_*s) can be supplied, otherwise the current mode-state of the device will be used. """ remap = self._remap if modes is None: modes = self._modes # Output from the single key output = remap([key], modes=modes) if output is None: output = mapping.ReMapping(key.string).output oldKey = None if key.string in self._pressed: oldKey = self._pressed[key.string] elif key.string in self._absolute: oldKey = self._absolute[key.string] if oldKey: # This key is/was already doing something oldOutput = remap([oldKey], modes=modes) if oldOutput and output != oldOutput: # The old output is over self._stopEvent(oldKey) if output and output != oldOutput: # Something new and different! self._startOutput(output) elif output: if output in self._queue: # We're still processing from this key's last press self._continueOutput(output) else: # This is completely new output self._startOutput(output) # And the chord of all pressed keys withPressed = self._pressed.copy() withPressed[key.string] = key if len(withPressed) > 1: if key.string in self._pressed: oldCombo = remap(self._pressed.values(), modes=modes) elif key.string in self._absolute: withAbs = self._pressed.values() withAbs.append(self._absolute[key.string]) oldCombo = remap(withAbs, modes=modes) else: oldCombo = None newCombo = remap(withPressed.values(), modes=modes) if oldCombo != newCombo: if oldCombo: self._stopOutput(oldCombo) if newCombo: self._startOutput(newCombo) # Store the state of this key if key.type == const.EV_ABS: self._absolute[key.string] = key else: # Absolute values don't get stored as 'pressed' self._pressed[key.string] = key
def _startEvent(self, key): """ Starts whatever needs to be started with the additional key given. """ remap = self._remap # Output from the single key output = remap([key]) if output is None: output = mapping.ReMapping(key).output oldKey = None if key.string in self._pressed: oldKey = self._pressed[key.string] elif key.string in self._absolute: oldKey = self._absolute[key.string] if oldKey: # This key is/was already doing something oldOutput = remap([oldKey]) if oldOutput and output != oldOutput: # The old output is over self._stopEvent(oldKey) if output and output != oldOutput: # Something new and different! self._startOutput(output) elif output: if output in self._queue: # We're still processing from this key's last press self._continueOutput(output) else: # This is completely new output self._startOutput(output) # And the chord of all pressed keys withPressed = self._pressed.copy() withPressed[key.string] = key if len(withPressed) > 1: if key.string in self._pressed: oldCombo = remap(self._pressed.values()) elif key.string in self._absolute: withAbs = self._pressed.values() withAbs.append(self._absolute[key.string]) oldCombo = remap(withAbs) else: oldCombo = None newCombo = remap(withPressed.values()) if oldCombo != newCombo: if oldCombo: self._stopOutput(oldCombo) if newCombo: self._startOutput(newCombo) # Store the state of this key if key.string.startswith('ABS_'): self._absolute[key.string] = key else: # Absolute values don't get stored as 'pressed' self._pressed[key.string] = key