Skip to content

Commit

Permalink
Merge pull request #71 from rbreaves/master
Browse files Browse the repository at this point in the history
Release mapped modifiers after the original is released. Closes #70.
  • Loading branch information
mooz authored Apr 25, 2020
2 parents 123432b + 5cd22ed commit e3dcedd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 14 additions & 5 deletions xkeysnail/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
_pressed_modifier_keys = set()
_pressed_keys = set()

def output_modifier_key():
return _pressed_modifier_keys

def update_modifier_key_pressed(key, action):
if key in Modifier.get_all_keys():
if action.is_pressed():
Expand Down Expand Up @@ -57,8 +60,17 @@ def send_combo(combo):
missing_modifiers.remove(modifier)

for modifier_key in extra_modifier_keys:
send_key_action(modifier_key, Action.RELEASE)
released_modifiers_keys.append(modifier_key)
# Do not release new modifier
# until original modifier is released
# unless no modifier is the new mapping
if len(combo.modifiers) > 0:
for modifier in combo.modifiers:
if modifier_key != str(modifier.get_key()):
send_key_action(modifier_key, Action.RELEASE)
released_modifiers_keys.append(modifier_key)
else:
send_key_action(modifier_key, Action.RELEASE)
released_modifiers_keys.append(modifier_key)

pressed_modifier_keys = []
for modifier in missing_modifiers:
Expand All @@ -70,9 +82,6 @@ def send_combo(combo):

send_key_action(combo.key, Action.RELEASE)

for modifier in reversed(pressed_modifier_keys):
send_key_action(modifier, Action.RELEASE)

for modifier in reversed(released_modifiers_keys):
send_key_action(modifier, Action.PRESS)

Expand Down
20 changes: 19 additions & 1 deletion xkeysnail/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
from time import time
from inspect import signature
from .key import Action, Combo, Key, Modifier
from .output import send_combo, send_key_action, send_key, is_pressed
from .output import send_combo, send_key_action, send_key, is_pressed, output_modifier_key

__author__ = 'zh'

# ============================================================ #

import Xlib.display

_release_combo = False

def get_active_window_wm_class(display=Xlib.display.Display()):
"""Get active window's WM_CLASS"""
Expand Down Expand Up @@ -417,14 +418,31 @@ def on_event(event, device_name, quiet):


def on_key(key, action, wm_class=None, quiet=False):
global _release_combo
output_mods = output_modifier_key().copy()
if key in Modifier.get_all_keys():
update_pressed_modifier_keys(key, action)
send_key_action(key, action)
# Release mapped modifier only when physical mod
# is released
if str(key) != "Key.LEFT_SHIFT" and str(key) != "Key.RIGHT_SHIFT":
for output_key in output_mods:
update_pressed_modifier_keys(output_key, action)
send_key_action(output_key, action)
elif not action.is_pressed():
if is_pressed(key):
send_key_action(key, action)
# Unset modifiers used in nested mode_maps
elif _release_combo and len(output_mods) > 0:
_release_combo = False
for output_key in output_mods:
update_pressed_modifier_keys(output_key, action)
send_key_action(output_key, action)
else:
transform_key(key, action, wm_class=wm_class, quiet=quiet)
# Will unset mode maps modifiers on next combo
if _mode_maps != None:
_release_combo = True


def transform_key(key, action, wm_class=None, quiet=False):
Expand Down

0 comments on commit e3dcedd

Please sign in to comment.