Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement "Bounce keys" feature #32

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions evdevremapkeys/evdevremapkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,45 @@
registered_devices = {}


async def handle_events(input: InputDevice, output: UInput, remappings, modifier_groups):
async def handle_events(input: InputDevice, output: UInput, remappings, modifier_groups, bouncekeys):
active_group = {}
last_code = None
last_timestamp = None
try:
async for event in input.async_read_loop():
code = event.code

if not active_group:
active_mappings = remappings
else:
active_mappings = modifier_groups[active_group['name']]

if (event.code == active_group.get('code') or
(event.code in active_mappings and
'modifier_group' in active_mappings.get(event.code)[0])):
if (code == active_group.get('code') or
(code in active_mappings and
'modifier_group' in active_mappings.get(code)[0])):
if event.value == 1:
active_group['name'] = \
active_mappings[event.code][0]['modifier_group']
active_group['code'] = event.code
elif event.value == 0:
active_group = {}
else:
if event.code in active_mappings:
remap_event(output, event, active_mappings[event.code])
else:
output.write_event(event)
output.syn()
ignore_event = False
if bouncekeys is not None:
now = event.timestamp()
if event.value == 1:
if code != ecodes.KEY_RESERVED and \
code == last_code and \
now - last_timestamp < bouncekeys:
ignore_event = True
last_code = code
last_timestamp = now
if not ignore_event:
if code in active_mappings:
remap_event(output, event, active_mappings[code])
else:
output.write_event(event)
output.syn()
finally:
del registered_devices[input.path]
print('Unregistered: %s, %s, %s' % (input.name, input.path, input.phys),
Expand Down Expand Up @@ -293,6 +308,8 @@ def register_device(device, loop: AbstractEventLoop):
if 'modifier_groups' in device:
modifier_groups = device['modifier_groups']

bouncekeys = device['bouncekeys'] if 'bouncekeys' in device else None

def flatmap(lst):
return [l2 for l1 in lst for l2 in l1]

Expand All @@ -309,7 +326,7 @@ def flatmap(lst):
output = UInput(caps, name=device['output_name'])
print('Registered: %s, %s, %s' % (input.name, input.path, input.phys), flush=True)
task = loop.create_task(
handle_events(input, output, remappings, modifier_groups),
handle_events(input, output, remappings, modifier_groups, bouncekeys),
name=input.name)
registered_devices[input.path] = {
'task': task,
Expand Down