I am trying to make it where you can move a mouse on a website using python

23 Views Asked by At

I am making a project where i can hit a button on a website and it will run python code that moves your mouse with "WASD" but when i try and use ngrok to let others use it, it does not move i thought it had somthing to do with pip installing things but i am not quite sure please help

my current code:

testing.py

import keyboard
import pyautogui as gui
import webbrowser
import turtle


inp = gui.prompt("Give me a link")


list = []


def movemouse(x=1, y=4):
    if keyboard.is_pressed("shift"):
        x //= 2
        y //= 2
    elif keyboard.is_pressed("alt"):
        x *= 4
        y *= 4
    list.append(1)
    gui.move(x, y)
    print(len(list))
    
#down movemouse(0, 45)
#up movemouse(0, -45)
#left movemouse(-45, 0)
#right movemouse(45, 0)

def mu():
    movemouse(0, -45)

def md():
    movemouse(0, 45)

def mr():
    movemouse(45, 0)

def ml():
    movemouse(-45, 0)




def click():
    list.append(1)
    gui.click()
    print(len(list))


linkstuff = ["www", "http", "com", "net", "gov", ".io", "website", "to", "tv"]


def testclick():
    for i in linkstuff:
        if i in inp:
            webbrowser.open(inp)
            break
        else:
            webbrowser.open("https://www.youtube.com")
            list.append(1)
            print(len(list))

def byebye():
    turtle.Screen().bye()

def main():
#    keyboard.on_press_key("s", lambda _: movemouse(0, 45))
#    keyboard.on_press_key("w", lambda _: movemouse(0, -45))
#    keyboard.on_press_key("a", lambda _: movemouse(-45, 0))
#    keyboard.on_press_key("d", lambda _: movemouse(45, 0))
#    keyboard.on_press_key("e", lambda _: click())
#    keyboard.on_press_key("q", lambda _: testclick())
#    keyboard.wait("esc")  # Press 'esc' to exit
    turtle.Screen().onkeypress(mu, "w")
    turtle.Screen().onkeypress(md, "s")
    turtle.Screen().onkeypress(ml, "a")
    turtle.Screen().onkeypress(mr, "d")
    turtle.Screen().onkeypress(click, "e")
    turtle.Screen().onkeypress(testclick, "q")
    turtle.Screen().onkeypress(byebye, "Escape")


if __name__ == "__main__":
    main()




turtle.Screen().listen()
turtle.Screen().mainloop()

testweb.py

import threading
from flask import Flask, render_template, request
from testing import main

app = Flask(__name__, static_url_path='/static')

@app.route('/')
def index():
    return render_template('testweb.html')

@app.route('/runcode')
def run_code():
    try:
        t = threading.Thread(target=main)
        t.start()
        return 'Code executed successfully', 200
    except Exception as e:
        return str(e), 500

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

testweb.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Run Code</title>
</head>
<body>
    <button onclick="runCode()">Run Code</button>
    
    <script>
        function runCode() {
            fetch('/runcode')
                .then(response => {
                    if (response.ok) {
                        console.log('Code executed successfully');
                    } else {
                        console.error('Error:', response.statusText);
                    }
                })
                .catch(error => console.error('Error:', error));
        }
    </script>
</body>
</html>

I tried a different way of reading keys (turtle) but it still doesn't move the mouse on different devices.

0

There are 0 best solutions below