Python Automated Click Script - python

I am working on a program to make python auto click and type something in. I know this has been done before and asked before but no one has asked about recording mouse clicks to be "Played back" later on. I have the basic code set up from tutorials all over the place. I am wondering if I can get a hand with this. Here is what I have to this point:
import win32api, win32con
import time
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
print "Clicking 300, 300"
click(300,300)
time.sleep(3)
print "Clicking 800, 800"
click(800, 800)
How do I make this so the user can input and save a pre-generated script for clicks?

Well, I don't have any experience with the Win32 API, however, it should work along those lines:
The Module you are using needs to let you define a callback method for when a click happens
You somewhere set a boolean that tells you that you are currently recorded.
Your callback method stores tuples in a list:
The tuples store the timestamp (time.time) and the coordinates.
You can even store more information, like right-click or whatever.
When you are done recording you should have every informationen necessary to start replaying :)
(You may also consider this post)
Hope it helps!

Related

Automate to click 'down' key and take screenshot

I am newbie who wants to learn python. English is not my primary language, please ignore grammer error.
Here's my question.
In tradingview charts, i want to take screenshot, click down button, take screenshot again, wait for 3 second then go to next stock.
As i am newbie on python as well as stackoverflow if my any action fell dumb or wrong then please give me guidence.
Keyboard module (python) to control keyboard
first we need install a module name- keyboard in python
pip3 install keyboard
First, let's import the module:
import keyboard
Next, you can also simulate key presses using the send() function:
it will press space:
keyboard.send("space")
This will press and release the space button. In fact, there is an equivalent function press_and_release() that does the same thing.
You can also pass multi-keys:
keyboard.send("windows+d")
The + operator means we press both buttons in the same time, you can also use multi-step hotkeys:
send ALT+F4 in the same time, and then send space,
keyboard.send("alt+F4, space")
But what if you want to press a specific key but you don't want to release it ? Well, press() and release() functions comes into play:
press CTRL button
keyboard.press("ctrl")
release the CTRL button
keyboard.release("ctrl")
So this will press CTRL button and then release it, you can do anything in between, such as sleeping for few seconds, etc.
But now what if you want to write a long text and not just specific buttons ? send() would be inefficient. Luckily for us, write() function does exactly that, it sends artificial keyboard events to the OS simulating the typing of a given text, let's try it out:
keyboard.write("Python Programming is always fun!", delay=0.1)
Setting delay to 0.1 indicates 0.1 seconds to wait between keypresses, this will look fancy like in hacking movies!
Saving your problem
note- in windows 10, hot key for tacking screen shot is "windows+PrtScn.
Use your own hot key
from selenium import webdriver
import keyboard
import time
driver = webdriver.Chrome(executable_path="C:\\Users\\hp\\Desktop\\webcontrol\\chromedriver.exe")
driver.get("url ypu want to reach")
keyboard.send("windows+PrtScn")
keyboard.send("down")
keyboard.send("windows+PrtScn")
time.sleep(3)
driver.get("next stock url you want")
note- for going to next stock you can also use buttons or you can use selenium to click that particular stock
THANKS!

How to not immediately refresh after user input curses

I am writing a DoodleJump game for the terminal in Python with curses. I use getch() to get the user input to know in which direction the character should move. When I get the user input my environment gets updated simultaneously but I want that it just keeps on moving and not update when I get the user input.
Does anybody know how I can achieve this? I tried using nodelay() but that still doesn't cut it. Thanks in advance
please try with:
time.sleep(seconds)
This will wait the seconds you want before continue.
Sources:
https://www.pythoncentral.io/pythons-time-sleep-pause-wait-sleep-stop-your-code/
Hope it help, have a nice day,
David.
curses refreshes the window associated with getch. You could prevent refreshes (or hide them) by turning off echo, and using a window that won't have changes made to it (even one that you create just for that purpose).

Running Python clicker based on keylogger inputs on the background in Windows

