Skip to content

Commit

Permalink
Merge pull request #68 from alex5105/ignore_repeat
Browse files Browse the repository at this point in the history
Add throttle time change slider on setting page and ignore repeat
  • Loading branch information
willwade authored Oct 2, 2024
2 parents 57bffe0 + 1ce638a commit bd60b87
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
7 changes: 7 additions & 0 deletions src/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def __init__(self):
self.unsave_configs = False
self.unsave_mouse_bindings = False
self.unsave_keyboard_bindings = False
self.throttle_time = 1.5
self.config = None

# Load config
Expand All @@ -61,6 +62,12 @@ def currentProfilePath(self):
App().profilesDirectory, CURRENT_PROFILE_FILENAME)
return self._currentProfilePath

def set_throttle_time(self, time):
self.throttle_time = time

def get_throttle_time(self):
return self.throttle_time

def _get_profiles_directory(self, *name):
path = App().profilesDirectory
if path.exists():
Expand Down
26 changes: 24 additions & 2 deletions src/controllers/keybinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Keybinder(metaclass=Singleton):
def __init__(self) -> None:
self.delay_count = None
self.key_states = None
self.last_act_time = None
self.schedule_toggle_off = {}
self.schedule_toggle_on = {}
self.monitors = None
Expand Down Expand Up @@ -55,11 +56,13 @@ def init_states(self) -> None:
"""
# keep states for all registered keys.
self.key_states = {}
self.last_act_time = {}
self.start_hold_ts = {}
for _, v in (ConfigManager().mouse_bindings |
ConfigManager().keyboard_bindings).items():
state_name = v[0]+"_"+v[1]
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)
self.schedule_toggle_off[state_name] = False
self.schedule_toggle_on[state_name] = True
self.start_hold_ts[state_name] = math.inf
Expand Down Expand Up @@ -98,6 +101,8 @@ def get_current_monitor(self) -> int:
def meta_action(self, val, action, threshold, is_active: bool) -> None:
state_name = "meta_" + action

if self.last_act_time[state_name] > int(time.time() * 1000) - ConfigManager().get_throttle_time() * 1000:
return
if action == "pause":

if (val > threshold) and (self.key_states[state_name] is False):
Expand All @@ -110,6 +115,7 @@ def meta_action(self, val, action, threshold, is_active: bool) -> None:
self.key_states[state_name] = True
elif (val < threshold) and (self.key_states[state_name] is True):
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)

if is_active:

Expand All @@ -127,6 +133,7 @@ def meta_action(self, val, action, threshold, is_active: bool) -> None:
elif (val < threshold) and (self.key_states[state_name] is
True):
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)

elif action == "cycle":
if (val > threshold) and (self.key_states[state_name] is
Expand All @@ -140,15 +147,19 @@ def meta_action(self, val, action, threshold, is_active: bool) -> None:
elif (val < threshold) and (self.key_states[state_name] is
True):
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)

def mouse_action(self, val, action, threshold, mode) -> None:
state_name = "mouse_" + action

if self.last_act_time[state_name] > int(time.time() * 1000) - ConfigManager().get_throttle_time() * 1000:
return
if mode == Trigger.SINGLE:
if val > threshold:
if self.key_states[state_name] is False:
pydirectinput.click(button=action)
self.key_states[state_name] = True
self.last_act_time[state_name] = int(time.time() * 1000)
if val < threshold:
self.key_states[state_name] = False

Expand All @@ -160,6 +171,7 @@ def mouse_action(self, val, action, threshold, mode) -> None:
elif (val < threshold) and (self.key_states[state_name] is True):
pydirectinput.mouseUp(button=action)
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)

elif mode == Trigger.DYNAMIC:
if val > threshold:
Expand All @@ -177,11 +189,12 @@ def mouse_action(self, val, action, threshold, mode) -> None:
elif (val < threshold) and (self.key_states[state_name] is True):

self.key_states[state_name] = False

self.last_act_time[state_name] = int(time.time() * 1000)
if self.holding[state_name]:
pydirectinput.mouseUp(button=action)
self.holding[state_name] = False
self.start_hold_ts[state_name] = math.inf
self.last_act_time[state_name] = int(time.time() * 1000)

elif mode == Trigger.TOGGLE:
if val > threshold:
Expand All @@ -194,6 +207,7 @@ def mouse_action(self, val, action, threshold, mode) -> None:
if self.schedule_toggle_off[state_name] is True:
pydirectinput.mouseUp(button=action)
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)

if val < threshold:
if self.key_states[state_name] is True:
Expand Down Expand Up @@ -221,16 +235,20 @@ def mouse_action(self, val, action, threshold, mode) -> None:
if self.key_states[state_name] is True:
self.key_states[state_name] = False
self.start_hold_ts[state_name] = math.inf
self.last_act_time[state_name] = int(time.time() * 1000)

def keyboard_action(self, val, keysym, threshold, mode):

state_name = "keyboard_" + keysym

if self.last_act_time[state_name] > int(time.time() * 1000) - ConfigManager().get_throttle_time() * 1000:
return
if mode == Trigger.SINGLE:
if val > threshold:
if self.key_states[state_name] is False:
pydirectinput.press(keys=keysym)
self.key_states[state_name] = True
self.last_act_time[state_name] = int(time.time() * 1000)
if val < threshold:
self.key_states[state_name] = False

Expand All @@ -242,6 +260,7 @@ def keyboard_action(self, val, keysym, threshold, mode):
elif (val < threshold) and (self.key_states[state_name] is True):
pydirectinput.keyUp(key=keysym)
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)

elif mode == Trigger.DYNAMIC:
if val > threshold:
Expand All @@ -263,7 +282,8 @@ def keyboard_action(self, val, keysym, threshold, mode):
if self.holding[state_name]:
pydirectinput.keyUp(key=keysym)
self.holding[state_name] = False
self.start_hold_ts[state_name] = math.inf
self.start_hold_ts[state_name] = math.inf
self.last_act_time[state_name] = int(time.time() * 1000)

elif mode == Trigger.TOGGLE:
if val > threshold:
Expand All @@ -276,6 +296,7 @@ def keyboard_action(self, val, keysym, threshold, mode):
if self.schedule_toggle_off[state_name] is True:
pydirectinput.keyUp(key=keysym)
self.key_states[state_name] = False
self.last_act_time[state_name] = int(time.time() * 1000)

if val < threshold:
if self.key_states[state_name] is True:
Expand Down Expand Up @@ -303,6 +324,7 @@ def keyboard_action(self, val, keysym, threshold, mode):
if self.key_states[state_name] is True:
self.key_states[state_name] = False
self.start_hold_ts[state_name] = math.inf
self.last_act_time[state_name] = int(time.time() * 1000)

def act(self, blendshape_values) -> None:
"""Trigger devices action base on blendshape values
Expand Down
5 changes: 3 additions & 2 deletions src/gui/pages/page_select_gestures.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ def dialog_callback(self, caller_name, target_gesture):
div["volume_bar"].grid()
div["tips_label"].grid()
div["subtle_label"].grid()
div["timer_slider"].grid()
div["timer_label"].grid()
if 'blink' in target_gesture:
div["timer_slider"].grid()
div["timer_label"].grid()
div["trigger_dropdown"].grid()
thres_value = div["slider"].get() / 100
trigger = Trigger(div["trigger_dropdown"].get())
Expand Down
39 changes: 39 additions & 0 deletions src/gui/pages/page_setting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from tkinter import messagebox
from src.config_manager import ConfigManager
from src.controllers import MouseController
from functools import partial
import tkinter as tk

