
    iF                     x   d Z ddlZddlZddlZddlZddlmZmZ ddlmZm	Z
  eed      s! ej                  ej                        e_         G d dej                        Z G d	 d
ej                        Z G d dej                        Z G d dej&                        Z G d dej                        Z ej                  e      Zej.                  j0                  Zej4                  fe_        ej.                  j8                  Zej<                  ej<                  fe_        de_        ej.                  j@                  Z ej<                  ejB                  ejD                  fe _        ejF                  jH                  Z$ej                  e$_%         G d de&      Z' G d de&      Z( G d de&      Z) G d de&      Z*y)z8
Utility functions and classes for the *win32* backend.
    N)windllwintypes   )AbstractListener	win32_vksLPDWORDc                       e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdej&                  fdej&                  fdej(                  fdej(                  fdej(                  fdej,                  fgZy)
MOUSEINPUTz8Contains information about a simulated mouse event.
    r                   @         i   i      dxdy	mouseDatadwFlagstimedwExtraInfoN)__name__
__module____qualname____doc__MOVELEFTDOWNLEFTUP	RIGHTDOWNRIGHTUP
MIDDLEDOWNMIDDLEUPXDOWNXUPWHEELHWHEELABSOLUTEXBUTTON1XBUTTON2r   LONGDWORDctypesc_void_p_fields_     \/home/obispo/Crisostomo_bridge/mision_env/lib/python3.12/site-packages/pynput/_util/win32.pyr
   r
   ,   s    DHFIGJHE
CEFHHH 
x}}	x}}	hnn%	HNN#	 	(*Hr2   r
   c                       e Zd ZdZdZdZdZdZdej                  fdej                  fdej                  fd	ej                  fd
ej                  fgZy)
KEYBDINPUTz;Contains information about a simulated keyboard event.
    r   r   r   r   wVkwScanr   r   r   N)r   r   r   r   EXTENDEDKEYKEYUPSCANCODEUNICODEr   WORDr-   r.   r/   r0   r1   r2   r3   r5   r5   H   s`    KEHG 
	(-- 	HNN#	 	(*Hr2   r5   c                   b    e Zd ZdZdej
                  fdej                  fdej                  fgZy)HARDWAREINPUTztContains information about a simulated message generated by an input
    device other than a keyboard or mouse.
    uMsgwParamLwParamHN)r   r   r   r   r   r-   r<   r0   r1   r2   r3   r>   r>   X   s4     
 	HMM"	HMM"$Hr2   r>   c                   &    e Zd ZdZdefdefdefgZy)INPUT_unionz;Represents the union of input types in :class:`INPUT`.
    mikihiN)r   r   r   r   r
   r5   r>   r0   r1   r2   r3   rC   rC   b   s(     
z	z	}Hr2   rC   c                   @    e Zd ZdZdZdZdZdej                  fde	fgZ
y)INPUTzUsed by :attr:`SendInput` to store information for synthesizing input
    events such as keystrokes, mouse movement, and mouse clicks.
    r   r   r   typevalueN)r   r   r   r   MOUSEKEYBOARDHARDWAREr   r-   rC   r0   r1   r2   r3   rH   rH   k   s5     EHH 
 	+ Hr2   rH   c                   :   e Zd ZdZdZ ej                  ej                        Z	e
j                  j                  Zej                  ej                  ej                   ej                   fe_        e
j                  j$                  Zej                  ej                  ej                   ej                   ej                   fe_        e
j                  j(                  Zej,                  ej                   ej.                  ej0                  fe_        dZd Zd Zd Zd Zd Zy	)
MessageLoopz)A class representing a message loop.
    i  r   c                 R    d | _         t        j                         | _        d | _        y N)	_threadid	threadingEvent_eventthreadselfs    r3   __init__zMessageLoop.__init__   s    oo'r2   c              #   2  K   | j                   J 	 	 t        j                         }t        j                  |      }| j                  |ddd      }|dk  s|j                  | j                  k(  rn| a	 d| _         d| _        y# d| _         d| _        w xY ww)zInitialises the message loop and yields all messages until
        :meth:`stop` is called.

        :raises AssertionError: if :meth:`start` has not been called
        Nr   )	rR   r   MSGr.   byref_GetMessagemessageWM_STOPrV   )rX   msglpmsgrs       r3   __iter__zMessageLoop.__iter__   s      ~~)))	llnS)$$UD!Q76S[[DLL8I 
 
 "DNDK "DNDKs   BA"B 5BBBc                    t               | _        t        j                         | _        t        j                         }t        j                  |      }| j                  |ddd| j                         | j                  j                          y)zStarts the message loop.

        This method must be called before iterating over messages, and it must
        be called from the same thread.
        Ni   )GetCurrentThreadIdrR   rS   current_threadrV   r   r[   r.   r\   _PeekMessagePM_NOREMOVErU   set)rX   r`   ra   s      r3   startzMessageLoop.start   se     ,-..0 llnS!%vvt7G7GH 	r2   c                     | j                   j                          | j                  r| j                  | j                  dd       yy)z Stops the message loop.
        r   N)rU   waitrR   postr_   rW   s    r3   stopzMessageLoop.stop   s4     	>>IIdllAq) r2   c                 @    | j                  | j                  |||       y)zPosts a message to this message loop.

        :param ctypes.wintypes.UINT msg: The message.

        :param ctypes.wintypes.WPARAM wparam: The value of ``wParam``.

        :param ctypes.wintypes.LPARAM lparam: The value of ``lParam``.
        N)_PostThreadMessagerR   )rX   r`   wparamlparams       r3   rm   zMessageLoop.post   s     	VVDr2   N)r   r   r   r   r_   r.   POINTERr   r[   _LPMSGr   user32GetMessageWr]   c_voidpHWNDUINTargtypesPeekMessageWrg   PostThreadMessageWrp   r-   WPARAMLPARAMrh   rY   rc   rj   rn   rm   r1   r2   r3   rO   rO      s     GV^^HLL)F--++K	K
 ==--LL  99	# K
."*	Er2   rO   c                   :   e Zd ZdZdZ ej                  ej                  ej                  ej                  ej                        Zej                  j                  Zej                   eej"                  ej$                  fe_        ej                  j(                  Zej,                  fe_        ej                  j.                  Zej,                  ej                   ej                  ej                  fe_        i Z G d de      Zd fdZd Zd Zeed	               Z y
)
SystemHookz%A class to handle Windows hooks.
    r   c                       e Zd ZdZy)SystemHook.SuppressExceptionzbAn exception raised by a hook callback to suppress further
        propagation of events.
        N)r   r   r   r   r1   r2   r3   SuppressExceptionr   	  s    	 	r2   r   c                      y rQ   r1   )coder`   lpdatas      r3   <lambda>zSystemHook.<lambda>  s    r2   c                 .    || _         || _        d | _        y rQ   )hook_idon_hook_hook)rX   r   r   s      r3   rY   zSystemHook.__init__  s    
