Python pyautogui.press() Examples

The following are 13 code examples of pyautogui.press(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pyautogui , or try the search function .
Example #1
Source File: _keyboard.py    From robotframework-imagehorizonlibrary with MIT License 8 votes vote down vote up
def type(self, *keys_or_text):
        '''Type text and keyboard keys.

        See valid keyboard keys in `Press Combination`.

        Examples:

        | Type | separated              | Key.ENTER | by linebreak |
        | Type | Submit this with enter | Key.enter |              |
        | Type | key.windows            | notepad   | Key.enter    |
        '''
        for key_or_text in keys_or_text:
            key = self._convert_to_valid_special_key(key_or_text)
            if key:
                ag.press(key)
            else:
                ag.typewrite(key_or_text) 
Example #2
Source File: tools.py    From Dindo-Bot with MIT License 8 votes vote down vote up
def type_text(text, interval=0.1):
	for c in text:
		if c.isdigit():
			pyautogui.hotkey('shift', c, interval=0.1)
		else:
			pyautogui.press(c)
		time.sleep(interval)

# Scroll to value 
Example #3
Source File: test_pyautogui.py    From pyautogui with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def test_failsafe(self):
        self.oldFailsafeSetting = pyautogui.FAILSAFE

        pyautogui.moveTo(1, 1)  # make sure mouse is not in failsafe position to begin with
        for x, y in pyautogui.FAILSAFE_POINTS:
            pyautogui.FAILSAFE = True
            # When move(), moveTo(), drag(), or dragTo() moves the mouse to a
            # failsafe point, it shouldn't raise the fail safe. (This would
            # be annoying. Only a human moving the mouse to a failsafe point
            # should trigger the failsafe.)
            pyautogui.moveTo(x, y)

            pyautogui.FAILSAFE = False
            pyautogui.moveTo(1, 1)  # make sure mouse is not in failsafe position to begin with (for the next iteration)

        pyautogui.moveTo(1, 1)  # make sure mouse is not in failsafe position to begin with
        for x, y in pyautogui.FAILSAFE_POINTS:
            pyautogui.FAILSAFE = True
            pyautogui.moveTo(x, y)  # This line should not cause the fail safe exception to be raised.

            # A second pyautogui function call to do something while the cursor is in a fail safe point SHOULD raise the failsafe:
            self.assertRaises(pyautogui.FailSafeException, pyautogui.press, "esc")

            pyautogui.FAILSAFE = False
            pyautogui.moveTo(1, 1)  # make sure mouse is not in failsafe position to begin with (for the next iteration)

        for x, y in pyautogui.FAILSAFE_POINTS:
            pyautogui.FAILSAFE = False
            pyautogui.moveTo(x, y)  # This line should not cause the fail safe exception to be raised.

            # This line shouldn't cause a failsafe to trigger because FAILSAFE is set to False.
            pyautogui.press("esc")

        pyautogui.FAILSAFE = self.oldFailsafeSetting 
Example #4
Source File: tools.py    From Dindo-Bot with MIT License 7 votes vote down vote up
def press_key(key, interval=None):
	if interval is not None:
		time.sleep(interval)
	keys = parser.parse_key(key)
	count = len(keys)
	if count == 1:
		pyautogui.press(keys[0])
	elif count == 2:
		pyautogui.hotkey(keys[0], keys[1])

# Type text 
Example #5
Source File: gmail_generator.py    From gmail-generator with MIT License 6 votes vote down vote up
def locate_gmail():
    
    #Sleep for a while and wait for Firefox to open
    time.sleep(3)

    # Printing message
    msg(1,'Opening Gmail...')

    # Typing the website on the browser
    pyautogui.keyDown('ctrlleft');  pyautogui.typewrite('a'); pyautogui.keyUp('ctrlleft')
    pyautogui.typewrite('https://accounts.google.com/SignUp?service=mail&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ltmpl=default')
    pyautogui.typewrite('\n')
    
    # Wait for a while until the website responds
    time.sleep(6)

    # Print a simple message
    msg(1,'Locating the form...')

    # Locate the form
    pyautogui.press('tab')
 
    time.sleep(2)

    _gmail_ = pyautogui.locateOnScreen('images/gmail_form.png')
    formx, formy = pyautogui.center(_gmail_)
    pyautogui.click(formx, formy)
    
    # Check and print message
    if not pyautogui.click(formx, formy):
        msg(1,'Located the form.')
    else:
        msg(3,'Failed to locate the form.')
        ext()


# Function used to randomize credentials 
Example #6
Source File: im_bot.py    From automate-the-boring-stuff-projects with MIT License 6 votes vote down vote up
def auto_message(name, message):
    """Searches for friend on Google Hangouts and messages them."""
    print("Make sure the Google Hangout 'Conversations' page is visible and "
          "your cursor is not currently on the page.")
    time.sleep(3)

    search_bar = pyautogui.locateOnScreen('search.png')
    pyautogui.click(search_bar)
    pyautogui.typewrite(name)
    time.sleep(1)

    online_select = pyautogui.locateOnScreen('online-friend.png')
    if online_select is None:
        print('Friend not found or currently offline.')
        return
    else:
        pyautogui.doubleClick(online_select)

    attempts = 3
    while attempts > 0:
        message_box = pyautogui.locateOnScreen('message.png')
        pyautogui.click(message_box)
        pyautogui.typewrite(message)

        # If it can no longer be found it is because the message was entered.
        if pyautogui.locateOnScreen('message.png') is None:
            pyautogui.press('enter')
            pyautogui.press('esc')
            print('Message sent to {}'.format(name))
            break
        else:
            if attempts == 1:
                print('Unable to send message to {}.'.format(name))
                pyautogui.press('esc')

            else:
                print('Sending message to {} failed. Another {} attempts will '
                      'be made before moving on.'.format(name, attempts))

            attempts -= 1 
Example #7
Source File: recovery.py    From kcauto with GNU General Public License v3.0 6 votes vote down vote up
def basic_recovery():
        """Method that contains steps for basic recovery attempt, which
        involves clearing any interaction-blocking browser popups.

        Returns:
            bool: True if recovery was successful; False otherwise.
        """
        Log.log_debug("Attempting Basic Recovery.")

        if cfg.config.general.is_direct_control:
            pyautogui.moveTo(1, 1)
            pyautogui.press('esc')
            sleep(0.5)
            pyautogui.press('space')
            sleep(0.5)
            pyautogui.press('f10')
            sleep(0.5)

        try:
            if kca_u.kca.find_kancolle():
                return True
        except FindFailed:
            pass

        return False 
Example #8
Source File: recovery.py    From kcauto with GNU General Public License v3.0 6 votes vote down vote up
def _refresh_screen(screen):
        """Helper method that contains steps for refreshing the tab. Includes
        logic to re-instantiate neccessary Chrome hooks.

        Args:
            screen (Region, optional): screen region.
        """
        if cfg.config.general.is_direct_control:
            pyautogui.press('f5')
            sleep(0.5)
            pyautogui.press('space')
            sleep(0.5)
            pyautogui.press('tab')
            sleep(0.5)
            pyautogui.press('space')
            sleep(5)
        else:
            kca_u.kca.visual_hook.Page.reload()
            sleep(0.5)

        kca_u.kca.wait(screen, 'global|game_start.png', 90)
        sleep(3) 
Example #9
Source File: dino_api.py    From go_dino with GNU General Public License v3.0 5 votes vote down vote up
def play_game(self, get_command_callback: Callable[[int, int, int], str]) -> int:
        self.start_game()

        start = last_compute_speed = last_command_time = time.time()
        last_distance = self.landscape['width']
        speed = 0
        last_speeds = [3] * 30

        while True:
            buffer = self.shooter.grab(self.landscape)
            image = Image.frombytes('RGB', buffer.size, buffer.rgb).convert('L')
            image = np.array(image)
            image += np.abs(247 - image[0, self.x2])
            roi = image[self.y1:self.y2, self.x1:self.x2]
            score = int((time.time() - start) * 10)
            distance, size = self.compute_distance_and_size(roi, self.x2)
            speed = self.compute_speed(distance, last_distance, speed, last_speeds, last_compute_speed)
            last_compute_speed = time.time()
            # Check GAME OVER
            if distance == last_distance or distance == 0:
                res = cv2.matchTemplate(image, self.gameover_template, cv2.TM_CCOEFF_NORMED)
                if np.max(res) > 0.5:
                    return score
            last_distance = distance
            if time.time() - last_command_time < 0.6:
                continue
            command = get_command_callback(distance, size, speed)
            if command:
                last_command_time = time.time()
                pyautogui.press(command) 
Example #10
Source File: dino_api.py    From go_dino with GNU General Public License v3.0 5 votes vote down vote up
def start_game():
        pyautogui.press('up')
        time.sleep(.3)
        pyautogui.press('up')
        time.sleep(.3)
        pyautogui.press('up')
        time.sleep(1.) 
Example #11
Source File: test_pyautogui.py    From pyautogui with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run(self):
        time.sleep(0.25)  # NOTE: BE SURE TO ACCOUNT FOR THIS QUARTER SECOND FOR TIMING TESTS!
        pyautogui.press(self.keysArg) 
Example #12
Source File: dino_chrome_bot.py    From dino_chrome_bot with MIT License 5 votes vote down vote up
def jump():
    global X
    pyautogui.press("up")
    X += 0.4  # Increment in detection region for increase speed of game 
Example #13
Source File: playback.py    From xbmc with GNU General Public License v3.0 3 votes vote down vote up
def _Input(mousex=0, mousey=0, click=0, keys=None, delay='0.2'):
    import pyautogui
    '''Control the user's mouse and/or keyboard.
       Arguments:
         mousex, mousey - x, y co-ordinates from top left of screen
         keys - list of keys to press or single key
    '''
    g = Globals()
    screenWidth, screenHeight = pyautogui.size()
    mousex = int(screenWidth / 2) if mousex == -1 else mousex
    mousey = int(screenHeight / 2) if mousey == -1 else mousey
    exit_cmd = [('alt', 'f4'), ('ctrl', 'shift', 'q'), ('command', 'q')][(g.platform & -g.platform).bit_length() - 1]

    if keys:
        if '{EX}' in keys:
            pyautogui.hotkey(*exit_cmd)
        else:
            pyautogui.press(keys, interval=delay)
    else:
        pyautogui.moveTo(mousex, mousey)
        if click:
            pyautogui.click(clicks=click)

    Log('Input command: Mouse(x={}, y={}, click={}), Keyboard({})'.format(mousex, mousey, click, keys))