randomize the time from time.sleep - python

I'm fairly new to python and have been trying to write a script that automates a string of text. The problem is that i don't know how to randomize the time.sleep function. I have also tried adding the time in range function but still can't get it to randomize it.
The code looks like this at the moment. I have to get rid of the time.sleep(40) to randomize it
import time
from pynput.keyboard import Key, Controller
while True:
keyboard = Controller()
time.sleep(40)
for char in "nnnn":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.21)

Use the random module
import random
import time
from pynput.keyboard import Key, Controller
while True:
keyboard = Controller()
sleep_time = random.randrange(0, 100)
time.sleep(sleep_time)
for char in "nnnn":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.21)

Related

How to make the script press return with pynput

I just started learning python and I have made this script that types for me, but I couldn't figure out how to make it press the enter key.
import time
keyboard = Controller()
time.sleep(3)
for char in "Hello, this isn't notking but this is a bot, he has been learning python and learned this script that types for him!":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.12)```
This should help you with your problem. Next time please provide the whole code related to the question.
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
time.sleep(3)
for char in "Hello, this isn't notking but this is a bot, he has been learning python and learned this script that types for him!":
keyboard.press(char)
keyboard.release(char)
time.sleep(0.12)
keyboard.press(Key.enter)
keyboard.release(Key.enter)

Is there a way to edit the maximum CPS in pynput?

So recently I've been using pynput and I have noticed that no matter the delay I set it as the maximum CPS with pynput is always 10 cps. Now in pyautogui you can edit the hard coded pause between clicks, but I was wondering if this is possible in pynput.
For example:
import pynput
from pynput.mouse import Button as btn
from pynput.mouse import Controller
import time
mouse = Controller()
delay = 0
mouse.click(btn.left)
time.sleep(delay)
No matter what you change the "delay" value to, the max CPS is always 10 with Pynput, and trying "pynput.PAUSE = 0.01" doesn't change the output.

I am trying to develop a keyboard spammer and auto clicker but i cannot find anywhere a way to stop the spammer with a keybind

for now I'm just trying to get it to work with the keyboard spammer. So far I have done much searching but nothing seems to work.
if you have any tips on how to make the spamming stop without having to close the application it would be appreciated.
This is the code:
from typing import KeysView
import keyboard
import time
keyboard.wait("esc")
x = 4
while x != 5:
for i in range(9):
time.sleep(0.4)
print(keyboard.send("ctrl+v,enter"))
time.sleep(6)
(thanks)
Using the keyboard module there are many ways you could do this, one of them would be to add a hotkey that stops your loop via a global variable (in this example it's when you hit the spacebar):
from typing import KeysView
import keyboard
import time
keyboard.wait("esc")
running = True
def on_space():
global running
running = False
keyboard.add_hotkey('space', on_space)
while running:
for i in range(9):
time.sleep(0.4)
print(keyboard.send("ctrl+v,enter"))
time.sleep(6)
Adding a check for if a key is pressed inside of the for loop should do the trick. You could either make pressing the key break the loop, or since your loop waits for x to equal 5, you could simply make pressing the key change x to 5.

python keyboard.press not working with other applicatons

I have made a python script that presses space once a second so I can go AFK in games and not get kicked for being idle. I tested it in notepad and it works but when i open Minecraft or Roblox nothing happens.
I did try looking for an answer but couldn't find anything
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
while True:
time.sleep(1)
keyboard.press(' ')
keyboard.release(' ')
Sometimes games won't recognize a super-short key press. You essentially have a space press of less than a microseconds. Try adding a delay between the press and release.
from pynput.keyboard import Key, Controller
import time
keyboard = Controller()
while True:
time.sleep(1)
keyboard.press(' ')
time.sleep(0.15)
keyboard.release(' ')
you should probably run the cmd as administrator and put in some delay for keys to be registered. play around with the values of the delay to find one that suits your need
from pynput.keyboard import Key, Controller
import time
import keyboard
keyboard = Controller()
while 1:
if keyboard.is_pressed('r'):
while True:
time.sleep(1)
keyboard.press('space')

While loop waiting for input python

I have a question about while loops in python.
I want to make a program that performs a while loop in a certain time.I want to add the extra feature that while the program us running,a certain variable can be changed by pressing a random key.
from time import sleep
import time
i=0
a=0
while i<10:
i=i+1
i=i+a
a=a+1
time.sleep(1)
print i
I want to do it that the variable a can be reset to 0 by pressing any key.The loop should continue unchanged if no button is pressed.What command should i add?
Thanks
Edit: I tried:
import pygame
from pygame.locals import *
import time
i=0
a=0
pygame.init()
while i<10:
pygame.event.get()
i=i+a
print i
keys = pygame.key.get_pressed()
if keys[K_ESCAPE]:
i=0
i=i+1
time.sleep(1)
pygame.quit()
But now nothing happens when I press a button.What did i miss?
What you need is a non-blocking input function
while i<10:
keys = pygame.key.get_pressed()
etc
...
pygame has all sorts of event stuff built in so doing all the hard work of threading yourself shouldn't be necessary.
If that doesn't work for you check this out: http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/
You can use curses.Excellent doc is here:
http://docs.python.org/dev/howto/curses.html#user-input

Categories