
    i:                     J   S r SSKJr  SrSSKrSSKrSSK	r
SSKJrJr  SSKr\" \SS5      =(       d    \R                   \l         \\4  S rS rSSKrSS	KJr  S r " S S\ 5      r! " S S\5      rSSK"r#\#RH                  " 5       S:X  a  SSK%J&r'  O^\#RH                  " 5       S:X  a  SSK%J(r'  OB\#RH                  " 5       S:X  a  SSK%J)r'  O&\*" SRW                  \#RH                  " 5       5      5      eSSK,J-r-J.r.J/r/  SSK0J1r2  SSK3J4r4J5r5J6r6  \7" 5       r8S r9\" 5       r:0 r;\;r<0 r= " S S\25      r>\>" 5       r?SLS  jr@S! rASMS" jrB\BrCS# rDS$ rES% rFSNS& jrG0 rHS'S( 4S) jrISOS* jrJSOS+ jrKSOS, jrLSOS- jrMSOS. jrNS/ rO\OrPS0 rQS1 rR\PrSS2 rT\PrUS3 rVS4 rW0 rXSPS5 jrY\YrZS6 r[\[=r\r]S7 r^\^=r_=r`raSQS8 jrb\[rcS9 rdS: reS; rfSRS< jrgSSS= jrhSTS> jriSOS? jrjSOS@ jrkSLSA jrlSLSB jrmSqnSTSC jroSD rpSUSE jrqSVSF jrr\rrs0 rtSG/S'SH4SI jruSJ rvSWSK jrw\urx\wry\vrzg! \ a    S
 rS rSSKrSSKJr   GNf = f)XuK  
keyboard
========

Take full control of your keyboard with this small Python library. Hook global events, register hotkeys, simulate key presses and much more.

## Features

