Skip to content

Commit

Permalink
Merge
Browse files Browse the repository at this point in the history
  • Loading branch information
boppreh committed Nov 13, 2016
2 parents d8a54cf + c758147 commit c20bb96
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 192 deletions.
58 changes: 30 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Take full control of your keyboard with this small Python library. Hook global e

**API docs**: https://github.com/boppreh/keyboard#api

**Installation**: `pip install keyboard`

- Global event hook on all keyboards (captures keys regardless of focus).
- **Listen** and **sends** keyboard events.
- Works with **Windows** and **Linux** (requires sudo).
Expand Down Expand Up @@ -124,7 +126,7 @@ This program makes no attempt to hide itself, so don't use it for keyloggers.
<a name="keyboard.matches"/>
## keyboard.**matches**(event, name)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L84)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L86)


Returns True if the given event represents the same key as the one given in
Expand All @@ -135,7 +137,7 @@ Returns True if the given event represents the same key as the one given in
<a name="keyboard.is_pressed"/>
## keyboard.**is\_pressed**(key)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L101)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L103)


Returns True if the key is pressed.
Expand All @@ -149,7 +151,7 @@ Returns True if the key is pressed.
<a name="keyboard.canonicalize"/>
## keyboard.**canonicalize**(hotkey)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L123)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L125)


Splits a user provided hotkey into a list of steps, each one made of a list
Expand All @@ -170,7 +172,7 @@ more than one physical key (e.g. two 'ctrl' keys).
<a name="keyboard.call_later"/>
## keyboard.**call\_later**(fn, args=(), delay=0.001)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L157)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L159)


Calls the provided function in a new thread after waiting some time.
Expand All @@ -182,7 +184,7 @@ the current execution flow.
<a name="keyboard.clear_all_hotkeys"/>
## keyboard.**clear\_all\_hotkeys**()

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L166)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L168)


Removes all hotkey handlers. Note some functions such as 'wait' and 'record'
Expand All @@ -202,7 +204,7 @@ Alias for `clear_all_hotkeys`.
<a name="keyboard.add_hotkey"/>
## keyboard.**add\_hotkey**(hotkey, callback, args=(), blocking=True, timeout=1)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L181)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L183)


Invokes a callback every time a key combination is pressed. The hotkey must
Expand Down Expand Up @@ -244,7 +246,7 @@ Alias for `add_hotkey`.
<a name="keyboard.hook"/>
## keyboard.**hook**(callback)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L246)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L248)


Installs a global listener on all available keyboards, invoking `callback`
Expand All @@ -266,15 +268,15 @@ Returns the given callback for easier development.
<a name="keyboard.unhook"/>
## keyboard.**unhook**(callback)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L265)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L267)

Removes a previously hooked callback.


<a name="keyboard.unhook_all"/>
## keyboard.**unhook\_all**()

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L269)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L271)


