Making A Bot In Python - python

I was making a bot that would basically keep the left mouse button clicked or unclicked based on a toggle. I get it to work but then it started lagging my entire computer so I didn't take it one step further, being scared to burn my PC, how It happened to my phone one year ago. So we are finally here asking you guys for some optimization to my project.
Code:
import keyboard
import win32api, win32con
from pynput.mouse import Button, Controller
mouse = Controller()
play=False
def toggle():
global play
if play==False:
play=True
else:
play=False
keyboard.add_hotkey('home',toggle)
played=False
while True:
if play==True and played==False:
played=True
mouse.press(Button.left)
elif play==False:
mouse.release(Button.left)
played=False
else:
pass

You should put a time.sleep() or something to wait between each loop because your while statement is running again and again without any pauses.

You can slow the while loop by inserting a sleep function in it. To do this, import the module time and call the function time.sleep(ms) in your while loop. It will be less reactive but you can set a sleep time of only a few ms and it will be better because the program will not be running at full speed all the time.

Related

Simulating Keys In Video Games

I want to code a bot that walks for me in a game called Minecraft To Explore All Of The Biomes. I've tried this code:
import pyautogui
import time
pyautogui.keydown("w")
time.sleep(8888)
pyautogui.keyUp("w")
print("Exploration Finished")
But it doesn't even move how can I fix it?
This is because you have inserted a time.sleep() code which will wait for 8888 seconds and then finish your code, resulting nothing. So instead, I put a for loop to fix your error:
from pyautogui import *
for i in range(8888):
keyDown("w")
keyUp("w")
print("Exploration Finished")
You can use the time.sleep() function to go to your game, start the game etc...
Please tell if this worked for you

Making keyboard shortcut, but it does not do anything

I am trying to make a shortcut to open google when I do the function normal it works but when I try to do it in a shortcut it doesn't work.
import pyautogui as pg
import keyboard
hotkey1 = "ctrl+alt+w"
def google():
pg.hotkey("win")
pg.typewrite("google\n", 0.05)
while True:
if keyboard.is_pressed(hotkey1):
google()
You only need a pause until pg can take over. It so happens that when you press the hotkey combo, you might accidentally hit some other keys and therefore it doesn't work as intended. This should give it enough time for the key to be depressed before the automatic process starts.
import time
def google():
time.sleep(0.25)
pg.hotkey("win")
pg.typewrite("google\n", 0.05)

Pynput and pyAutoGui can't hold keys

I wanted to make a simple macro to hold down 'W' for some time, but even simple script like this does not work.
import time
import pyautogui
from pynput.keyboard import Key, Controller
keyboard = Controller()
pyautogui.keyDown('w')
time.sleep(3)
pyautogui.keyUp('w')
time.sleep(5)
keyboard.press('w')
time.sleep(3)
keyboard.release('w')
If i test it in any text editor/text input window it will write one 'w' when script starts and anouther one after 8 seconds without holding/spaming it. And therefore it DOES NOT work in any games what should be the whole purpose of this script. I tried it in a huge variety of different games (Java Minecraft, source Gmod, Roblox and some unity games) and this script just was not working in any of them, but if a game has chat, search box or any other text input window, this script will write one 'w' and anouther one after some time in it.
I realy have no idea why this is happening, but i remembered that two years ago i tried to make similar script on pynput and it did work, so i tried installing old versions of pynput, but that did not help me as well...
So after a long time i could finaly take care of the issue, and i figured out that the source of the problem was in the Microsoft's DirectInput. Basically pynput and pyAutoGUI are not able to get in to DirectInput and there is no signal at all while I thought that there wwas a signal, but for so little amount of time that the games just were not able to pick it up. And the solution to this was pretty easy thanks to this guy PyAutoGUI not working? Use DirectInput. Thanks to his PyDirectInput librarry you are able to use python to emulate button presses and holdes in games!
This might be a start to work with:
#https://stackoverflow.com/questions/66284097/pynput-and-pyautogui-cant-hold-keys
#simulate keystroke for some amount of time
import msvcrt
import time
def press(char, duration, sleep, limit=None):
while True:
lim = limit
t1 = time.time()
while time.time() - t1 < duration: # do for duration specified
msvcrt.putch(char) # output character, putch() awaits a byte string
if lim: # limit specified?
lim -= 1
if lim == 0: # limit reached
break
time.sleep(SLEEP)
if msvcrt.kbhit(): # key pressed?
char = msvcrt.getch()
press(b'w', .001, 2, None)

Is there a way to mimic the effect of the time.sleep() function without pausing the entire program?

I am a new python 3.9 user who's trying to step up my coding. I'm writing a program that uses a while loop and will call functions to press keys based on user input. My code basically looks like:
from pynput.keyboard import Key, Controller
from graphics import *
import time
def wack():
M = Controller()
for i in range(3):
M.press('a')
time.sleep(1) # This guy right here is the issue
M.release('a')
def main():
click = Win.checkMouse()
while not click(click, exit_box):
click = Win.checkMouse()
if *input* == *correct string*:
wack()
I have a graphics window with an exit button and when exit is clicked, it terminates the program and closes the window. The issue is that the time.sleep(1) doesn't pause the inputs or that point in the code, it pauses the entire program suspending the while loop which is not what I want it to do.
Searching about the time function hasn't provided any clear solutions since time.sleep() seems to be the most popular pausing function in python.
asyncio is an amazing feature added in python3.7 which allows you to wait for an object or function(callled a coroutine in the context of asynchronous python) to get a result while the rest of the program executes.
asyncio has a sleep method which paused only the asynchronous function(coroutine) and lets the rest of the code in the program execute
This is how:
from pynput.keyboard import Key, Controller
from graphics import *
import time
import asyncio
#This is the asnychronous funtion
async def wack():
M = Controller()
for i in range(3):
M.press('a')
await asyncio.sleep(1)#this sleeps the function but allows the rest of the program to work
M.release('a')
def main():
click = Win.checkMouse()
while not click(click, exit_box):
click = Win.checkMouse()
if *input* == *correct string*:
asyncio.run(wack()) #calling the asynchronous funtion
Here is a tutorial on understanding asyn io for python

How do I make get_pressed to work in pygame?

Despite hunting around I can't seem to find an answer to this seemingly simple question:
I'm new to pygame (but not to python), and am trying to get some code to work from continuous button presses - but get_pressed just does not seem to work for me. I made this just to check that I wasn't going insane (I've left out the importing to make it neat for you guys):
def buttonpress():
while True:
keys = pygame.key.get_pressed()
print keys[K_SPACE]
time.sleep(0.5)
buttonpress()
To the best of my knowledge, this should return a '1' when you press the space bar, but no matter what key you change it too - it simply returns an endless string of zeros.
What am I missing?
Thanks
There is no code that processes the input to get all the keys pressed. In order for this to work you need to call event.poll().
So your code will look like this.
import pygame
from pygame.locals import *
import time
pygame.init()
screen = pygame.display.set_mode((640,380))
def buttonpress():
while True:
keys = pygame.key.get_pressed()
print (keys[K_SPACE])
time.sleep(0.5)
pygame.event.poll()
buttonpress()
One more thing, do not use time.sleep(). This pauses the thread, and can cause the OS to think that your application does not respond (since it's not removing events from the event queue).

Categories

Resources