Python, how to detect a 'right' arrow key to skip some line of codes

Viewed 573

I'm new to this forum and python, any help is greatly appreciated.

I'm trying to add a if statement under FOR LOOP, as my code shows below, at the moment, my mouse moves to 4 spots before it closes the current web tab, and it repeats this for 10 times.

My question is how to add a if statement ie. if the right arrow key is pressed while the mouse is moving, it skips a few line of codes and goes straight to the last four line of codes?

The current tab is closed and it moves onto the next tab, until it finishes it's 10 cycles. This new If statement helps to close the browser tab much quicker with human intervention instead of waiting for the whole 8 seconds.

Thank you.

import pyautogui as pg
import time, pyautogui
import keyboard

from pynput.keyboard import Key, Listener
pg.moveTo(2679, 900)
pg.click()

for i in range (10):
    pg.moveTo(2679, 1620, duration=2)
    pg.moveTo(2679, 1923, duration=2)
    pg.moveTo(1365, 1875, duration=2)
    pg.moveTo(1365, 1572, duration=2)
    pg.keyDown('ctrl')
    pg.keyDown('w')
    pg.keyUp('w')
    pg.keyUp('ctrl')

2 Answers

Not sure if this is exactly what you mean, however when the right arrow key is pressed it will exit the loop being able to continue the code.

import pyautogui as pg
import time, pyautogui
import keyboard

from pynput.keyboard import Key, Listener
pg.moveTo(2679, 900)
pg.click()

for i in range (10):
    if keyboard.is_pressed('right arrow'):
        break
    pg.moveTo(2679, 1620, duration=2)
    pg.moveTo(2679, 1923, duration=2)
    pg.moveTo(1365, 1875, duration=2)
    pg.moveTo(1365, 1572, duration=2)
    pg.keyDown('ctrl')
    pg.keyDown('w')
    pg.keyUp('w')
    pg.keyUp('ctrl')

or maybe:

for i in range (10):
    if keyboard.is_pressed('right arrow'):
        pg.keyDown('ctrl')
        pg.keyDown('w')
        pg.keyUp('w')
        pg.keyUp('ctrl')
    else:
        pg.moveTo(2679, 1620, duration=2)
        pg.moveTo(2679, 1923, duration=2)
        pg.moveTo(1365, 1875, duration=2)
        pg.moveTo(1365, 1572, duration=2)
        pg.keyDown('ctrl')
        pg.keyDown('w')
        pg.keyUp('w')
        pg.keyUp('ctrl')

You could use threading to check for the keypress instantly which might save some time.

from threading import Thread
import pyautogui as pg
import time, pyautogui
import keyboard

from pynput.keyboard import Key, Listener
pg.moveTo(2679, 900)
pg.click()
pressed = False
StopCheckingForKey = False

def loop_function():
    global pressed, StopCheckingForKey
    for i in range (10):
        pg.moveTo(2679, 1620, duration=2)
        pg.moveTo(2679, 1923, duration=2)
        pg.moveTo(1365, 1875, duration=2)
        pg.moveTo(1365, 1572, duration=2)
        if pressed == True:
            pressed = False
        else:
            pg.keyDown('ctrl')
            pg.keyDown('w')
            pg.keyUp('w')
            pg.keyUp('ctrl')
    StopCheckingForKey = True

def check_key():
    global pressed, StopCheckingForKey
    while not StopCheckingForKey:
        if keyboard.is_pressed('right arrow'):
            pg.keyDown('ctrl')
            pg.keyDown('w')
            pg.keyUp('w')
            pg.keyUp('ctrl')
            pressed = True


t1 = Thread(target=loop_function)
t1.setDaemon(True)
t1.start()

t2 = Thread(target=check_key)
t2.setDaemon(True)
t2.start()
Related