Removes all keyboard hooks in use, including hotkeys, abbreviations, word
Expand All @@ -285,7 +287,7 @@ listeners, [`record`](#keyboard.record)ers and [`wait`](#keyboard.wait)s.
<a name="keyboard.hook_key"/>
## keyboard.**hook\_key**(key, keydown\_callback=&lt;lambda&gt;, keyup\_callback=&lt;lambda&gt;)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L278)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L280)


Hooks key up and key down events for a single key. Returns the event handler
Expand All @@ -300,7 +302,7 @@ affects it aswell.
<a name="keyboard.on_press"/>
## keyboard.**on\_press**(callback)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L299)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L301)


Invokes `callback` for every KEY_DOWN event. For details see [`hook`](#keyboard.hook).
Expand All @@ -310,7 +312,7 @@ Invokes `callback` for every KEY_DOWN event. For details see [`hook`](#keyboard.
<a name="keyboard.on_release"/>
## keyboard.**on\_release**(callback)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L305)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L307)


Invokes `callback` for every KEY_UP event. For details see [`hook`](#keyboard.hook).
Expand All @@ -320,7 +322,7 @@ Invokes `callback` for every KEY_UP event. For details see [`hook`](#keyboard.ho
<a name="keyboard.remove_hotkey"/>
## keyboard.**remove\_hotkey**(hotkey\_or\_handler)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L332)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L334)


Removes a previously registered hotkey. Accepts either the hotkey used
Expand All @@ -338,7 +340,7 @@ Alias for `remove_hotkey`.
<a name="keyboard.add_word_listener"/>
## keyboard.**add\_word\_listener**(word, callback, triggers=[&#x27;space&#x27;], match\_suffix=False, timeout=2)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L344)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L346)


Invokes a callback every time a sequence of characters is typed (e.g. 'pet')
Expand Down Expand Up @@ -375,7 +377,7 @@ Alias for `add_word_listener`.
<a name="keyboard.remove_word_listener"/>
## keyboard.**remove\_word\_listener**(word\_or\_handler)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L395)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L397)


Removes a previously registered word listener. Accepts either the word used
Expand All @@ -393,7 +395,7 @@ Alias for `remove_word_listener`.
<a name="keyboard.add_abbreviation"/>
## keyboard.**add\_abbreviation**(source\_text, replacement\_text, match\_suffix=True, timeout=2)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L403)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L405)


Registers a hotkey that replaces one typed text with another. For example
Expand Down Expand Up @@ -422,7 +424,7 @@ Alias for `add_abbreviation`.
<a name="keyboard.stash_state"/>
## keyboard.**stash\_state**()

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L429)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L431)


Builds a list of all currently pressed scan codes, releases them and returns
Expand All @@ -433,7 +435,7 @@ the list. Pairs well with [`restore_state`](#keyboard.restore_state).
<a name="keyboard.restore_state"/>
## keyboard.**restore\_state**(scan\_codes)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L439)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L441)


Given a list of scan_codes ensures these keys, and only these keys, are
Expand All @@ -444,7 +446,7 @@ pressed. Pairs well with [`stash_state`](#keyboard.stash_state).
<a name="keyboard.write"/>
## keyboard.**write**(text, delay=0, restore\_state\_after=True)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L451)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L453)


Sends artificial keyboard events to the OS, simulating the typing of a given
Expand All @@ -465,7 +467,7 @@ beginning. Defaults to True.
<a name="keyboard.to_scan_code"/>
## keyboard.**to\_scan\_code**(key)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L494)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L496)


Returns the scan code for a given key name (or scan code, i.e. do nothing).
Expand All @@ -477,7 +479,7 @@ one of the scan codes will be chosen.
<a name="keyboard.send"/>
## keyboard.**send**(combination, do\_press=True, do\_release=True)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L506)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L508)


Sends OS events that perform the given hotkey combination.
Expand All @@ -499,31 +501,31 @@ Note: keys are released in the opposite order they were pressed.
<a name="keyboard.press"/>
## keyboard.**press**(combination)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L531)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L533)

Presses and holds down a key combination (see [`send`](#keyboard.send)).


<a name="keyboard.release"/>
## keyboard.**release**(combination)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L535)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L537)

Releases a key combination (see [`send`](#keyboard.send)).


<a name="keyboard.press_and_release"/>
## keyboard.**press\_and\_release**(combination)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L539)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L541)

Presses and releases the key combination (see [`send`](#keyboard.send)).


<a name="keyboard.wait"/>
## keyboard.**wait**(combination)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L543)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L545)


Blocks the program execution until the given key combination is pressed.
Expand All @@ -533,7 +535,7 @@ Blocks the program execution until the given key combination is pressed.
<a name="keyboard.record"/>
## keyboard.**record**(until=&#x27;escape&#x27;)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L553)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L555)


Records all keyboard events from all keyboards until the user presses the
Expand All @@ -549,7 +551,7 @@ Note: for more details on the keyboard hook and events see [`hook`](#keyboard.ho
<a name="keyboard.play"/>
## keyboard.**play**(events, speed\_factor=1.0)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L569)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L571)


Plays a sequence of recorded events, maintaining the relative time
Expand All @@ -570,7 +572,7 @@ Alias for `play`.
<a name="keyboard.get_typed_strings"/>
## keyboard.**get\_typed\_strings**(events, allow\_backspace=True)

[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L597)
[\[source\]](https://github.com/boppreh/keyboard/blob/master/keyboard/__init__.py#L599)


Given a sequence of events, tries to deduce what strings were typed.
Expand Down
2 changes: 2 additions & 0 deletions keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
**API docs**: https://github.com/boppreh/keyboard#api
**Installation**: `pip install keyboard`
- Global event hook on all keyboards (captures keys regardless of focus).
- **Listen** and **sends** keyboard events.
- Works with **Windows** and **Linux** (requires sudo).
Expand Down
2 changes: 1 addition & 1 deletion keyboard/_nixcommon.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def output_file(self):
def read_event(self):
data = self.input_file.read(struct.calcsize(event_bin_format))
seconds, microseconds, type, code, value = struct.unpack(event_bin_format, data)
return (seconds + microseconds / 1e6, type, code, value)
return seconds + microseconds / 1e6, type, code, value, self.path

def write_event(self, type, code, value):
integer, fraction = divmod(now(), 1)
Expand Down
4 changes: 2 additions & 2 deletions keyboard/_nixkeyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def listen(queue):
build_tables()

while True:
time, type, code, value = device.read_event()
time, type, code, value, device_id = device.read_event()
if type != EV_KEY:
continue

Expand All @@ -130,7 +130,7 @@ def listen(queue):
pressed_modifiers.discard(name)

is_keypad = scan_code in keypad_scan_codes
queue.put(KeyboardEvent(event_type=event_type, scan_code=scan_code, name=name, time=time, is_keypad=is_keypad))
queue.put(KeyboardEvent(event_type=event_type, scan_code=scan_code, name=name, time=time, device=device_id, is_keypad=is_keypad))

def write_event(scan_code, is_down):
build_device()
Expand Down
2 changes: 1 addition & 1 deletion keyboard/_nixmouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def listen(queue):
build_device()

while True:
time, type, code, value = device.read_event()
time, type, code, value, device = device.read_event()
if type == EV_SYN or type == EV_MSC:
continue

Expand Down
Loading

0 comments on commit c20bb96

Please sign in to comment.