Skip to content

Commit

Permalink
Correctly report is_pressed during release events (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed Mar 29, 2018
1 parent 083b78f commit 9c68587
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,19 +206,14 @@ def init(self):
# https://github.com/boppreh/keyboard/issues/22
self.modifier_states = {} # "alt" -> "allowed"

def invoke_callbacks(self, event, mapping):
# Currently only Windows is filling this value
# TODO: change to event.modifiers to avoid race conditions on
# non-blocking callbacks.
with _pressed_events_lock:
hotkey = tuple(sorted(_pressed_events))
return [callback(event) for callback in mapping[hotkey]]

def pre_process_event(self, event):
for key_hook in self.nonblocking_keys[event.scan_code]:
key_hook(event)

self.invoke_callbacks(event, self.nonblocking_hotkeys)
with _pressed_events_lock:
hotkey = tuple(sorted(_pressed_events))
for callback in self.nonblocking_hotkeys[hotkey]:
callback(event)

return event.scan_code or (event.name and event.name != 'unknown')

Expand All @@ -240,28 +235,35 @@ def direct_callback(self, event):
return False

event_type = event.event_type
scan_code = event.scan_code

# Update tables of currently pressed keys and modifiers.
with _pressed_events_lock:
if event_type == KEY_DOWN:
if is_modifier(scan_code): self.active_modifiers.add(scan_code)
_pressed_events[scan_code] = event
hotkey = tuple(sorted(_pressed_events))
if event_type == KEY_UP:
self.active_modifiers.discard(scan_code)
if scan_code in _pressed_events: del _pressed_events[scan_code]

# Mappings based on individual keys instead of hotkeys.
for key_hook in self.blocking_keys[event.scan_code]:
for key_hook in self.blocking_keys[scan_code]:
if not key_hook(event):
return False

# Update tables of currently pressed keys and modifiers.
if event_type == KEY_DOWN:
if is_modifier(event.scan_code): self.active_modifiers.add(event.scan_code)
with _pressed_events_lock:
_pressed_events[event.scan_code] = event

# Default accept.
accept = True

if self.blocking_hotkeys:
if self.filtered_modifiers[event.scan_code]:
if self.filtered_modifiers[scan_code]:
origin = 'modifier'
modifiers_to_update = set([event.scan_code])
modifiers_to_update = set([scan_code])
else:
modifiers_to_update = self.active_modifiers
callback_results = self.invoke_callbacks(event, self.blocking_hotkeys)
if is_modifier(scan_code):
modifiers_to_update = modifiers_to_update | {scan_code}
callback_results = [callback(event) for callback in self.blocking_hotkeys[hotkey]]
if callback_results:
accept = all(callback_results)
origin = 'hotkey'
Expand All @@ -275,17 +277,11 @@ def direct_callback(self, event):
if new_accept is not None: accept = new_accept
self.modifier_states[key] = new_state

# Update tables of currently pressed keys and modifiers.
if event_type == KEY_UP:
self.active_modifiers.discard(event.scan_code)
with _pressed_events_lock:
if event.scan_code in _pressed_events: del _pressed_events[event.scan_code]

if accept:
if event_type == KEY_DOWN:
_logically_pressed_keys[event.scan_code] = event
elif event_type == KEY_UP and event.scan_code in _logically_pressed_keys:
del _logically_pressed_keys[event.scan_code]
_logically_pressed_keys[scan_code] = event
elif event_type == KEY_UP and scan_code in _logically_pressed_keys:
del _logically_pressed_keys[scan_code]

# Queue for handlers that won't block the event.
self.queue.put(event)
Expand Down

0 comments on commit 9c68587

Please sign in to comment.