- **Global event hook** on all keyboards (captures keys regardless of focus).
- **Listen** and **send** keyboard events.
- Works with **Windows** and **Linux** (requires sudo), with experimental **OS X** support (thanks @glitchassassin!).
- **Pure Python**, no C modules to be compiled.
- **Zero dependencies**. Trivial to install and deploy, just copy the files.
- **Python 2 and 3**.
- Complex hotkey support (e.g. `ctrl+shift+m, ctrl+space`) with controllable timeout.
- Includes **high level API** (e.g. [record](#keyboard.record) and [play](#keyboard.play), [add_abbreviation](#keyboard.add_abbreviation)).
- Maps keys as they actually are in your layout, with **full internationalization support** (e.g. `Ctrl+ç`).
- Events automatically captured in separate thread, doesn't block main program.
- Tested and documented.
- Doesn't break accented dead keys (I'm looking at you, pyHook).
- Mouse support available via project [mouse](https://github.com/boppreh/mouse) (`pip install mouse`).

## Usage

Install the [PyPI package](https://pypi.python.org/pypi/keyboard/):

    pip install keyboard

or clone the repository (no installation required, source files are sufficient):

    git clone https://github.com/boppreh/keyboard

or [download and extract the zip](https://github.com/boppreh/keyboard/archive/master.zip) into your project folder.

Then check the [API docs below](https://github.com/boppreh/keyboard#api) to see what features are available.


## Example


```py
import keyboard

keyboard.press_and_release('shift+s, space')

keyboard.write('The quick brown fox jumps over the lazy dog.')

keyboard.add_hotkey('ctrl+shift+a', print, args=('triggered', 'hotkey'))

# Press PAGE UP then PAGE DOWN to type "foobar".
keyboard.add_hotkey('page up, page down', lambda: keyboard.write('foobar'))

# Blocks until you press esc.
keyboard.wait('esc')

# Record events until 'esc' is pressed.
recorded = keyboard.record(until='esc')
# Then replay back at three times the speed.
keyboard.play(recorded, speed_factor=3)

# Type @@ then press space to replace with abbreviation.
keyboard.add_abbreviation('@@', 'my.long.email@example.com')

# Block forever, like `while True`.
keyboard.wait()
```

## Known limitations:

- Events generated under Windows don't report device id (`event.device == None`). [#21](https://github.com/boppreh/keyboard/issues/21)
- Media keys on Linux may appear nameless (scan-code only) or not at all. [#20](https://github.com/boppreh/keyboard/issues/20)
- Key suppression/blocking only available on Windows. [#22](https://github.com/boppreh/keyboard/issues/22)
- To avoid depending on X, the Linux parts reads raw device files (`/dev/input/input*`)
but this requires root.
- Other applications, such as some games, may register hooks that swallow all 
key events. In this case `keyboard` will be unable to report events.
- This program makes no attempt to hide itself, so don't use it for keyloggers or online gaming bots. Be responsible.
    )print_functionz0.13.5N)ThreadLock	monotonicc                 "    [        U [        5      $ N)
isinstance
basestringxs    U/home/kali/github-search/github-env/lib/python3.13/site-packages/keyboard/__init__.py<lambda>r   _   s    
1j1    c                 .    [        U [        [        45      $ r   )r	   intlongr   s    r   r   r   `   s    :a#t5r   )_Eventc                 "    [        U [        5      $ r   )r	   strr   s    r   r   r   f   s    
1c*r   c                 "    [        U [        5      $ r   )r	   r   r   s    r   r   r   g   s    :a-r   )Eventc                 .    [        U [        [        45      $ r   )r	   listtupler   s    r   r   r   j   s    ZD%=1r   c                       \ rS rSrSrg)_Statem    N)__name__
__module____qualname____firstlineno____static_attributes__r   r   r   r   r   m   s    dr   r   c                       \ rS rSrS rSrg)r   r   c                 B     [         R                  " U S5      (       a  g M  )Ng      ?)_UninterruptibleEventwaitselfs    r   r(   _Event.waits   s     $))$44 r   r   N)r   r    r!   r"   r(   r#   r   r   r   r   r   r   s    r   r   Windows   )_winkeyboardLinux)_nixkeyboardDarwin)_darwinkeyboardzUnsupported platform '{}')KEY_DOWNKEY_UPKeyboardEvent)GenericListener)all_modifierssided_modifiersnormalize_namec                     [        U 5      (       a	  U [        ;   $ [        (       d   S [         5       n[        R                  " U6   U [        ;   $ )zA
Returns True if `key` is a scan code or name of a modifier key.
c              3   :   #    U  H  n[        US 5      v   M     g7f)FNkey_to_scan_codes).0names     r   	<genexpr>is_modifier.<locals>.<genexpr>   s     S]T+D%88]s   )_is_strr7   _modifier_scan_codesupdate)key
scan_codess     r   is_modifierrG      sB     s||m####S]SJ ''4***r   c                   F   \ rS rSr0 S\S4S_S\S4S_S\S4S_S\S4S_S	\S4S
_S	\S4S_S\S4S_S\S4S_S\S4S_S\S4S_S\S4S_S\S4S_S	\S4S_S	\S4S_S\S4S_S\S4S_S\S4S_S\S4SS\S4SS\S4SS	\S4SS	\S4SS\S4SS\S4S0ErS rS rS r	S r
Srg)_KeyboardListener   freemodifier)FTrK   )FFpendingrM   )TTrK   )FTallowed
suppressed)FFrK   )FFrO   rN   hotkey)FNrK   )FNrO   )FNrN   other)TTrN   )FFrN   c                    [         R                  " 5         [        5       U l        / U l        [
        R                  " [        5      U l        [
        R                  " [        5      U l	        [
        R                  " [        5      U l
        [
        R                  " [        5      U l        [
        R                  " 5       U l        SU l        0 U l        g NF)_os_keyboardinitsetactive_modifiersblocking_hooks_collectionsdefaultdictr   blocking_keysnonblocking_keysblocking_hotkeysnonblocking_hotkeysCounterfiltered_modifiersis_replayingmodifier_statesr)   s    r   rU   _KeyboardListener.init   s     # )55d; , 8 8 > , 8 8 >#/#;#;D#A "."6"6"8!  "r   c                 h   U R                   UR                      H  nU" U5        M     [           [        [	        [
        5      5      nS S S 5        U R                  W    H  nU" U5        M     UR                  =(       d"    UR                  =(       a    UR                  S:g  $ ! , (       d  f       Na= f)Nunknown)r\   	scan_code_pressed_events_lockr   sorted_pressed_eventsr^   r?   )r*   eventkey_hookrP   callbacks        r   pre_process_event#_KeyboardListener.pre_process_event   s    --eoo>HUO ? "6/23F "008HUO 9 J5::#I%**	2IJ "!s   B##
B1c                   ^ U R                   (       a  g[        U4S jU R                   5       5      (       d  gTR                  nTR                  n[
           U[        :X  a4  [        U5      (       a  U R                  R                  U5        T[        U'   [        [        [        5      5      nU[        :X  a,  U R                  R                  U5        U[        ;   a  [        U	 SSS5        U R                  U    H  nU" T5      (       a  M    g   SnU R                   (       a  U R"                  U   (       a  Sn[%        U/5      nO\U R                  n[        U5      (       a  X1-  nU R                   W    V	s/ s H
  o" T5      PM     n
n	U
(       a  [        U
5      nSnOSn[        U5       HX  nU R&                  R)                  US5      X'4nU R*                  U   u  pnU(       a  [-        U5        Ub  UnXR&                  U'   MZ     U(       a/  U[        :X  a
  T[.        U'   OU[        :X  a  U[.        ;   a  [.        U	 U R0                  R3                  T5        U$ ! , (       d  f       GN= fs  sn	f )	aK  
This function is called for every OS keyboard event and decides if the
event should be blocked or not, and passes a copy of the event to
other, non-blocking, listeners.

There are two ways to block events: remapped keys, which translate
events by suppressing and re-emitting; and blocked hotkeys, which
suppress specific hotkeys.
Tc              3   2   >#    U  H  o" T5      v   M     g 7fr   r   )r>   hookrj   s     r   r@   4_KeyboardListener.direct_callback.<locals>.<genexpr>   s     ?+>44;;+>s   FNrL   rP   rQ   rK   )ra   allrX   
event_typerf   rg   r3   rG   rW   addri   r   rh   r4   discardr[   r]   r`   rV   rb   gettransition_tablepress_logically_pressed_keysqueueput)r*   rj   rt   rf   rP   rk   acceptoriginmodifiers_to_updaterl   callback_resultsrE   transition_tupleshould_press
new_accept	new_states    `              r   direct_callback!_KeyboardListener.direct_callback   s"    ?4+>+>???%%
OO	 "X%y))4+@+@+D+DY+O-2	*6/23FV#%%--i8/_Y5O " **95HE?? 6
   &&y1#&)9+&6#&*&;&;#y))*=*K'DHDYDYZ`Da#bDaHUODa #b# !12F%F$F12$($8$8$<$<S&$I:#^ 6:6K6KL\6]3)s)J6,5$$S) 3 X%5:'	2v%)7N*N+I6 	

ua "!2 $cs   BI9I/
I,c                 D    [         R                  " U R                  5        g r   )rT   listenr   r)   s    r   r   _KeyboardListener.listen%  s    D001r   )	rW   rX   r]   r[   r`   ra   rb   r^   r\   N)r   r    r!   r"   r4   r3   rx   rU   rm   r   r   r#   r   r   r   rI   rI      s   ) 
v,.D) 
x,.G) 
v,.D)  
x,.G!)" 
v,.D#)$ 
x,.J%)& 
v,.D')( 
x,.G)), 
v*.D-). 
x*.D/)0 
v*.J1)2 
x*.J3)4 
v*.J5)6 
x*.J7)8 
v*.G9): 
x*.G;)> 
v).D?)@ 
x).D	v).G	x).G 
v).G	x).G	v).G	x).GQ)V" 	KEN2r   rI   c                   ^ [        U 5      (       a  U 4$ [        U 5      (       a  [        S U  5       S5      $ [        U 5      (       d2  [	        S[        [        U 5      5      -   S-   [        U 5      -   S-   5      e[        U 5      nU[        ;   a6  [        SU-   S5      m[        SU-   S5      nT[        U4S	 jU 5       5      -   $  [        [        R                  " S
 [        R                  " U5       5       5      5      nSnU(       d+  U(       a$  [	        SR#                  [        U 5      5      U5      eU$ ! [         [        4 a  nSnUn SnANMSnAff = f)zL
Returns a list of scan codes associated with this key (name or scan code).
c              3   8   #    U  H  n[        U5      v   M     g 7fr   r<   )r>   is     r   r@   $key_to_scan_codes.<locals>.<genexpr>1  s     6#Q%a((#   r   zUnexpected key type z	, value ()left Fright c              3   6   >#    U  H  oT;  d  M
  Uv   M     g 7fr   r   )r>   cleft_scan_codess     r   r@   r   9  s     &_2BQF^qq2Bs   		c              3   .   #    U  H  u  pUS 4v   M     g7f)TNr   )r>   rf   rL   s      r   r@   r   =  s     *zXyATIt+<Xys   Nz&Key {} is not mapped to any known key.)