APP_NAME = 'FaceCommander'
logger = logging.getLogger("PageSetting")
Expand Down Expand Up @@ -145,6 +147,43 @@ def __init__(self, master, master_callback:callable, **kwargs):
pady=120,
sticky="nw")

self.slider = customtkinter.CTkSlider(master=self,
from_=1,
to=300,
width=250,
number_of_steps=300,
command=partial(self.slider_drag_callback))
self.slider.bind("<Button-1>",
partial(self.slider_mouse_down_callback))
self.slider.bind("<ButtonRelease-1>",
partial(self.slider_mouse_up_callback))
self.slider.grid(row=0,
column=0,
padx=(5, 0),
pady=(160, 10),
sticky="nw")

self.slider_label = customtkinter.CTkLabel(master=self,
text="0\t Throttle time\t\t 3s",
text_color="#868686",
justify=tk.LEFT)
self.slider_label.cget("font").configure(size=11)
self.slider_label.grid(row=0,
column=0,
padx=(10, 0),
pady=(182, 10),
sticky="nw")

def slider_drag_callback(self, slider_value: str):
self.slider_dragging = True

def slider_mouse_down_callback(self, event):
self.slider_dragging = True

def slider_mouse_up_callback(self, event):
self.slider_dragging = False
ConfigManager().set_throttle_time(self.slider.get()/100)

def open_log_directory(self):
log_file_path = App().logPath

Expand Down

0 comments on commit bd60b87

Please sign in to comment.