
    tf2                         d Z ddlZddlmZmZmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ erddlmZ  G d	 d
e      Z G d de      Z G d de      Zy)zKeyboard and Mouse module.    N)AnyDictTYPE_CHECKING)
CDPSession)PyppeteerError)keyDefinitions)
merge_dict)Setc            	           e Zd ZdZdeddfdZddedededdfd	Z	dede
fd
ZdedefdZdeddfdZdeddfdZddedededdfdZddedededdfdZy)Keyboarda  Keyboard class provides as api for managing a virtual keyboard.

    The high level api is :meth:`type`, which takes raw characters and
    generate proper keydown, keypress/input, and keyup events on your page.

    For finer control, you can use :meth:`down`, :meth:`up`, and
    :meth:`sendCharacter` to manually fire events as if they were generated
    from a real keyboard.

    An example of holding down ``Shift`` in order to select and delete some
    text:

    .. code::

        await page.keyboard.type('Hello, World!')
        await page.keyboard.press('ArrowLeft')

        await page.keyboard.down('Shift')
        for i in ' World':
            await page.keyboard.press('ArrowLeft')
        await page.keyboard.up('Shift')

        await page.keyboard.press('Backspace')
        # Result text will end up saying 'Hello!'.

    An example of pressing ``A``:

    .. code::

        await page.keyboard.down('Shift')
        await page.keyboard.press('KeyA')
        await page.keyboard.up('Shift')
    clientreturnNc                 >    || _         d| _        t               | _        y )Nr   )_client
