How to randomly simulate keypresses for special keys

36 views Asked by At

I'm trying to make an AFK keypress simulator which is run by a Hardware Random Number Generator. It works great so far, pressing keys, moving the mouse, and clicking and scrolling but I'm trying to have it press special keys such as enter and escape.

When I add this it keeps failing and automatically quitting.

Here is my code see prepare random keypress section:

import cv2
import random
import string
from pynput.mouse import Button, Controller as mouseController
from pynput.keyboard import Controller as keyboardController
from hashlib import sha512


####Variables####
jiggleMin = -200  #Min/max values for range used to generate the next mouse X/Y position relative to current position of mouse. Bigger range = more movement
jiggleMax = 200  #Play around with jiggleMin/jiggleMax in combination with smoothing_factor to change mouse movement style
jiggleChanceMin = 0 #Keep this at 0 to avoid crashes :D
jiggleChanceMax =500 #Default 1 in 100 chance that the mouse will jiggle. This will be overridden by the brightness calc once video is running
darknessThreshold = 15 #0-255. Pixel brightness threshold below which pixels are considered black. Higher threshold = more black detected = higher jiggle chance
brightnessThreshold = 150 #0-255. Pixel brightness threshold above which pixels are considered white. Lower this number if you want higher chance of movement in a bright scene.
smoothing_factor = 1 # Controls how much the mouse movement is smoothed
################f

#prepare mouse & keyboard controller
mouse = mouseController()
keyboard = keyboardController()

#Prepare random keypress
def press_random_key():
    keys = [char for char in string.ascii_letters + string.digits if char != 'q']
    spkeys = ['enter','up','down']
    key = random.choice(keys)
    spkey = random.choice(spkeys)
    print('pressing', key)
    keyboard.press(key)
    keyboard.press(spkey)
    #time.sleep(0.1)
    keyboard.release(key)
    keyboard.release(spkey)

#Prepare screen text
# font
font = cv2.FONT_HERSHEY_SIMPLEX
# org1
org1 = (50, 50)
# org2
org2 = (50, 100)
# org3
org3 = (50, 150)

# fontScale
fontScale = 2
# Blue color in BGR
blue = (255, 0, 0)
#Green color
green = (0,255,0)
# Line thickness of 2 px
thickness = 2

# Open the default camera
cap = cv2.VideoCapture(0)

#Prepare RNG
rng = random

# Check if the camera is opened
if not cap.isOpened():
    print("Cannot open camera")
    exit()

#Prepare the always-on-top window
cv2.namedWindow('frame', cv2.WINDOW_NORMAL) # create a named window and set the flag to WINDOW_NORMAL
cv2.setWindowProperty('frame', cv2.WND_PROP_TOPMOST, 1) # set the window property to always be on top

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Convert the frame to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    #Apply the brightness threshold to detect more/less pixels as black
    _, thresh = cv2.threshold(gray, darknessThreshold, brightnessThreshold, cv2.THRESH_BINARY)

    # Calculate the mean brightness of the frame, factoring in the thresholded pixels
    mean_brightness = cv2.mean(thresh)[0]

    #Set the jiggle probability based on the mean brightness
    if mean_brightness > 0.0:
        jiggleChanceMax = int(mean_brightness)

    #get hash from frame
    digest = sha512(frame).digest()

    # Generate random numbers with hash as seed
    rng.seed(digest)
    random_number1 = rng.randint(jiggleMin, jiggleMax) ##For X coord
    random_number2 = rng.randint(jiggleMin, jiggleMax) ##For Y coord
    random_number3 = rng.randint(jiggleChanceMin, jiggleChanceMax) ##For jiggle chance with a min:max ratio probability

    #Show text colour as green if we hit our 1 in 100 chance to move mouse
    textColour = blue
    if random_number3 >= jiggleChanceMax:
        textColour = green

    frame = cv2.putText(frame, str(random_number3), org1, font, 
                   fontScale, textColour, thickness, cv2.LINE_AA)

    frame = cv2.putText(frame, str(digest), org2, font, 
                   1, textColour, thickness, cv2.LINE_8)
    
    frame = cv2.putText(frame, str(jiggleChanceMax), org3, font, 
                   1, textColour, thickness, cv2.LINE_8)
    

    # Display the frame
    cv2.imshow('frame', frame)

    if random_number1 and random_number2 and random_number3 is not None:
        #Move the mouse to x/y coords from random numbers
        #TODO use a coroutine for longer jiggle periods
        if random_number3 >= jiggleChanceMax:
            #Get current mouse position
            x_pos, y_pos = mouse.position

            # Update the mouse position with smoothing
            x_pos += int((random_number1 - (x_pos - mouse.position[0])) * smoothing_factor)
            y_pos += int((random_number2 - (y_pos - mouse.position[1])) * smoothing_factor)
            mouse.move(x_pos - mouse.position[0], y_pos - mouse.position[1])
            mouse.click(Button.left)
            mouse.scroll(0, -2)
            press_random_key()
            
    if cv2.waitKey(1) == ord('q'):
        print('q pressed, exiting...')
        break


# Release the capture
cap.release()
cv2.destroyAllWindows()


0

There are 0 answers