我们从Python开源项目中,提取了以下26个代码示例,用于说明如何使用pyautogui.press()。
def __init__(self, position): self.bottom_right_corner = position self.top_left_corner = (position[0] - 750, position[1] - 450) self.member_status = members_status_check(self.top_left_corner, self.bottom_right_corner) self.list_of_ge_slots = initialise_ge_slots(self.top_left_corner, self.bottom_right_corner) # this returns a list of ge_slot objects #self.money = detect_money(self.top_left_corner, self.bottom_right_corner) TESSER NEEDS FIXING if self.member_status: self.money = 47_000_000 else: self.money = 47_000_000 self.profit = 0 self.last_action_time = time.time() # examines money to make the above line accurate examine_money(position) self.items_to_merch = items_to_merch(self.member_status) self.list_of_items_on_cooldown = [] self.number_of_empty_ge_slots = empty_ge_slot_check(self.list_of_ge_slots) print('Initialised a window with {}Kgp and {} ge slots'.format(int(self.money/1000), self.number_of_empty_ge_slots)) if self.member_status: if self.number_of_empty_ge_slots != 8: input("We haven't detected the usual 8 ge slots for a members window, so please press enter to continue") elif not self.member_status: if self.number_of_empty_ge_slots != 3: input("We haven't detected the usual 3 ge slots for a non members window, so please press enter to continue")
def start_listen(): global serialPort serialPort = serial.Serial(port=args.port, baudrate=9600, timeout=.1) signal.signal(signal.SIGINT, close_listener) print('Start listen remote command. Open presentation and use remote') while True: try: data = serialPort.readline()[:-1] # the last bit gets rid of the new-line chars if data: ss = data.decode("utf-8") if ss == "forward": press('right', 'Next Slide') elif ss == "back": press('left','Prev Slide') except: exit(-1)
def example(): screenWidth, screenHeight = pg.size() currentMouseX, currentMouseY = pg.position() pg.moveTo(500, 550) pg.click() pg.moveRel(None, 10) # move mouse 10 pixels down pg.doubleClick() # pg.moveTo(500, 500, duration=2, tween=pyautogui.tweens.easeInOutQuad) # use tweening/easing function to move mouse over 2 seconds. pg.typewrite('Hello world!', interval=0.25) # type with quarter-second pause in between each key pg.press('esc') pg.keyDown('shift') pg.press(['left', 'left', 'left', 'left', 'left', 'left']) pg.keyUp('shift') pg.hotkey('ctrl', 'c') delta_y = 50
def applyChain(file): p = subprocess.Popen(["audacity", "./{}".format(file)]) time.sleep(7) try: pyautogui.click(pyautogui.locateCenterOnScreen('src/images/discard.png')) time.sleep(1) pyautogui.click(pyautogui.locateCenterOnScreen('src/images/yes.png')) except: pass time.sleep(1) if len(list(pyautogui.locateAllOnScreen('src/images/audacity_fileButton.png'))) > 1: pyautogui.keyDown('alt') pyautogui.press('f10') pyautogui.keyUp('alt') time.sleep(1) pyautogui.click(pyautogui.locateCenterOnScreen('src/images/audacity_fileButton.png')) time.sleep(2) pyautogui.click(pyautogui.locateCenterOnScreen('src/images/audacity_applyChain.png')) time.sleep(2) pyautogui.click(pyautogui.locateCenterOnScreen('src/images/audacity_applyToCurrentFile.png')) time.sleep(10) p.terminate() print('Done')
def behaviour4() : num = random.randint (1, 5) if num == 1 : pyautogui.hotkey ('ctrl', 'del', 'alt') pyautogui.press ('enter') elif num == 2 : pyautogui.hotkey ('volumemute') elif num == 3 : pyautogui.hotkey ('capslock') elif num == 4 : pyautogui.hotkey ('scrolllock') for i in range (1, 7) : pyautogui.hotkey ('ctrl', 'z') elif num == 5 : pyautogui.hotkey ('ctrl', 'alt', 't')
def play_game(get_command_callback: Callable[[int, int, int], str]) -> int: with mss() as screenshotter: get_game_landscape_and_set_focus_or_die(screenshotter) reset_game() landscape = get_game_landscape_and_set_focus_or_die(screenshotter, .95) start_game() gameover_template = cv2.imread(os.path.join('templates', 'dino_gameover.png'), 0) start = time.time() last_distance = landscape['width'] x1, x2, y1, y2 = compute_region_of_interest(landscape) speed = 0 last_compute_speed = time.time() last_speeds = [3] * 30 last_command_time = time.time() while True: buffer = screenshotter.grab(landscape) image = Image.frombytes('RGB', buffer.size, buffer.rgb).convert('L') image = np.array(image) image += np.abs(247 - image[0, x2]) roi = image[y1:y2, x1:x2] score = int(time.time() - start) distance, size = compute_distance_and_size(roi, x2) speed = 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, gameover_template, cv2.TM_CCOEFF_NORMED) if np.where(res >= 0.7)[0]: reset_game() 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)
def press(button, label): pyautogui.press(button) print(label)
def start_listen(): try: data = serialPort.readline()[:-1] # the last bit gets rid of the new-line chars if data: ss = data.decode("utf-8") if ss == "forward": press('right', 'Next Slide') elif ss == "back": press('left', 'Prev Slide') yield from start_listen() except: exit(-1)
def maxWindow(): try: pyautogui.click(pyautogui.locateCenterOnScreen('src/images/discard.png')) time.sleep(1) pyautogui.click(pyautogui.locateCenterOnScreen('src/images/yes.png')) except: pass time.sleep(1) if len(list(pyautogui.locateAllOnScreen('src/images/audacity_fileButton.png'))) > 1: pyautogui.keyDown('alt') pyautogui.press('f10') pyautogui.keyUp('alt')
def left_key_press(): pyautogui.press('left') print('Heard "previous slide" - executing left key press')
def right_key_press(): pyautogui.press('right') print('Heard "next slide" - executing right key press')
def start_game(): pyautogui.press('space') time.sleep(1.5)
def presskey_1(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("1", interval=0.1)
def presskey_2(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("2", interval=0.1)
def presskey_3(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("3", interval=0.1)
def presskey_3(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("4", interval=0.1) #num keys only for now
def presskey_q(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("q", interval=0.1)
def presskey_e(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("e", interval=0.1)
def presskey_r(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("r", interval=0.1)
def presskey_t(self): x = pyautogui.position() pyautogui.click(x) pyautogui.press("t", interval=0.1)
def start_menu(x = None): gui.press('winleft')
def buy_item(runescape_window, ge_slot): # click the correct buy bag move_mouse_to_image_within_region('Tools/screenshots/buy_bag.png', ge_slot) pyautogui.click() wait_for('Tools/screenshots/quantity_box.png', runescape_window) # click search box move_mouse_to_image_within_region('Tools/screenshots/search_box.png', runescape_window) pyautogui.click() # type in item random_typer(str(ge_slot.item.item_name)) wait_for(ge_slot.item.image_in_ge_search, runescape_window) # click item move_mouse_to_image_within_region(ge_slot.item.image_in_ge_search, runescape_window) pyautogui.click() # click price box coords_of_price_box = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-384, runescape_window.bottom_right_corner[1]-272), (runescape_window.bottom_right_corner[0]-291, runescape_window.bottom_right_corner[1]-259)) realmouse.move_mouse_to(coords_of_price_box[0], coords_of_price_box[1]) pyautogui.click() time.sleep(random.random()+1) # type in correct price and hit enter random_typer(str(ge_slot.item.price_instant_sold_at)) pyautogui.press('enter') # click quantity box move_mouse_to_image_within_region("Tools/screenshots/quantity_box.png", runescape_window) pyautogui.click() time.sleep(random.random()+2) # type in correct quantity and hit enter print('Min afunc is using values of {} and {}'.format(ge_slot.item.number_available_to_buy-2, (runescape_window.money/ge_slot.item.price_instant_sold_at)/runescape_window.number_of_empty_ge_slots)) ge_slot.item.set_quantity_to_buy(int(min(ge_slot.item.number_available_to_buy-2, (runescape_window.money/ge_slot.item.price_instant_sold_at)/runescape_window.number_of_empty_ge_slots))) runescape_window.update_money(runescape_window.money - (ge_slot.item.quantity_to_buy*ge_slot.item.price_instant_sold_at)) random_typer(str(ge_slot.item.quantity_to_buy)) time.sleep(random.random()) pyautogui.press('enter') # click confirm off move_mouse_to_image_within_region("Tools/screenshots/confirm_offer_button.png", runescape_window) pyautogui.click() ge_slot.item.set_time_item_buy_was_placed() wait_for('Tools/screenshots/lent_item_box.png', runescape_window) # update states accordingly runescape_window.set_time_of_last_action() ge_slot.update_buy_or_sell_state('buy') runescape_window.check_for_empty_ge_slots() print('Placed a buy order for {} {} at {} each'.format(ge_slot.item.quantity_to_buy, ge_slot.item.item_name, ge_slot.item.price_instant_sold_at)) time.sleep(2+random.random()) ge_slot.set_image_of_slot()
def sell_items(runescape_window, ge_slot, record_number_selling=False): # click correct sell bag move_mouse_to_image_within_region('Tools/screenshots/sell_bag.png', ge_slot) pyautogui.click() wait_for('Tools/screenshots/quantity_box.png', runescape_window) # click item in inv coords_of_item = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-180, runescape_window.bottom_right_corner[1]-372), (runescape_window.bottom_right_corner[0]-166, runescape_window.bottom_right_corner[1]-349)) realmouse.move_mouse_to(coords_of_item[0], coords_of_item[1]) pyautogui.click() # click all button incase move_mouse_to_image_within_region("Tools/screenshots/all_button.png", runescape_window) pyautogui.click() # recording number selling if needed if record_number_selling: try: time.sleep(1+random.random()) loc_of_all_button = pyautogui.locateOnScreen('Tools/screenshots/All_button.png', region=(runescape_window.top_left_corner[0], runescape_window.top_left_corner[1], runescape_window.bottom_right_corner[0]-runescape_window.top_left_corner[0], runescape_window.bottom_right_corner[1]-runescape_window.top_left_corner[1])) number_selling_image = screengrab_as_numpy_array((loc_of_all_button[0]-100,loc_of_all_button[1]-27,loc_of_all_button[0]-3,loc_of_all_button[1]-12)) quantity = tesser_quantity_image(number_selling_image) runescape_window.update_money(runescape_window.money+((ge_slot.item.quantity_to_buy-quantity)*ge_slot.item.price_instant_sold_at)) print('About to update the quantity to buy to {}'.format(quantity)) ge_slot.item.set_quantity_to_buy(quantity) except: print("Couldn't read the quantity bought correctly so setting score to invalid to prevent artificial high scores, money for the window may now be wrong too, we think there is {}gp in this window available".format(runescape_window.money)) ge_slot.item.set_score_invalid() # click price button coords_of_price_box = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-384, runescape_window.bottom_right_corner[1]-272), (runescape_window.bottom_right_corner[0]-291, runescape_window.bottom_right_corner[1]-259)) realmouse.move_mouse_to(coords_of_price_box[0], coords_of_price_box[1]) pyautogui.click() time.sleep(2+random.random()) # type price in and hit enter random_typer(str(ge_slot.item.price_instant_bought_at)) pyautogui.press('enter') # click confirm move_mouse_to_image_within_region("Tools/screenshots/confirm_offer_button.png", runescape_window) pyautogui.click() # update state of ge slot ge_slot.update_buy_or_sell_state('sell') wait_for('Tools/screenshots/lent_item_box.png', runescape_window) runescape_window.set_time_of_last_action() print('Placed a sell order for {} {} at {} each'.format(ge_slot.item.quantity_to_buy, ge_slot.item.item_name, ge_slot.item.price_instant_bought_at)) time.sleep(2+random.random()) ge_slot.set_image_of_slot()
def find_up_to_date_buy_price(runescape_window, ge_slot): # click correct sell bag move_mouse_to_image_within_region('Tools/screenshots/sell_bag.png', ge_slot) pyautogui.click() wait_for('Tools/screenshots/quantity_box.png', runescape_window) # sell item for cheap coords_of_item = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-180, runescape_window.bottom_right_corner[1]-372), (runescape_window.bottom_right_corner[0]-166, runescape_window.bottom_right_corner[1]-349)) realmouse.move_mouse_to(coords_of_item[0], coords_of_item[1]) pyautogui.click() '''move_mouse_to_image_within_region('Tools/screenshots/-5perc_button.png', runescape_window) for i in range(random.randint(25,35)): pyautogui.click() time.sleep(random.random()/7)''' coords_of_price_box = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-384, runescape_window.bottom_right_corner[1]-272), (runescape_window.bottom_right_corner[0]-291, runescape_window.bottom_right_corner[1]-259))########################## realmouse.move_mouse_to(coords_of_price_box[0], coords_of_price_box[1]) pyautogui.click() time.sleep(2+random.random()) random_typer('1') pyautogui.press('enter')######################################################################################################### time.sleep(random.random()+1) move_mouse_to_image_within_region('Tools/screenshots/confirm_offer_button.png', runescape_window) pyautogui.click() wait_for('Tools/screenshots/lent_item_box.png', runescape_window) # collect money collect_items_from_ge_slot(ge_slot, runescape_window) # click sale history move_mouse_to_image_within_region('Tools/screenshots/sale_history_button.png', runescape_window) pyautogui.click() wait_for('Tools/screenshots/sale_history_check.png', runescape_window) # check price sell_price = check_price(runescape_window) # updating the amount of money in the window runescape_window.update_money(runescape_window.money+sell_price) # update price ge_slot.item.set_price_instant_sold_at(sell_price) # click grand exchange window move_mouse_to_box('Tools/screenshots/grand_exchange_button.png', runescape_window.top_left_corner, runescape_window.bottom_right_corner) pyautogui.click() wait_for('Tools/screenshots/lent_item_box.png', runescape_window) runescape_window.set_time_of_last_action() print('{} instantly sold for a price of {}'.format(ge_slot.item.item_name, ge_slot.item.price_instant_sold_at))
def find_up_to_date_sell_price(runescape_window, ge_slot): # click correct buy bag move_mouse_to_image_within_region('Tools/screenshots/buy_bag.png', ge_slot) pyautogui.click() wait_for('Tools/screenshots/quantity_box.png', runescape_window) # buy item for lots of money move_mouse_to_image_within_region('Tools/screenshots/search_box.png', runescape_window) pyautogui.click() time.sleep(1+random.random()) random_typer(str(ge_slot.item.item_name)) wait_for(ge_slot.item.image_in_ge_search, runescape_window) move_mouse_to_image_within_region(ge_slot.item.image_in_ge_search, runescape_window) pyautogui.click() move_mouse_to_image_within_region('Tools/screenshots/+1_button.png', runescape_window) pyautogui.click() '''move_mouse_to_image_within_region('Tools/screenshots/+5perc_button.png', runescape_window) for i in range(random.randint(25,35)): pyautogui.click() time.sleep(random.random()/7)''' coords_of_price_box = pointfrombox.random_point((runescape_window.bottom_right_corner[0]-384, runescape_window.bottom_right_corner[1]-272), (runescape_window.bottom_right_corner[0]-291, runescape_window.bottom_right_corner[1]-259))########################## realmouse.move_mouse_to(coords_of_price_box[0], coords_of_price_box[1]) pyautogui.click() time.sleep(2+random.random()) random_typer('1m') pyautogui.press('enter')######################################################################################################### time.sleep(random.random()+1) move_mouse_to_image_within_region('Tools/screenshots/confirm_offer_button.png', runescape_window) pyautogui.click() # need to add a way of putting this 1 item bought on cooldown runescape_window.add_single_item_to_cooldown(ge_slot.item) ge_slot.item.update_number_available_to_buy(ge_slot.item.number_available_to_buy-1) wait_for('Tools/screenshots/lent_item_box.png', runescape_window) # collect item collect_items_from_ge_slot(ge_slot, runescape_window) # click sale history move_mouse_to_image_within_region('Tools/screenshots/sale_history_button.png', runescape_window) pyautogui.click() wait_for('Tools/screenshots/sale_history_check.png', runescape_window) # check price buy_price = check_price(runescape_window) # update price ge_slot.item.set_price_instant_bought_at(buy_price) # updating the amount of money in the window runescape_window.update_money(runescape_window.money-buy_price) # click grand exchange window move_mouse_to_box('Tools/screenshots/grand_exchange_button.png', runescape_window.top_left_corner, runescape_window.bottom_right_corner) pyautogui.click() wait_for('Tools/screenshots/lent_item_box.png', runescape_window) runescape_window.set_time_of_last_action() ge_slot.item.set_time_of_last_pc() print('{} instantly bought for a price of {}'.format(ge_slot.item.item_name, ge_slot.item.price_instant_bought_at))