r2   c                     t        j                         j                  }|| j                  vsJ | | j                  |<   | j	                  | j
                  | j                  d d      | _        | S )Nr   )rS   rf   ident_HOOKS_SetWindowsHookExr   _handlerr   )rX   keys     r3   	__enter__zSystemHook.__enter__  se    &&(..$++%%%  C++LLMM	
 r2   c                     t        j                         j                  }|| j                  v sJ | j                  )| j                  | j                         | j                  |= y y rQ   )rS   rf   r   r   r   _UnhookWindowsHookEx)rX   exc_typerJ   	tracebackr   s        r3   __exit__zSystemHook.__exit__"  sU    &&(..dkk!!!::!%%djj1C  "r2   c                 
   t        j                         j                  }t        j                  j                  |d       }|r,	 |j                  | ||       t        j                  d| ||      S y # |j                  $ r Y y Y -xY w)Nr   r   )	rS   rf   r   r   r   getr   r   _CallNextHookEx)r   r`   r   r   rX   s        r3   r   zSystemHook._handler+  s     &&(..  $$S$/T3/ --asFCC  )) s   A. .B?BN)!r   r   r   r   	HC_ACTIONr.   WINFUNCTYPEr   r~   c_int32r}   	_HOOKPROCr   ru   SetWindowsHookExWr   c_int	HINSTANCEr-   rz   UnhookWindowsHookExr   HHOOKCallNextHookExr   r   	Exceptionr   rY   r   r   staticmethodr   r1   r2   r3   r   r      s    I""":I 77	"
 "==<<%!mm22O	 O FI  )G 