_modifiersset_pressedKeys)selfr   s     Y/var/www/html/software/conda/envs/higlass/lib/python3.12/site-packages/pyppeteer/input.py__init__zKeyboard.__init__5   s    &)e    keyoptionskwargsc                   K   t        ||      }| j                  |      }|d   | j                  v }| j                  j                  |d          | xj                  | j                  |d         z  c_        |j                  d      }||d   }| j                  j                  d|rdnd| j                  |d   |d   |d   ||||d	   |d	   d
k(  d
       d{    y7 w)ai  Dispatch a ``keydown`` event with ``key``.

        If ``key`` is a single character and no modifier keys besides ``Shift``
        are being held down, and a ``keypress``/``input`` event will also
        generated. The ``text`` option can be specified to force an ``input``
        event to be generated.

        If ``key`` is a modifier key, like ``Shift``, ``Meta``, or ``Alt``,
        subsequent key presses will be sent with that modifier active. To
        release the modifier key, use :meth:`up` method.

        :arg str key: Name of key to press, such as ``ArrowLeft``.
        :arg dict options: Option can have ``text`` field, and if this option
            specified, generate an input event with this text.

        .. note::
            Modifier keys DO influence :meth:`down`. Holding down ``shift``
            will type the text in upper case.
        coder   textNInput.dispatchKeyEventkeyDown
rawKeyDownkeyCodelocation   )
type	modifierswindowsVirtualKeyCoder   r   r   unmodifiedText
autoRepeatr"   isKeypad)	r	   _keyDescriptionForStringr   addr   _modifierBitgetr   send)r   r   r   r   descriptionr(   r   s          r   downzKeyboard.down:   s     * Wf-33C8 (D,=,==
k&124,,[-?@@{{6"<v&Dll 8!%I<%0%;'u%"$#J/#J/14;
  	 	s   CC CC c                 4    |dk(  ry|dk(  ry|dk(  ry|dk(  ryy	)
NAlt   Control   Meta   Shift   r    )r   r   s     r   r,   zKeyboard._modifierBitg   s.    %<)&='>r   	keyStringc                    | j                   dz  }dddddd}t        j                  |      }|st        d|       d|v r|d   |d<   |r|j                  d      r|d   |d<   d|v r|d   |d<   |r|j                  d	      r|d	   |d<   d
|v r|d
   |d
<   d|v r|d   |d<   t	        |d         dk(  r|d   |d<   d|v r|d   |d<   |r|j                  d      r|d   |d<   | j                   dz  rd|d<   |S )Nr9    r   )r   r!   r   r   r"   zUnknown key: r   shiftKeyr!   shiftKeyCoder   r"   r3   r   	shiftTexti)r   r   r-   r   len)r   r;   shiftr/   
definitions        r   r*   z!Keyboard._keyDescriptionForStringr   sT   !#
 *--i8
 =!<==J!+E!2KZ^^J/!+J!7K
"%/	%:K	"Z^^N3%/%?K	"Z",V"4K#&0&<K
#{5!"a'"-e"4KZ",V"4KZ^^K0",["9K??R"$Kr   c           
      b  K   | j                  |      }| xj                  | j                  |d          z  c_        |d   | j                  v r| j                  j	                  |d          | j
                  j                  dd| j                  |d   |d   |d   |d   d       d{    y7 w)	zyDispatch a ``keyup`` event of the ``key``.

        :arg str key: Name of key to release, such as ``ArrowLeft``.
        r   r   r   keyUpr!   r"   )r$   r%   r   r&   r   r"   N)r*   r   r,   r   remover   r.   )r   r   r/   s      r   upzKeyboard.up   s     
 33C8D--k%.@AAAv$"3"33$$[%89ll 8u%%0%;'#J/;
  	 	s   B%B/'B-(B/charc                 \   K   | j                   j                  dd|i       d{    y7 w)aB  Send character into the page.

        This method dispatches a ``keypress`` and ``input`` event. This does
        not send a ``keydown`` or ``keyup`` event.

        .. note::
            Modifier keys DO NOT effect :meth:`sendCharacter`. Holding down
            ``shift`` will not type the text in upper case.
        zInput.insertTextr   N)r   r.   )r   rH   s     r   sendCharacterzKeyboard.sendCharacter   s'      ll 2VTNCCCs   ",*,r   c                 $  K   t        ||      }|j                  dd      }|D ]c  }|t        v r| j                  |d|i       d{    n| j	                  |       d{    |sDt        j                  |dz         d{    e y7 D7 ,7 w)a  Type characters into a focused element.

        This method sends ``keydown``, ``keypress``/``input``, and ``keyup``
        event for each character in the ``text``.

        To press a special key, like ``Control`` or ``ArrowDown``, use
        :meth:`press` method.

        :arg str text: Text to type into a focused element.
        :arg dict options: Options can have ``delay`` (int|float) field, which
          specifies time to wait between key presses in milliseconds. Defaults
          to 0.

        .. note::
            Modifier keys DO NOT effect :meth:`type`. Holding down ``shift``
            will not type the text in upper case.
        delayr   N  )r	   r-   r   pressrJ   asynciosleep)r   r   r   r   rL   rH   s         r   r$   zKeyboard.type   s     & Wf-GQ' 	2D~%jj'7888((...mmEDL111	28.1s<   ABB
BB B'BBBBBc                    K   t        ||      }| j                  ||       d{    d|v r#t        j                  |d   dz         d{    | j	                  |       d{    y7 E7  7 	w)a  Press ``key``.

        If ``key`` is a single character and no modifier keys besides
        ``Shift`` are being held down, a ``keypress``/``input`` event will also
        generated. The ``text`` option can be specified to force an input event
        to be generated.

        :arg str key: Name of key to press, such as ``ArrowLeft``.

        This method accepts the following options:

        * ``text`` (str): If specified, generates an input event with this
          text.
        * ``delay`` (int|float): Time to wait between ``keydown`` and
          ``keyup``. Defaults to 0.

        .. note::
            Modifier keys DO effect :meth:`press`. Holding down ``Shift`` will
            type the text in upper case.
        NrL   rM   )r	   r0   rO   rP   rG   )r   r   r   r   s       r   rN   zKeyboard.press   sj     , Wf-iiW%%%g-- 04 7888ggcl 	&8s3   "A0A*&A0A,A0$A.%A0,A0.A0N)__name__
__module____qualname____doc__r   r   strdictr   r0   intr,   r   r*   rG   rJ   r$   rN   r:   r   r   r   r      s     D,z ,d ,
+c +D +3 ++Z	 	 	)# )$ )VC D &
D 
D 
D2s 2T 2C 22:s T C r   r   c                       e Zd ZdZdededdfdZddeded	ed
e	ddf
dZ
ddeded	ed
e	ddf
dZdd	ed
e	ddfdZdd	ed
e	ddfdZy)MousezMouse class.

    The :class:`Mouse` operates in main-frame CSS pixels relative to the
    top-left corner of the viewport.
    r   keyboardr   Nc                 J    || _         || _        d| _        d| _        d| _        y )Ng        none)r   	_keyboard_x_y_buttonr   r   r\   s      r   r   zMouse.__init__   s%    !r   xyr   r   c           
        K   t        ||      }| j                  }| j                  }|| _        || _        |j                  dd      }t	        d|dz         D ]  }t        || j                  |z
  ||z  z  z         }t        || j                  |z
  ||z  z  z         }| j                  j                  dd| j                  ||| j                  j                  d       d{     y7 w)zMove mouse cursor (dispatches a ``mousemove`` event).

        Options can accepts ``steps`` (int) field. If this ``steps`` option
        specified, Sends intermediate ``mousemove`` events. Defaults to 1.
        stepsr3   Input.dispatchMouseEvent