_is_number_is_listsumrB   
ValueErrorr   typereprr9   r8   r=   r   rY   OrderedDictrT   map_nameKeyErrorformat)rE   error_if_missing
normalizedright_scan_codeste	exceptionr   s          @r   r=   r=   *  s4    #v	#6#6;;S\\/#d3i.@;NQUVYQZZ]``aa$J_$+Gj,@%H,X
-BEJ&_2B&_!___,***zXdXmXmnxXy*zz{
 !AHHcSUVWW j! s   <D6 6EEEc                    [        U 5      (       d  [        U 5      S:X  a  [        U 5      nU4nU4nU$ [        U 5      (       a7  [	        [        [        U 5      5      (       d  [        S U  5       5      nU4nU$ U $ / n[        R                  " SU 5       H;  n[        R                  " SU5      nUR                  [        S U 5       5      5        M=     [        U5      $ )a  
Parses a user-provided hotkey into nested tuples representing the
parsed structure, with the bottom values being lists of scan codes.
Also accepts raw scan codes, which are then wrapped in the required
number of nestings.

Example:

    parse_hotkey("alt+shift+a, alt+b, c")
    #    Keys:    ^~^ ^~~~^ ^  ^~^ ^  ^
    #    Steps:   ^~~~~~~~~~^  ^~~~^  ^

    # ((alt_codes, shift_codes, a_codes), (alt_codes, b_codes), (c_codes,))
r-   c              3   8   #    U  H  n[        U5      v   M     g 7fr   r<   )r>   ks     r   r@   parse_hotkey.<locals>.<genexpr>^  s     >v!*1--vr   z,\s?z\s?\+\s?c              3   8   #    U  H  n[        U5      v   M     g 7fr   r<   )r>   rE   s     r   r@   r   f  s     BTc,S11Tr   )
r   lenr=   r   anymapr   _resplitappend)rP   rF   stepstepskeyss        r   parse_hotkeyr   H  s     &S[A-&v.
}	&		3x())>v>>DGELE		'6*yyd+UBTBBC + <r   c                    S[         l        [        U 5      nU H`  nU(       a"  U H  n[        R                  " US   5        M     U(       d  M5  [        U5       H  n[        R                  " US   5        M     Mb     S[         l        g)a  
Sends OS events that perform the given *hotkey* hotkey.

- `hotkey` can be either a scan code (e.g. 57 for space), single key
(e.g. 'space') or multi-key, multi-step hotkey (e.g. 'alt+F4, enter').
- `do_press` if true then press events are sent. Defaults to True.
- `do_release` if true then release events are sent. Defaults to True.

    send(57)
    send('ctrl+alt+del')
    send('alt+F4, enter')
    send('shift+s')

Note: keys are released in the opposite order they were pressed.
Tr   FN)	_listenerra   r   rT   ry   reversedrelease)rP   do_press
do_releaseparsedr   rF   s         r   sendr   i  sp      "I&!F"
"":a=1 # :&tn
$$Z]3 -  #Ir   c                     [        U SS5        g)z.Presses and holds down a hotkey (see `send`). TFNr   rP   s    r   ry   ry     s    ur   c                     [        U SS5        g)z Releases a hotkey (see `send`). FTNr   r   s    r   r   r     s    r   c                   ^ [         R                  5         [        U 5      (       a  [           U [        ;   sSSS5        $ [        U 5      n[        U5      S:  a  [        S5      e[           [        [        5      mSSS5        US    H  n[        U4S jU 5       5      (       a  M    g   g! , (       d  f       Nz= f! , (       d  f       NH= f)z
Returns True if the key is pressed.

    is_pressed(57) #-> True
    is_pressed('space') #-> True
    is_pressed('ctrl+space') #-> True
Nr-   zRImpossible to check if multi-step hotkeys are pressed (`a+b` is ok, `a, b` isn't).r   c              3   ,   >#    U  H	  oT;   v   M     g 7fr   r   )r>   rf   pressed_scan_codess     r   r@   is_pressed.<locals>.<genexpr>  s     OJy 22Js   FT)
r   start_if_necessaryr   rg   ri   r   r   r   rV   r   )rP   r   rF   r   s      @r   
is_pressedr     s       "&!_, "!  E
5zA~mnn 
 1 
Ah
OJOOO   "! 
	s   	B+*B<+
B9<
C
c                 H   ^ ^^ [        UUU 4S jS9nUR                  5         g)z
Calls the provided function in a new thread after waiting some time.
Useful for giving the system some time to process an event, without blocking
the current execution flow.
c                  :   > [         R                  " T5      T" T 6 4$ r   )_timesleep)argsdelayfns   r   r   call_later.<locals>.<lambda>  s    U[[%7T$Cr   )targetN)_Threadstart)r   r   r   threads   ``` r   
call_laterr     s     CDF
LLNr   Fc                      g r   r   r   r   r   r   r     s    Tr   c                 8  ^ ^^^ U(       aJ  [         R                  5         [         R                  R                  [         R                  R                  snmO![         R
                  [         R                  snmU" T 5        U UUU4S jmT=[        T '   [        T'   T$ )a+  
Installs a global listener on all available keyboards, invoking `callback`
each time a key is pressed or released.

The event passed to the callback is of type `keyboard.KeyboardEvent`,
with the following attributes:

- `name`: an Unicode representation of the character (e.g. "&") or
description (e.g.  "space"). The name is always lower-case.
- `scan_code`: number representing the physical key, e.g. 55.
- `time`: timestamp of the time the event occurred, with as much precision
as given by the OS.

Returns the given callback for easier development.
c                  @   > [         T 	 [         T	 T" T 5        T" 5         g r   _hooks)rl   	on_removeremoveremove_s   r   r   hook.<locals>.remove_  s    87Oxr   )r   r   rX   r   r   add_handlerremove_handlerr   )rl   suppressr   r   r   r   s   ` ` @@r   rq   rq     sz      $$&"1188):R:R:Y:Y"..	0H0H
8 
 *10F8vgNr   c                     ^  [        U 4S jUS9$ )zF
Invokes `callback` for every KEY_DOWN event. For details see `hook`.
c                 H   > U R                   [        :H  =(       d    T" U 5      $ r   rt   r4   r   rl   s    r   r   on_press.<locals>.<lambda>  s    !,,&0?HQK?r   r   rq   rl   r   s   ` r   on_pressr     s     ?(SSr   c                     ^  [        U 4S jUS9$ )zD
Invokes `callback` for every KEY_UP event. For details see `hook`.
c                 H   > U R                   [        :H  =(       d    T" U 5      $ r   rt   r3   r   s    r   r   on_release.<locals>.<lambda>  s    !,,(2AhqkAr   r   r   r   s   ` r   
on_releaser     s     AHUUr   c                    ^ ^^^^ [         R                  5         U(       a  [         R                  O[         R                  m[	        T 5      mT H  nTU   R                  T5        M     UU UUU4S jmT=[        T'   =[        T '   [        T'   T$ )z
Hooks key up and key down events for a single key. Returns the event handler
created. To remove a hooked key use `unhook_key(key)` or
`unhook_key(handler)`.

Note: this function shares state with hotkeys, so `clear_all_hotkeys`
affects it as well.
c                  j   > [         T	 [         T	 [         T	 T H  n TU    R                  T5        M     g r   )r   r   )rf   rl   rE   r   rF   stores    r   r   hook_key.<locals>.remove_  s6    83K7O#I)##H- $r   )r   r   r[   r\   r=   r   r   )rE   rl   r   rf   r   rF   r   s   ``  @@@r   hook_keyr     sz       "'/I##Y5O5OE"3'J	i)  . . 8?>F8>vc{VG_Nr   c                 "   ^ [        U U4S jUS9$ )zY
Invokes `callback` for KEY_DOWN event related to the given key. For details see `hook`.
c                 H   > U R                   [        :H  =(       d    T" U 5      $ r   r   r   s    r   r   on_press_key.<locals>.<lambda>  s    1<<6#9#HXa[#Hr   r   r   rE   rl   r   s    ` r   on_press_keyr     s     CHS[\\r   c                 "   ^ [        U U4S jUS9$ )zW
Invokes `callback` for KEY_UP event related to the given key. For details see `hook`.
c                 H   > U R                   [        :H  =(       d    T" U 5      $ r   r   r   s    r   r    on_release_key.<locals>.<lambda>  s    1<<8#;#Jx{#Jr   r   r   r   s    ` r   on_release_keyr      s     CJU]^^r   c                      [         U    " 5         g)zW
Removes a previously added hook, either by callback or by the return value
of `hook`.
Nr   )r   s    r   unhookr     s    
 6Nr   c                     [         R                  5         [         R                  R                  5         [         R                  R                  5         [         R
                  SS2	 [         R                  SS2	 [        5         g)zo
Removes all keyboard hooks in use, including hotkeys, abbreviations, word
listeners, `record`ers and `wait`s.
N)r   r   r[   clearr\   rX   handlersunhook_all_hotkeysr   r   r   
unhook_allr    sV    
   "!!#$$&  #1r   c                     [        U S SS9$ )zF
Suppresses all key events of the given key, regardless of modifiers.
c                     grS   r   )r   s    r   r   block_key.<locals>.<lambda>  s    5r   Tr   r   rE   s    r   	block_keyr    s     C488r   c                 $   ^ U4S jn[        XSS9$ )zt
Whenever the key `src` is pressed or released, regardless of modifiers,
press or release the hotkey `dst` instead.
c                 \   > U R                   [        :X  a  [        T5        g[        T5        grS   )rt   r3   ry   r   )rj   dsts    r   handlerremap_key.<locals>.handler&  s)    x'#J  CLr   Tr   r   )srcr  r  s    ` r   	remap_keyr  !  s    
 C400r   c                 F   ^ S m[        U4S j[        U 5       5       5      $ )z
Parses a user-provided hotkey. Differently from `parse_hotkey`,
instead of each step being a list of the different scan codes for each key,
each step is a list of all possible combinations of those scan codes.
c                 6    S [         R                  " U 6  5       $ )Nc              3   J   #    U  H  n[        [        U5      5      v   M     g 7fr   )r   rh   )r>   rF   s     r   r@   Bparse_hotkey_combinations.<locals>.combine_step.<locals>.<genexpr>;  s     V<UjfZ())<Us   !#)
_itertoolsproduct)r   s    r   combine_step/parse_hotkey_combinations.<locals>.combine_step5  s     WJ<N<NPT<UVVr   c              3   F   >#    U  H  n[        T" U5      5      v   M     g 7fr   )r   )r>   r   r  s     r   r@   ,parse_hotkey_combinations.<locals>.<genexpr>=  s      L7Kt|D)**7Ks   !)r   r   )rP   r  s    @r   parse_hotkey_combinationsr  /  s!    W L|F7KLLLr   c                   ^ ^^ U(       a  [         R                  O[         R                  mT HM  nU H0  n[        U5      (       d  M  [         R                  U==   S-  ss'   M2     TU   R                  T 5        MO     UUU 4S jnU$ )z.
Hooks a single-step hotkey (e.g. 'shift+a').
r-   c                     > T HM  n U  H0  n[        U5      (       d  M  [        R                  U==   S-  ss'   M2     TU    R                  T5        MO     g )Nr-   )rG   r   r`   r   )rF   rf   combinations	containerr  s     r   r    _add_hotkey_step.<locals>.removeN  sM    &J'	y))00;q@; ( j!((1	 'r   )r   r]   r^   rG   r`   r   )r  r  r   rF   rf   r   r  s   ``    @r   _add_hotkey_stepr   ?  sr     /7	**I<Y<YI
 #
#I9%%,,Y71<7 $ 	*$$W-	 #2 Mr   c                 |  ^ ^^^^^^	^
^^^^^ T(       a	  T4U4S jjm[         R                  5         [        T 5      mU(       a  [        O[        m
[        T5      S:X  a=  UU
4S jn[        UTS   T5      mUU UU4S jmT=[        T '   =[        T'   [        T'   T$ [        5       mSTl	        STl
        / Tl        [        S5      Tl        SUU
UUU4S jjm	UU	U
UUUU4S	 jmT" S5        T Vs/ s H  n[        5       R                  " U6 PM     snmUU UU4S
 jmT=[        T '   =[        T'   [        T'   T$ s  snf )a  
Invokes a callback every time a hotkey is pressed. The hotkey must
be in the format `ctrl+shift+a, s`. This would trigger when the user holds
ctrl, shift and "a" at once, releases, and then presses "s". To represent
literal commas, pluses, and spaces, use their names ('comma', 'plus',
'space').

- `args` is an optional list of arguments to passed to the callback during
each invocation.
- `suppress` defines if successful triggers should block the keys from being
sent to other programs.
- `timeout` is the amount of seconds allowed to pass between key presses.
- `trigger_on_release` if true, the callback is invoked on key release instead
of key press.

The event handler function is returned. To remove a hotkey call
`remove_hotkey(hotkey)` or `remove_hotkey(handler)`.
before the hotkey state is reset.

Note: hotkeys are activated when the last key is *pressed*, not released.
Note: the callback is executed in a separate thread, asynchronously. For an
example of how to use a callback synchronously, see `wait`.

Examples:

    # Different but equivalent ways to listen for a spacebar key press.
    add_hotkey(' ', print, args=['space was pressed'])
    add_hotkey('space', print, args=['space was pressed'])
    add_hotkey('Space', print, args=['space was pressed'])
    # Here 57 represents the keyboard code for spacebar; so you will be
    # pressing 'spacebar', not '57' to activate the print function.
    add_hotkey(57, print, args=['space was pressed'])

    add_hotkey('ctrl+q', quit)
    add_hotkey('ctrl+alt+enter, space', some_callback)
c                    > U " T6 $ r   r   )rl   r   s    r   r   add_hotkey.<locals>.<lambda>}  s	    Xt_r   r-   c                    > T[         :H  =(       a-    U R                  [        :H  =(       a    U R                  [        ;   =(       d    TU R                  :H  =(       a    T" 5       $ r   )r3   rt   r4   rf   rz   )r   rl   rt   s    r   r   r#    sn    Z83y8NySTS^S^bySy  j  @J  NO  NZ  NZ  @Z  @i  _g  _i  jr   r   c                  >   > T" 5         [         T	 [         T	 [         T 	 g r   _hotkeys)rl   rP   r   remove_steps   r   r   add_hotkey.<locals>.remove_  s     M !"r   Nz-infc                   > U R                   T:X  a.  TR                  (       a  U R                  TTR                     ;  d4  T(       a&  [        R                  " 5       TR
                  -
  T:  d  U(       a|  TR                  5         TR                   HC  n U R                   [        :X  a  [        U R                  5        M.  [        U R                  5        ME     TR                  S S 2	 SnT" S5        g)Nr   T)rt   indexrf   r   r   last_updateremove_last_stepsuppressed_eventsr3   ry   r   )rj   
force_failr+  allowed_keys_by_steprt   	set_indexstatetimeouts      r   catch_misses add_hotkey.<locals>.catch_misses  s      J.KKOO+?+LLOO%(9(99WD""$00##x/%//*EOO,	 1
 ''*EaLr   c                   >^ U Tl         U S:X  a	  S Tl        O%U S:X  a  TR                  5         [        TSS9Tl        U [        T5      S-
  :X  a&  UUUUUU4S jn[	        UTTR                      T	5      mO2TR                   S-   4UUU4S jjn[	        UTTR                      T	5      mTTl        [        R                  " 5       Tl        g)	Nr   c                      g r   r   r   r   r   r   /add_hotkey.<locals>.set_index.<locals>.<lambda>  s    r   r-   Tr   c                    > U R                   [        :X  a  T" 5         T" S5        U R                   T:H  =(       a    T" 5       nU(       a  T" U SS9$ U /TR                  S S & g)Nr   T)r/  F)rt   r4   r.  )rj   r}   rl   r4  rt   r   r1  r2  s     r   r  .add_hotkey.<locals>.set_index.<locals>.handler  sY    ##v-HaL))Z7FHJ'$??27E++A. r   c                    > U R                   [        :X  a  T" 5         T" U5        TR                  R                  U 5        grS   )rt   r4   r.  r   )rj   	new_indexr   r1  r2  s     r   r  r:    s5    ##v-Hi(''..u5r   F)	r+  remove_catch_missesrq   r   r   r-  r   r   r,  )
r<  r  r   rl   r4  rt   r1  r2  r   r   s
     @r   r1  add_hotkey.<locals>.set_index  s    > )5E%!^%%'(,\D(IE%E
Q&	! 	! &guU[[/A8LF */Q   &guU[[/A8LF!'!OO-r   c                  p   > TR                  5         TR                  5         [        T	 [        T	 [        T 	 g r   )r=  r-  r'  )rl   rP   r   r2  s   r   r   r)    s3    !!# VWXr   F)r   r   r  r4   r3   r   r   r'  r   r=  r-  r.  floatr,  rV   union)rP   rl   r   r   r3  trigger_on_releaser  r   r0  r4  rt   r   r(  r1  r2  r   s   `````   @@@@@@@@r   
add_hotkeyrD  W  s1   J #+<  "%f-E-8J
5zQ
 j&wa(C	# 	# ELKK8G,x/AHE $E!E EfE .# #H aL D 	T
  AHGHVGx(8H+=Ns   /!D9c                      [         U    " 5         g)z]
Removes a previously hooked hotkey. Must be called with the value returned
by `add_hotkey`.
Nr&  )hotkey_or_callbacks    r   remove_hotkeyrG    s    
  "r   c                  |    [         R                  R                  5         [         R                  R                  5         g)zh
Removes all keyboard hotkeys in use, including abbreviations, word listeners,
`record`ers and `wait`s.
N)r   r]   r   r^   r   r   r   r  r    s(     $$&!!'')r   c                 $   ^ U4S jn[        XX#S9$ )zt
Whenever the hotkey `src` is pressed, suppress it and send
`dst` instead.

Example:

    remap('alt+w', 'ctrl+up')
c                     > [        S [        R                  R                  5        5       5      n U  H  n[	        U5        M     [        T5        [        U 5       H  n[        U5        M     g)Nc              3   :   #    U  H  u  pUS :X  d  M  Uv   M     g7f)rN   Nr   )r>   rL   r2  s      r   r@   0remap_hotkey.<locals>.handler.<locals>.<genexpr>  s     !zBcxglpygy((Bcs   	F)rh   r   rb   itemsr   r   r   ry   )rW   rL   r  s     r   r  remap_hotkey.<locals>.handler  sV    !!z)B[B[BaBaBc!zz(HH )S	 !12H(O 3r   r   rC  )rD  )r  r  r   rC  r  s    `   r   remap_hotkeyrP    s     cX]]r   c                      [            [        [        5      n SSS5        W  H  n[        R                  " U5        M     U $ ! , (       d  f       N/= f)z
Builds a list of all currently pressed scan codes, releases them and returns
the list. Pairs well with `restore_state` and `restore_modifiers`.
N)rg   rh   ri   rT   r   )r2  rf   s     r   stash_staterR    s<     
' 
	Y' L	 
	s   A  
Ac                 *   S[         l        [           [        [        5      nSSS5        [        U 5      nWU-
   H  n[
        R                  " U5        M     X!-
   H  n[
        R                  " U5        M     S[         l        g! , (       d  f       Nh= f)z
Given a list of scan_codes ensures these keys, and only these keys, are
pressed. Pairs well with `stash_state`, alternative to `restore_modifiers`.
TNF)r   ra   rg   rV   ri   rT   r   ry   )rF   currentr   rf   s       r   restore_staterU    sr    
 "I	o& 
_Fv%	Y' &%	9% & #I 
	s   B
Bc                 (    [        S U  5       5        g)z8
Like `restore_state`, but only restores modifier keys.
c              3   J   #    U  H  n[        U5      (       d  M  Uv   M     g 7fr   )rG   )r>   rf   s     r   r@   $restore_modifiers.<locals>.<genexpr>1  s     SjK	<R99js   #	#N)rU  )rF   s    r   restore_modifiersrY  -  s     SjSTr   c                    Uc  [         R                  " 5       S:H  n[        5       nU(       aQ  U  HJ  nUS;   a  [        U5        O[        R
                  " U5        U(       d  M4  [        R                  " U5        ML     OU  H  n [        R                  " [        U5      5      n[        [        U5      5      u  pxU H  n	[        U	5        M     [        R                  " U5        [        R                  " U5        U H  n	[        U	5        M     U(       d  M  [        R                  " U5        M     U(       a  [!        U5        gg! [        [        4 a    [        R
                  " U5         M  f = f)a  
Sends artificial keyboard events to the OS, simulating the typing of a given
text. Characters not available on the keyboard are typed as explicit unicode
characters using OS-specific functionality, such as alt+codepoint.

To ensure text integrity, all currently pressed keys are released before
the text is typed, and modifiers are restored afterwards.

- `delay` is the number of seconds to wait between keypresses, defaults to
no delay.
- `restore_state_after` can be used to restore the state of pressed keys
after the text is typed, i.e. presses the keys that were released at the
beginning. Defaults to True.
- `exact` forces typing all characters as explicit unicode (e.g.
alt+codepoint or special events). If None, uses platform-specific suggested
value.
Nr,   z
)	_platformsystemrR  r   rT   type_unicoder   r   r   r9   nextiterr   r   ry   r   rY  )
textr   restore_state_afterexactr2  letterentriesrf   	modifiersrL   s
             r   writerf  3  s%   $ }  "i/ME FV))&1uekk%(  F&//v0FG'+DM':$	
 &h & y)  +%! & uE"% ( %  ! j) ))&1s   5E&E.-E.c                    ^ U (       a5  [        5       m[        U U4S jXS9nTR                  5         [        U5        g [        R
                  " S5        M  )zl
Blocks the program execution until the given hotkey is pressed or,
if given no parameters, blocks forever.
c                  $   > T R                  5       $ r   )rV   )locks   r   r   wait.<locals>.<lambda>q  s    DHHJr   rO  g    .AN)r   rD  r(   rG  r   r   )rP   r   rC  r   ri  s       @r   r(   r(   j  sC    
 xF$6q		fKK r   c                   ^ U cQ  [         R                  5         [           [        R	                  5        Vs/ s H  oR
                  PM     n nSSS5        OU  Vs/ s H  n[        U5      PM     n n[        S U  5       5      n/ SQmU4S jnSR                  [        X4S95      $ s  snf ! , (       d  f       NG= fs  snf )a  
Returns a string representation of hotkey from the given key names, or
the currently pressed keys if not given.  This function:

- normalizes names;
- removes "left" and "right" prefixes;
- replaces the "+" key name with "plus" to avoid ambiguity;
- puts modifier keys first, in a standardized order;
- sort remaining keys;
- finally, joins everything with "+".

Example:

    get_hotkey_name(['+', 'left ctrl', 'shift'])
    # "ctrl+shift+plus"
Nc              3      #    U  H6  oR                  S S5      R                  SS5      R                  SS5      v   M8     g7f)r    r   +plusN)replace)r>   r   s     r   r@   "get_hotkey_name.<locals>.<genexpr>  s:     gaf\]ii,44XrBJJ3PVWWafs   >A )ctrlaltshiftwindowsc                 L   > U T;   a  TR                  U 5      OS[        U 5      4$ )N   )r+  r   )r   re  s    r   r   !get_hotkey_name.<locals>.<lambda>  s    1	>Y__Q/q#a&Qr   rn  r  )
r   r   rg   ri   valuesr?   r9   rV   joinrh   )namesr   r?   clean_namessorting_keyre  s        @r   get_hotkey_namer~  x  s    " }$$&!%4%;%;%=>%=VV%=E> "! 388%$%%8gafggK 4IQK88F;899 ? "! 9s"   B*B%B*B;%B**
B8c                     [         R                  " SS9n[        UR                  U S9n UR	                  5       n[        U5        U$ )zA
Blocks until a keyboard event happens, then returns that event.
r-   )maxsizer   )_queueQueuerq   r|   rw   r   )r   r{   hookedrj   s       r   
read_eventr    s<     LL#E%))h/F
		vr   c                 V    [        U 5      nUR                  =(       d    UR                  $ )zf
Blocks until a keyboard event happens, then returns that event's name or,
if missing, its scan code.
)r  r?   rf   )r   rj   s     r   read_keyr    s!    
 x E::((r   c                   ^ [         R                  " 5       mU4S jn[        XS9n TR                  5       nUR                  [
        :X  aa  [        U5        [           [        R                  5        Vs/ s H  oDR                  PM     snUR                  /-   nSSS5        [        W5      $ M  s  snf ! , (       d  f       N = f)z
Similar to `read_key()`, but blocks until the user presses and releases a
hotkey (or single key), then returns a string representing the hotkey
pressed.

Example:

    read_hotkey()
    # "ctrl+shift+p"
c                 Z   > TR                  U 5      =(       d    U R                  [        :H  $ r   )r|   rt   r3   )r   r{   s    r   r   read_hotkey.<locals>.<lambda>  s    599Q<;1<<8#;;r   r   N)r  r  rq   rw   rt   r4   r   rg   ri   ry  r?   r~  )r   r   r  rj   r   r{  r{   s         @r   read_hotkeyr    s     LLNE	;B"(F
		v%6N%)8)?)?)AB)AA)ABejj\Q &"5)) 
 C &%s   B33B.B3.B33
Cc              #   $  #    [         R                  " 5       S:X  a  SOSnSnSnSnU  H  nUR                  nUR                  S:X  a  SnSUR                  ;   a  UR                  S	:H  nMB  UR                  S
:X  a  UR                  S	:X  a	  U(       + nMk  U(       a'  UR                  U:X  a  UR                  S	:X  a  USS nM  UR                  S	:X  d  M  [	        U5      S:X  a  X4-  (       a  UR                  5       nXW-   nM  Uv   SnM     Uv   g7f)a  
Given a sequence of events, tries to deduce what strings were typed.
Strings are separated when a non-textual key is pressed (such as tab or
enter). Characters are converted to uppercase according to shift and
capslock status. If `allow_backspace` is True, backspaces remove the last
character typed.

This function is a generator, so you can pass an infinite stream of events
and convert them to strings in real time.

Note this functions is merely an heuristic. Windows for example keeps per-
process keyboard state such as keyboard layout, and this information is not
available for our hooks.

    get_type_strings(record()) #-> ['This is what', 'I recorded', '']
r1   delete	backspaceFrm  space rt  downz	caps lockNr-   )r[  r\  r?   rt   r   upper)eventsallow_backspacebackspace_nameshift_pressedcapslock_pressedstringrj   r?   s           r   get_typed_stringsr    s     " "+!1!1!3x!?X[NMFzz :: Dejj !,,6MZZ;&5+;+;v+E#33~!=%BRBRV\B\CR[F'4yA~ 3::<D+ , Ls   CD=Dc                 x    U =(       d    [         R                  " 5       n U [        U R                  5      4q[        $ )z
Starts recording all keyboard events into a global variable, or the given
queue if any. Returns the queue of events and the hooked function.

Use `stop_recording()` or `unhook(hooked_function)` to stop.
)r  r  rq   r|   
_recording)recorded_events_queues    r   start_recordingr    s1     2CV\\^'.C.G.G)HIJr   c                  ~    [         (       d  [        S5      e[         u  p[        U5        [        U R                  5      $ )zQ
Stops the global recording of events and returns a list of the events
captured.
z#Must call "start_recording" before.)r  r   r   r   r{   )r  r  s     r   stop_recordingr    s7     :>??$.!
6N%++,,r   c                 >    [        5         [        XUS9  [        5       $ )a,  
Records all keyboard events from all keyboards until the user presses the
given hotkey. Then returns the list of events recorded, of type
`keyboard.KeyboardEvent`. Pairs well with
`play(events)`.

Note: this is a blocking function.
Note: for more details on the keyboard hook and events see `hook`.
rO  )r  r(   r  )untilr   rC  s      r   recordr    s     6HIr   c                 P   [        5       nSnU  H  nUS:  a)  Ub&  [        R                  " UR                  U-
  U-  5        UR                  nUR                  =(       d    UR
                  nUR                  [        :X  a  [        U5        M~  [        U5        M     [        U5        g)a"  
Plays a sequence of recorded events, maintaining the relative time
intervals. If speed_factor is <= 0 then the actions are replayed as fast
as the OS allows. Pairs well with `record()`.

Note: the current keyboard state is cleared at the beginning and restored at
the end of the function.
Nr   )rR  r   r   timerf   r?   rt   r3   ry   r   rY  )r  speed_factorr2  	last_timerj   rE   s         r   playr    s     MEI!	 5KKi/<?@JJ	oo+&&(2c
  er   r     c                    ^ ^^^^^^^^ [        5       mSTl        STl        UUUUUU 4S jm[        T5      mUUUU 4S jmT=[        T '   =[        T'   [        T'   T$ )ar  
Invokes a callback every time a sequence of characters is typed (e.g. 'pet')
and followed by a trigger key (e.g. space). Modifiers (e.g. alt, ctrl,
shift) are ignored.

- `word` the typed text to be matched. E.g. 'pet'.
- `callback` is an argument-less function to be invoked each time the word
is typed.
- `triggers` is the list of keys that will cause a match to be checked. If
the user presses some key that is not a character (len>1) and not in
triggers, the characters so far will be discarded. By default the trigger
is only `space`.
- `match_suffix` defines if endings of words should also be checked instead
of only whole words. E.g. if true, typing 'carpet'+space will trigger the
listener for 'pet'. Defaults to false, only whole words are checked.
- `timeout` is the maximum number of seconds between typed characters before
the current word is discarded. Defaults to 2 seconds.

Returns the event handler created. To remove a word listener use
`remove_word_listener(word)` or `remove_word_listener(handler)`.

Note: all actions are performed on key down. Key up events are ignored.
Note: word matches are **case sensitive**.
rm  r  c                   > U R                   nU R                  [        :X  d
  U[        ;   a  g T(       a$  U R                  TR                  -
  T:  a  STl        U R                  Tl        TR
                  T:H  =(       d$    T=(       a    TR
                  R                  T5      nUT;   a  U(       a  T" 5         STl        g [        U5      S:  a  STl        g T=R
                  U-  sl        g )Nrm  r-   )r?   rt   r4   r7   r  rT  endswithr   )	rj   r?   matchedrl   match_suffixr2  r3  triggerswords	      r   r  "add_word_listener.<locals>.handlerJ  s    zzv%)>uzzEJJ.8EMZZ
--4'ZL,YU]]=S=STX=Y8JEMY]EMMMT!Mr   c                  >   > T" 5         [         T	 [         T 	 [         T	 g r   _word_listeners)r  r  r   r  s   r   r   !add_word_listener.<locals>.remove\  s     D!G$F#r   )r   rT  r  rq   r  )	r  rl   r  r  r3  r  r  r   r2  s	   `````@@@@r   add_word_listenerr  -  sa    2 HEEMEJ" "" ']F$ $
 RXWODWOG4v7NMr   c                      [         U    " 5         g)z
Removes a previously registered word listener. Accepts either the word used
during registration (exact string) or the event handler returned by the
`add_word_listener` or `add_abbreviation` functions.
Nr  )word_or_handlers    r   remove_word_listenerr  e  s     O$&r   c                 L   ^ S[        U 5      S-   -  U-   mU4S jn[        XX#S9$ )uy  
Registers a hotkey that replaces one typed text with another. For example

    add_abbreviation('tm', u'™')

Replaces every "tm" followed by a space with a ™ symbol (and no space). The
replacement is done by sending backspace events.

- `match_suffix` defines if endings of words should also be checked instead
of only whole words. E.g. if true, typing 'carpet'+space will trigger the
listener for 'pet'. Defaults to false, only whole words are checked.
- `timeout` is the maximum number of seconds between typed characters before
the current word is discarded. Defaults to 2 seconds.

For more details see `add_word_listener`.
r-   c                     > [        T 5      $ r   )rf  )replacements   r   r   "add_abbreviation.<locals>.<lambda>  s
    u[)r   )r  r3  )r   r  )source_textreplacement_textr  r3  rl   r  s        @r   add_abbreviationr  m  s1    " K(*+.>>K)H[__r   )T)TT)r   gMbP?r@  )r   Fr-   F)TF)r   TN)NFFr   )escapeFF)g      ?)Fr  ){__doc__
__future__r   _print_functionversionrer   	itertoolsr  collectionsrY   	threadingr   r   r   _Lockr  r   getattrr   r   r
   rB   r   r  r  r   r'   	NameErrorr{   r   r   objectr   platformr[  r\  rm  r.   rT   r0   r2   OSErrorr   _keyboard_eventr3   r4   r5   _genericr6   _GenericListener_canonical_namesr7   r8   r9   rV   rC   rG   rg   ri   _physically_pressed_keysrz   rI   r   r=   r   r   press_and_releasery   r   r   r   r   rq   r   r   r   r   r   r   
unhook_keyr  r  unblock_keyr  unremap_keyr  r   r'  rD  register_hotkeyrG  unregister_hotkeyclear_hotkeyr  unregister_all_hotkeysremove_all_hotkeysclear_all_hotkeysrP  unremap_hotkeyrR  rU  rY  rf  r(   r~  r  r  r  r  r  r  r  r  r  replayr  r  r  r  register_word_listenerregister_abbreviationremove_abbreviationr   r   r   <module>r     s  M\ 9
   " 6 %d3Auzz9*1G5J9 2 V 
"  "-7"-8#0
-44Y5E5E5GH
II < < 9 L Lu 
+ w *  O2( O2b 	<B#>  6 
!\ >TV0]_ 

9
 1 M . O` # $1 0 L* CU T  T+.?^$ 
#"U5!n:>	)*,,\ 


-* 
07yuVW 6p'`, + ( * C!  9*G-J889s   H H"!H"