a
    >xd2                     @   s   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
 d
eZG dd deZG dd deZdS )zKeyboard and Mouse module.    N)AnyDictTYPE_CHECKING)
CDPSession)PyppeteerError)keyDefinitions)
merge_dict)Setc                   @   s   e Zd ZdZeddddZdeeeddddZ	ee
d	d
dZeedddZedd	ddZeddddZdeeeddddZdeeeddddZdS )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')
    N)clientreturnc                 C   s   || _ d| _t | _d S )Nr   )_client
_modifiersset_pressedKeys)selfr    r   G/var/www/html/Ranjet/env/lib/python3.9/site-packages/pyppeteer/input.py__init__5   s    zKeyboard.__init__)keyoptionskwargsr   c                    s   t ||}| |}|d | jv }| j|d  |  j| |d O  _|d}|du rd|d }| jd|rtdnd| j|d |d |d ||||d	 |d	 d
kd
I dH  dS )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.dispatchKeyEventZkeyDownZ
rawKeyDownkeyCodelocation   )
type	modifierswindowsVirtualKeyCoder   r   r   unmodifiedText
autoRepeatr   ZisKeypad)	r   _keyDescriptionForStringr   addr   _modifierBitgetr   send)r   r   r   r   descriptionr"   r   r   r   r   down:   s(    




zKeyboard.down)r   r   c                 C   s4   |dkrdS |dkrdS |dkr$dS |dkr0dS d	S )
NZAlt   Control   ZMeta   ZShift   r   r   )r   r   r   r   r   r%   g   s    zKeyboard._modifierBit)	keyStringr   c                 C   s   | j d@ }dddddd}t|}|s6td| d|v rJ|d |d< |rd|drd|d |d< d|v rx|d |d< |r|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|dr|d |d< | j d@ rd|d< |S )Nr.    r   )r   r   r   r   r   zUnknown key: r   ZshiftKeyr   ZshiftKeyCoder   r   r*   r   Z	shiftTexti)r   r   r&   r   len)r   r/   shiftr(   Z
definitionr   r   r   r#   r   s>    

z!Keyboard._keyDescriptionForStringc              
      sz   |  |}|  j| |d  M  _|d | jv rB| j|d  | jdd| j|d |d |d |d dI dH  dS )	zyDispatch a ``keyup`` event of the ``key``.

        :arg str key: Name of key to release, such as ``ArrowLeft``.
        r   r   r   ZkeyUpr   r   )r   r   r   r    r   r   N)r#   r   r%   r   remover   r'   )r   r   r(   r   r   r   up   s    
zKeyboard.up)charr   c              	      s&   | j dd| j|||dI dH  dS )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.
        r   r5   )r   r   r   r   r!   N)r   r'   r   )r   r5   r   r   r   sendCharacter   s    
zKeyboard.sendCharacter)r   r   r   r   c                    sl   t ||}|dd}|D ]L}|tv r>| |d|iI dH  n| |I dH  |rt|d I dH  qdS )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   pressr6   asynciosleep)r   r   r   r   r7   r5   r   r   r   r      s    
zKeyboard.typec                    sP   t ||}| ||I dH  d|v r<t|d d I dH  | |I dH  dS )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.
        Nr7   r8   )r   r)   r:   r;   r4   )r   r   r   r   r   r   r   r9      s
    
zKeyboard.press)N)N)N)__name__
__module____qualname____doc__r   r   strdictr   r)   intr%   r   r#   r4   r6   r   r9   r   r   r   r   r
      s   "-+r
   c                   @   sz   e Zd ZdZeeddddZdeeee	ddddZ
deeee	ddd	d
Zdee	ddddZdee	ddddZdS )MousezMouse class.Nr   keyboardr   c                 C   s"   || _ || _d| _d| _d| _d S )Ng        none)r   	_keyboard_x_y_buttonr   r   rE   r   r   r   r      s
    zMouse.__init__)xyr   r   r   c           	   
      s   t ||}| j}| j}|| _|| _|dd}td|d D ]^}t|| j| ||   }t|| j| ||   }| jdd| j||| j	j
dI dH  q<dS )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.
        stepsr*   Input.dispatchMouseEventZ
mouseMoved)r   buttonrL   rM   r   N)r   rH   rI   r&   rangeroundr   r'   rJ   rG   r   )	r   rL   rM   r   r   ZfromXZfromYrN   ir   r   r   move  s     
z
Mouse.movec                    sj   t ||}| ||I dH  | |I dH  |rV|drVt|ddd I dH  | |I dH  dS )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.
        Nr7   r   r8   )r   rT   r)   r&   r:   r;   r4   )r   rL   rM   r   r   r   r   r   click  s    
zMouse.click)r   r   r   c                    sR   t ||}|dd| _| jdd| j| j| j| jj|dp@ddI dH  dS )	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.
        rP   leftrO   ZmousePressed
clickCountr*   r   rP   rL   rM   r   rW   N)	r   r&   rJ   r   r'   rH   rI   rG   r   r   r   r   r   r   r   r)   4  s    	
z
Mouse.downc                    sP   t ||}d| _| jdd|dd| j| j| jj|dp>ddI d	H  d	S )
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.
        rF   rO   ZmouseReleasedrP   rV   rW   r*   rX   N)	r   rJ   r   r'   r&   rH   rI   rG   r   rY   r   r   r   r4   H  s    	

zMouse.up)N)N)N)N)r<   r=   r>   r?   r   r
   r   floatrA   r   rT   rU   r)   r4   r   r   r   r   rC      s   rC   c                   @   s4   e Zd ZdZeeddddZeeddddZdS )	TouchscreenzTouchscreen class.NrD   c                 C   s   || _ || _dS )zMake new touchscreen object.N)r   rG   rK   r   r   r   r   `  s    zTouchscreen.__init__)rL   rM   r   c                    sX   t |t |dg}| jdd|| jjdI dH  | jddg | jjdI dH  dS )zYTap (``x``, ``y``).

        Dispatches a ``touchstart`` and ``touchend`` event.
        )rL   rM   zInput.dispatchTouchEventZ
touchStart)r   touchPointsr   NZtouchEnd)rR   r   r'   rG   r   )r   rL   rM   r\   r   r   r   tape  s    zTouchscreen.tap)	r<   r=   r>   r?   r   r
   r   rZ   r]   r   r   r   r   r[   ]  s   r[   )r?   r:   typingr   r   r   Zpyppeteer.connectionr   Zpyppeteer.errorsr   Zpyppeteer.us_keyboard_layoutr   Zpyppeteer.utilr   r	   objectr
   rC   r[   r   r   r   r   <module>   s    l`