mouseMoved)r$   buttonrd   re   r%   N)r	   r`   ra   r-   rangeroundr   r.   rb   r_   r   )	r   rd   re   r   r   fromXfromYrg   is	            r   movez
Mouse.move  s      Wf-GQ'q%!)$ 		Aetww1u9==>Aetww1u9==>A,,##$>$,,!^^66A   		s   CC(C&C(c                 R  K   t        ||      }| j                  ||       d{    | j                  |       d{    |rA|j                  d      r0t	        j
                  |j                  dd      dz         d{    | j                  |       d{    y7 z7 c7 "7 w)a  Click button at (``x``, ``y``).

        Shortcut to :meth:`move`, :meth:`down`, and :meth:`up`.

        This method accepts the following options:

        * ``button`` (str): ``left``, ``right``, or ``middle``, defaults to
          ``left``.
        * ``clickCount`` (int): defaults to 1.
        * ``delay`` (int|float): Time to wait between ``mousedown`` and
          ``mouseup`` in milliseconds. Defaults to 0.
        NrL   r   rM   )r	   rp   r0   r-   rO   rP   rG   )r   rd   re   r   r   s        r   clickzMouse.click  s      Wf-ii1oii   w{{7+--GQ 7$ >???ggg	 	 ?sD   "B'BB'B!AB' B#B'B%B'!B'#B'%B'c                 6  K   t        ||      }|j                  dd      | _        | j                  j	                  dd| j                  | j
                  | j                  | j                  j                  |j                  d      xs dd       d{    y7 w)	a   Press down button (dispatches ``mousedown`` event).

        This method accepts the following options:

        * ``button`` (str): ``left``, ``right``, or ``middle``, defaults to
          ``left``.
        * ``clickCount`` (int): defaults to 1.
        rj   leftrh   mousePressed
clickCountr3   r$   rj   rd   re   r%   rv   N)	r	   r-   rb   r   r.   r`   ra   r_   r   r   r   r   s      r   r0   z
Mouse.down2  s      Wf-{{8V4ll :"ll22!++l38q=
  	 	s   BBBBc                 "  K   t        ||      }d| _        | j                  j                  dd|j	                  dd      | j
                  | j                  | j                  j                  |j	                  d      xs dd       d	{    y	7 w)
a  Release pressed button (dispatches ``mouseup`` event).

        This method accepts the following options:

        * ``button`` (str): ``left``, ``right``, or ``middle``, defaults to
          ``left``.
        * ``clickCount`` (int): defaults to 1.
        r^   rh   mouseReleasedrj   rt   rv   r3   rw   N)	r	   rb   r   r.   r-   r`   ra   r_   r   rx   s      r   rG   zMouse.upF  s|      Wf-ll :#kk(F322!++l38q=
  	 	s   BBBBrR   )rS   rT   rU   rV   r   r   r   floatrX   r   rp   rr   r0   rG   r:   r   r   r[   r[      s    z X $ E e d  %)0U u t !&**$   ( s t r   r[   c                   8    e Zd ZdZdededdfdZdededdfd	Zy)
TouchscreenzTouchscreen class.r   r\   r   Nc                      || _         || _        y)zMake new touchscreen object.N)r   r_   rc   s      r   r   zTouchscreen.__init__^  s    !r   rd   re   c                 4  K   t        |      t        |      dg}| j                  j                  dd|| j                  j                  d       d{    | j                  j                  ddg | j                  j                  d       d{    y7 A7 w)zYTap (``x``, ``y``).

        Dispatches a ``touchstart`` and ``touchend`` event.
        )rd   re   zInput.dispatchTouchEvent
touchStart)r$   touchPointsr%   NtouchEnd)rl   r   r.   r_   r   )r   rd   re   r   s       r   tapzTouchscreen.tapc  s     
 #1XE!H56ll : &22=
  	 	
 ll :22=
  	 		
	s$   ABB;BBBB)	rS   rT   rU   rV   r   r   r   r{   r   r:   r   r   r}   r}   [  s9    "z "X "$ "
5 U t r   r}   )rV   rO   typingr   r   r   pyppeteer.connectionr   pyppeteer.errorsr   pyppeteer.us_keyboard_layoutr   pyppeteer.utilr	   r
   objectr   r[   r}   r:   r   r   <module>r      sO    !  + + + + 7 %bv bJaF aH& r   