I wanted to write a program in Python for Windows that would act as a clicker, in which according to a key the user presses a click is made at a known location on the screen. This is used for an automated option selection from a list in a webpage. I have the clicking part working, but I wanted to be able to make several clicks during execution, as if there is a quiz with multiple lists one after another.
One option is to make a while loop with getch() from msvcrt. The thing is after a click outside the cmd its window is no longer selected, but rather the window where the destination point is located. Therefore, the script stops being active and the user cannot choose another location. A workaround is to click the cmd window to return the focus to it and be able to do any more clicks. To solve this, it would be necessary to create a service or, according to #Sanju, a thread.
The other option is to use a keylogger such as PyHook, which seems like the way to go. However, the problem is that the window where I want to use it in, a webpage in flash or another animations engine, causes an error that some users have found using this keylogger for example in Skype and is being described here. In my case, it also happens with this webpage and either when the click is made on the window itself or when the key is pressed with the window selected.
My base code is presented below, where click(...) would normally contain the coordinates as argument but they are being omitted for simplicity. In this case, 0 ends the program and there are three options being chosen with the numbers 1-3.
import msvcrt, win32api, win32con
def click(x,y):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
key=0
while key!=b'0':
key=msvcrt.getch()
if key==b'1':
click(...)
elif key==b'2':
click(...)
elif key==b'3':
click(...)
The attempts below try to implement #Sanju's suggestion, first with the whole while inside the thread and then with the queue, both not working as expected...
import threading, msvcrt, win32api, win32con
def MyThread():
key=0
while key!=b'0':
key=msvcrt.getch()
if key==b'1':
...
def click(x,y):
...
threading.Thread(target=MyThread,args=[]).start()
.
import queue, threading, msvcrt, win32api, win32con
def MyThread(key):
while key.get()!=b'0':
key.put(msvcrt.getch())
if key.get()==b'1':
...
def click(x,y):
...
key=queue.Queue()
key.put(0)
threading.Thread(target=MyThread,args=[key]).start()
The other attempt uses PyHook, but it's still facing the aforementioned issue.
import pyHook, pythoncom, win32api, win32con
def OnKeyboardEvent(event):
if event.Key=='Numpad1':
...
def click(x,y):
...
hm=pyHook.HookManager()
hm.KeyDown=OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
All you need here is move your click part to a thread and share the user input using a shareble object such as queue. It sounds like a overkill , but that's the way to keep your tasks in background.
And BTW, you have many GUI application frameworks available in Python like tkinter ,wxpython which can ease your objective.

Need to send key presses to a webpage(html5 game)

Trying to build a bot for experimenting with A.I for a webpage. The webpage in question is a game(HTML5).
I want to send keys (up, down, left, right, space)to an externally opened webpage to control a bot in the game.
I looked into mechanize, but it feels to me that its constructed for forms and stuff.
BTW, i'm taking A.I. right now, hence the curiosity.
Any help would be appreciated. Thank You.
Depending on how you want to approach this, and assuming you're on Windows, the guide here suggests that you just use The Python Imaging Library (for reading the screen), Numpy and PyWin (for actually clicking on the game). The equivalent packages for Win64 may be available here
To directly answer your question though:
import win32api, win32con
def leftClick():
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
time.sleep(.1)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)
print "Click." #completely optional. But nice for debugging purposes.
Selenium also offers something similiar to Mechanize in that you get control of a browser, although it has to be launched by Selenium itself, it can't just hook into a currently running process. You're able to send a click event directly to an HTML element, which might be just what you're looking for here.

Close an easygui Python script with the standard 'close' button

I've created a very simple app, which presents an easygui entrybox() and continues to loop this indefinitely as it receives user input.
I can quit the program using the Cancel button as this returns None, but I would also like to be able to use the standard 'close' button to quit the program. (ie. top right of a Windows window, top left of a Mac window) This button currently does nothing.
Taking a look at the easygui module I found this line:
root.protocol('WM_DELETE_WINDOW', denyWindowManagerClose )
This would seem to be the culprit. I'm no TKinter expert but I could probably work out how to alter this handler to act the way I want.
However, as I'd rather not mess about with the easygui module, Is there a way to override this behavior from my main script, and have the close button either close the program outright or return None?
It would require altering the easygui module, yes. I will get it modified!
** I have sent in a e-mail to the EasyGUI creator explaning this [12:12 PM, January 23/09]
** I just want to say that the possibility of this change happening - if at all, which I doubt - is very tiny. You see, EasyGUI is intended to be a simple, discrete way to create GUIs. I think that this addition wouldn't help any, especially since the interface is very sequential, so it would be confusing to new users. [12:19 PM, January 23/09]
** The EasyGUI creator said this in reply to my e-mail:
An easygui dialog should never exit
the application -- it should pass back
a value to the caller and let the
caller decide what to do.
But this is an interesting idea.
Rather than simply ignoring a click on
the "close" icon, easygui boxes could
return the same value that a click on
the "cancel" button would return.
I'll meditate on this.
-- Steve Ferg
I think that this is progress at least. [2:40 PM, January 23/09]
I don't know right now, but have you tried something like this?:
root.protocol('WM_DELETE_WINDOW', self.quit)
or
root.protocol('WM_DELETE_WINDOW', self.destroy)
I haven't tried, but google something like "Tkinter protocol WM_DELETE_WINDOW"
I found a solution, the answer is below the choice box, when he defines some functions (you can just type to find denyWindowManagerClose) and go to where he defines it. Just erase it and put this code in its place.
def denyWindowManagerClose():
#------------------------------------------------------------------
# Changed by ProgrammingBR
# Enables the user to close the window without entering a value
# Youtube URL: https://www.youtube.com/channel/UCTZh6kWz_iYACNE6Jcy2lhw
#------------------------------------------------------------------
global __enterboxText
__enterboxText = None
boxRoot.quit()
You can create a backup file, but this will work for all boxes, the suggestions given here aside from this will not work, I tried the them. if you want to see I have a video on it (it is in Portuguese, but you can follow it) just go to the youtube channel, I will upload it soon and post the link here :)

Categories

Resources