
    nc                     j    d dl mZmZ d dlZd dlZ	 d dlmZ  G d de      Z	y# e$ r	 d dlmZ Y w xY w)    )ThreadLockN)Queuec                   D    e Zd Z e       Zd Zd Zd Zd Zd Z	d Z
d Zy)	GenericListenerc                 >    g | _         d| _        t               | _        y )NF)handlers	listeningr   queueselfs    'E:\Python Projects\keyboard\_generic.py__init__zGenericListener.__init__   s    W
    c                     | j                   D ]  }	  ||      r y y # t        $ r}t        j                          Y d }~3d }~ww xY w)N   )r	   	Exception	traceback	print_exc)r   eventhandleres       r   invoke_handlerszGenericListener.invoke_handlers   sE    }}G&5> " %
  &##%%&s   	AA  Ac                 
   | j                   j                          	 | j                  s| j                          d| _        t	        | j
                        | _        d| j                  _        | j                  j                          t	        | j                        | _
        d| j                  _        | j                  j                          | j                   j                          y# | j                   j                          w xY w)zC
        Starts the listening thread if it wasn't already.
        T)targetN)lockacquirer
   initr   listenlistening_threaddaemonstartprocessprocessing_threadreleaser   s    r   start_if_necessaryz"GenericListener.start_if_necessary   s     				 >>		!%(.dkk(B%/3%%,%%++-)/t||)D&04&&-&&,,.IIDIIs   B/C& &Dc                     t        d      )Nz5This method should be implemented in the child class.)NotImplementedErrorr   r   s     r   pre_process_eventz!GenericListener.pre_process_event0   s    !"YZZr   c                     | j                   J 	 | j                   j                         }| j                  |      r| j                  |       | j                   j	                          W)zX
        Loops over the underlying queue of events and processes them in order.
        )r   getr*   r   	task_doner)   s     r   r#   zGenericListener.process3   sX     zz%%%JJNN$E%%e,$$U+JJ  "	 r   c                 Z    | j                          | j                  j                  |       y)zv
        Adds a function to receive each event captured, starting the capturing
        process if necessary.
        N)r&   r	   appendr   r   s     r   add_handlerzGenericListener.add_handler>   s"    
 	!W%r   c                 v    || j                   v r+| j                   j                  |       || j                   v r*yy)z+ Removes a previously added event handler. N)r	   remover0   s     r   remove_handlerzGenericListener.remove_handlerF   s.    &MM  ) &r   N)__name__
__module____qualname__r   r   r   r   r&   r*   r#   r1   r4    r   r   r   r      s-    6D
& ([	#&*r   r   )
	threadingr   r   r   	functoolsr   r   ImportErrorobjectr   r8   r   r   <module>r=      s6    "  >*f >*  s   $ 22