! D  Dr2   r   c                   j    e Zd ZdZdZdZg Zd Zd Zd Z	e
j                  d        Zd Zd	 Zd
 Zd Zy)ListenerMixina0  A mixin for *win32* event listeners.

    Subclasses should set a value for :attr:`_EVENTS` and implement
    :meth:`_handle_message`.

    Subclasses must also be decorated with a decorator compatible with
    :meth:`pynput._util.NotifierMixin._receiver` or implement the method
    ``_receive()``.
    Ni  c                 *    t         j                         )zCauses the currently filtered event to be suppressed.

        This has a system wide effect and will generally result in no
        applications receiving the event.

        This method will raise an undefined exception.
        )r   r   rW   s    r3   suppress_eventzListenerMixin.suppress_eventQ  s     **,,r2   c                    t               | _        | j                         5  | j                          | j                  j	                          	 t        | j                  | j                        5  | j                  D ]  }| j                  s n|j                  | j                  k(  r'| j                  |j                  |j                         Q|j                  | j                  v sj| j                  |j                  |j                  |j                          d d d        d d d        y # 1 sw Y   xY w#  Y xY w# 1 sw Y   y xY wrQ   )rO   _message_loop_receive_mark_readyrj   r   _EVENTSr   runningr^   _WM_PROCESS_processwParamlParam_WM_NOTIFICATIONS_on_notification)rX   r`   s     r3   _runzListenerMixin._run[  s   (]]]_ 	$$&dmm< 	E#11 E#||!;;$*:*:: MM#**cjjA [[D,B,BB 11 #SZZEE	E	 		E 	E%	 	s<   +D: D3,A6D'#3D'D3'D0	,D33D75D::Ec                 X    	 | j                   j                          y # t        $ r Y y w xY wrQ   )r   rn   AttributeErrorrW   s    r3   _stop_platformzListenerMixin._stop_platformr  s+    	##% 		s    	))c                     	 | j                  |||      }|' | j                  j                  | j                  g|  | j                  r| j                          yy# t        $ r | j                  |||       Y <w xY w)a  The callback registered with *Windows* for events.

        This method will post the message :attr:`_WM_PROCESS` to the message
        loop started with this listener using :meth:`MessageLoop.post`. The
        parameters are retrieved with a call to :meth:`_handle`.
        N)_convertr   rm   r   NotImplementedError_handle_messagesuppressr   )rX   r   r`   r   	converteds        r3   r   zListenerMixin._handlery  s}    	4dC8I$'""''(8(8E9E ==!  # 	4  sF3	4s   <A A;:A;c                     t               )zThe device specific callback handler.

        This method converts a low-level message and data to a
        ``WPARAM`` / ``LPARAM`` pair.
        r   rX   r   r`   r   s       r3   r   zListenerMixin._convert       "##r2   c                     t               )znThe device specific callback handler.

        This method performs the actual dispatching of events.
        r   )rX   rq   rr   s      r3   r   zListenerMixin._process  s    
 "##r2   c                     t               )zThe device specific callback handler.

        This method calls the appropriate callback registered when this
        listener was created based on the event.

        This method is only called if :meth:`_convert` is not implemented.
        r   r   s       r3   r   zListenerMixin._handle_message  s     "##r2   c                     t               )zAn additional notification handler.

        This method will be called for every message in
        :attr:`_WM_NOTIFICATIONS`.
        r   )rX   r   rq   rr   s       r3   r   zListenerMixin._on_notification  r   r2   )r   r   r   r   r   r   r   r   r   r   r   _emitterr   r   r   r   r   r1   r2   r3   r   r   >  sZ     G K -. " ""$$$$r2   r   c                   x   e Zd ZdZej
                  j                  j                  Zej                  fe_
        ej
                  j                  j                  Zej                  fe_
        ej
                  j                  j                  Zej"                  fe_
        ej
                  j                  j                  Zej                  fe_
        ej
                  j                  j&                  Zej*                  ej*                  ej,                  fe_
        ej
                  j                  j.                  Zej*                  ej*                  ej"                  ej"                  ej                  ej*                  ej,                  fe_
        dZdZdZd Zd Zd Zd Zd	 Z d
 Z!d Z"d Z#e$jJ                  d        Z&y)KeyTranslatorz:A class to translate virtual key codes to characters.
    r   r   r   c                 $    | j                          y rQ   )update_layoutrW   s    r3   rY   zKeyTranslator.__init__  s    r2   c                     | j                   | j                            }| j                  || j                        }||   \  }}||||dS )a@  Converts a virtual key code to a string.

        :param int vk: The virtual key code.

        :param bool is_press: Whether this is a press.

        :return: parameters suitable for the :class:`pynput.keyboard.KeyCode`
            constructor

        :raises OSError: if a call to any *win32* function fails
        )charis_deadvk_scan)_layout_data_modifier_state_to_scan_layout)rX   r   is_presslayout_datascan	characterr   s          r3   __call__zKeyTranslator.__call__  sX     ''(<(<(>?}}R.(.	7 	 	r2   c                 >    | j                         \  | _        | _        y)z(Updates the cached layout data.
        N)_generate_layoutr   r   rW   s    r3   r   zKeyTranslator.update_layout  s     +/*?*?*A'd'r2   c                 ,    | j                   d   |   d   S )zTranslates a scan code to a character, if possible.

        :param int scan: The scan code to translate.

        :return: maybe a character
        :rtype: str or None
        )FFFr   )r   )rX   r   s     r3   char_from_scanzKeyTranslator.char_from_scan  s        !67=a@@r2   c                    i }t        j                  dz         }| j                         5 }| j                  |      }ddd       t	        t        |            D cg c]  }| j                  |       }}t        j                  ddd      D ]=  \  }}}	dgt        |      z  }
|
||||	f<   |rdnd|t        j                  <   |rdnd|t        j                  <   |	rdnd|t        j                  <   t        j                  j                  dz         }t        |      D ]  \  }}| j!                  ||t        j"                  |      t        j"                  |      t        |      d      }|dk7  sR|d   }|dk  }||f|
|<   |sf| j!                  ||t        j"                  |      t        j"                  |      t        |      d|        @ |fS # 1 sw Y   xY wc c}w )a
  Generates the keyboard layout.

        This method will call ``ToUnicodeEx``, which modifies kernel buffers,
        so it must *not* be called from the keyboard hook.

        The return value is the tuple ``(layout_handle, layout_data)``, where
        ``layout_data`` is a mapping from the tuple ``(shift, ctrl, alt)`` to
        an array indexed by scan code containing the data
        ``(character, is_dead)``, and ``layout_handle`` is the handle of the
        layout.

        :return: a composite layout
           N)FT)NFr   r      )r.   c_ubyte_thread_input_GetKeyboardLayoutrangelen_to_vk	itertoolsproductVKSHIFTCONTROLMENUr   WCHAR	enumerate_ToUnicodeExr\   )rX   r   stateactive_threadlayoutr   vksshiftctrlaltcurrentoutr   countr   r   s                   r3   r   zKeyTranslator._generate_layout  s    #%(! 	<],,];F	< c%j)+ KKf%+ + !* 1 1}m!= 	DE4$oE
2G.5Kc*+ ',dE"((O(,$E"**%(TdE"''N ??((1,/C'n D
r))fll516<<3DHa)
 A: #AI#aiG%.$8GDM ))fll5&9"LL-s3xFDD	D< $$I	< 	<+s   GGGc                 <    | j                  || j                  |      S )zRetrieves the scan code for a virtual key code.

        :param int vk: The virtual key code.

        :param layout: The keyboard layout.

        :return: the scan code
        )_MapVirtualKeyEx_MAPVK_VK_TO_VSC)rX   r   r   s      r3   r   zKeyTranslator._to_scan/  s$     $$%%v/ 	/r2   c                 <    | j                  || j                  |      S )zRetrieves the virtual key code for a scan code.

        :param int vscan: The scan code.

        :param layout: The keyboard layout.

        :return: the virtual key code
        )r   _MAPVK_VSC_TO_VK)rX   r   r   s      r3   r   zKeyTranslator._to_vk;  s$     $$$''1 	1r2   c                    t        | j                  t        j                        dz        }t        | j                  t        j                        dz        }t        | j                  t        j
                        dz        }|||fS )zReturns a key into :attr:`_layout_data` for the current modifier
        state.

        :return: the current modifier state
        r   )bool_GetAsyncKeyStater   r   r   r   )rX   r   r   r   s       r3   r   zKeyTranslator._modifier_stateG  sk     T++BHH5>?D**2::6?@4))"''2V;<tS!!r2   c              #   $   K   t                yw)z&Yields the current thread ID.
        N)re   rW   s    r3   r   zKeyTranslator._thread_inputR  s      !""s   N)'r   r   r   r   r.   r   ru   GetAsyncKeyStater  r   rz   GetKeyboardLayoutr   r   r-   GetKeyboardState_GetKeyboardStaterw   _GetKeyStateMapVirtualKeyExWr   ry   HKLToUnicodeExr   r   r   _MAPVK_VK_TO_CHARrY   r   r   r   r   r   r   r   
contextlibcontextmanagerr   r1   r2   r3   r   r     s   ,,=="--??#,,=="==''88LL}}++<<! ==''33LL .B
A5%n
/
1	" # #r2   r   )+r   r  r.   r   rS   r   r    r   r   r   hasattrrs   r-   r   	Structurer
   r5   r>   UnionrC   rH   LPINPUTru   
VkKeyScanW	VkKeyScanr   rz   MapVirtualKeyWMapVirtualKeyry   MAPVK_VK_TO_VSC	SendInputrw   r   kernel32re   restypeobjectrO   r   r   r   r1   r2   r3   <module>r     s  "     0 x#%v~~hnn5H*!! *8*!! * $F$$ $&,, 
 F 
  &..
MM$$	NN	  ,,MMMM  !" MM##	MM
NN
LL	 
 __77 %^^  [E& [E|OD ODdl$F l$^i#F i#r2   