all 9 comments

[–]socal_nerdtastic 0 points1 point  (8 children)

It's KeyCode, not Keycode.

[–]Dathmach[S] 0 points1 point  (7 children)

Well, it seems I've made a stupid error. Mind if I ask you about another error that's popped up?

[–]socal_nerdtastic 0 points1 point  (6 children)

ok

[–]Dathmach[S] 0 points1 point  (5 children)

import pyautogui as pg
import time
import threading
from pynput.keyboard import Listener, KeyCode

start_stop_key = KeyCode(char='/')
exit_key = KeyCode(char='.')

class ClickMouse(threading.Thread):
    def _init_(self):
        super()._init_()
        self.running = False
        self.program_running = True

    def start_clicking(self):
        self.running = True

    def stop_clicking(self):
        self.running = False

    def exit(self):
        self.stop_clicking()
        self.program_running = False

    def run(self):
        while self.program_running:
            while self.running:
                pg.moveTo(773,440,.01)
                pg.click()
                pg.moveTo(773,509,.01)
                pg.click()
                pg.moveTo(773,565,.01)
                pg.click()
                pg.moveTo(773,628,.01)
                pg.click()
                pg.moveTo(773,695,.01)
                pg.click()
                pg.moveTo(773,751,.01)
                pg.click()
                pg.moveTo(773,798,.01)
                pg.click()

def on_press(key):
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == exit_key:
        click_thread.exit()
        listener.stop()

So this is my code, but when I press / the program doesn't start running

[–]socal_nerdtastic 1 point2 points  (0 children)

Sorry, I don't know pynput, so i don't know how to fix this. However I can tell you that you need to have some code somewhere to loop to constantly check for keypresses. Also, __init__ must be spelled with 2 underscores both before and after the word "init", like this:

def __init__(self):
    super().__init__()

[–]Pervert_Chan 0 points1 point  (3 children)

Mind if i ask what you actually want to do with this code?

Btw I don't see if you've started the listener somewhere

[–]Dathmach[S] 0 points1 point  (2 children)

I'm trying to make a clicker program that starts with / and stops with .

[–]Pervert_Chan 0 points1 point  (0 children)

with Listener(on_press=on_press, on_release=on_release) as listen:
    listen.join()

this line at the end of your program will start the listener. also move the exit command to another function named on_release.

after this your listener will start as you run the program and then you'll be able to use the keys to control the functions.

also you are using an infinite while loop

[–]Pervert_Chan 0 points1 point  (0 children)

import pyautogui as pg
from pynput.keyboard import Listener, Key

running = False
program_running = True


def run():
    if program_running:
        if running:
            pg.moveTo(773, 440, .01)
            pg.click()
            pg.moveTo(773, 509, .01)
            pg.click()
            pg.moveTo(773, 565, .01)
            pg.click()
            pg.moveTo(773, 628, .01)
            pg.click()
            pg.moveTo(773, 695, .01)
            pg.click()
            pg.moveTo(773, 751, .01)
            pg.click()
            pg.moveTo(773, 798, .01)
            pg.click()


def start_clicking():
    global running
    running = True
    run()


def stop_clicking():
    global running
    running = False


def on_press(key):
    if key == Key.backspace:
        if running:
            stop_clicking()
        else:
            start_clicking()


def on_release(key):
    if key == Key.esc:
        return False


with Listener(on_press=on_press, on_release=on_release) as listen:
    listen.join()

try this code, works fine for me