Skip to content

Commit

Permalink
add timeout for multipurpose
Browse files Browse the repository at this point in the history
use define_timeout(seconds) to set, default is 1
  • Loading branch information
nlfiasel committed Mar 7, 2020
1 parent 041e8f1 commit 795dbde
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 3 additions & 0 deletions example/config.py
Expand Up @@ -3,6 +3,9 @@
import re
from xkeysnail.transform import *

# define timeout for multipurpose_modmap
define_timeout(1)

# [Global modemap] Change modifier keys as in xmodmap
define_modmap({
Key.CAPSLOCK: Key.LEFT_CTRL
Expand Down
14 changes: 13 additions & 1 deletion xkeysnail/transform.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import itertools
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
Expand Down Expand Up @@ -260,6 +261,14 @@ def expand(target):
# or REPEAT
_last_key = None

# last key time record time when execute multi press
_last_key_time = time()
_timeout = 1
def define_timeout(seconds=1):
global _timeout
_timeout = seconds



def define_modmap(mod_remappings):
"""Defines modmap (keycode translation)
Expand Down Expand Up @@ -339,6 +348,7 @@ def maybe_press_modifiers(multipurpose_map):
# we need to register the last key presses so we know if a multipurpose key
# was a single press and release
global _last_key
global _last_key_time

if key in multipurpose_map:
single_key, mod_key, key_state = multipurpose_map[key]
Expand All @@ -349,13 +359,15 @@ def maybe_press_modifiers(multipurpose_map):
update_pressed_keys(key, action)
if action == Action.RELEASE and key_is_down:
# it is a single press and release
if key_was_last_press:
if key_was_last_press and _last_key_time + _timeout > time():
maybe_press_modifiers(multipurpose_map) # maybe other multipurpose keys are down
on_key(single_key, Action.PRESS)
on_key(single_key, Action.RELEASE)
# it is the modifier in a combo
elif mod_is_down:
on_key(mod_key, Action.RELEASE)
elif action == Action.PRESS and not key_is_down:
_last_key_time = time()
# if key is not a multipurpose or mod key we want eventual modifiers down
elif (key not in Modifier.get_all_keys()) and action == Action.PRESS:
maybe_press_modifiers(multipurpose_map)
Expand Down

0 comments on commit 795dbde

Please sign in to comment.