I'm trying to make a script for stardew valley, the mouse moves correctly and all the other stuff too, but when i use the click function it doesn't work
My code:
from PIL import ImageGrab
from PIL import ImageOps
import pydirectinput
import pyautogui
import keyboard
import time
from numpy import *
test1 = (1400, 720)
test2 = (890, 420)
store = (1150, 420)
time.sleep(1.5)
def main():
# pyautogui.write("t")
# pyautogui.write("/emote angry")
# pyautogui.press("enter")
pyautogui.moveTo(store)
time.sleep(1)
pyautogui.rightClick()
pydirectinput.rightClick()
main()
i've tried with both modules but none of them worked for me, and i have tried with other games and the click works
Related
I've been trying to make a simple script, it presses the key when the image of the key is visible in the left bottom corner of the screen.
The problem is when I'm in the game (Multi Theft Auto) the script stops working only in the game window. I tried running script/game as administrator, changing resolution, making the game windowed/fullscreen but nothing changes. Here's my code:
from numpy import random
import pyautogui
import pydirectinput
import time
import keyboard
import random
import win32api, win32con
sleeptime1 = random.uniform(1.05, 2.03)
sleeptime2 = random.uniform(1.04, 2.01)
sleeptime3 = random.uniform(1.02, 2.05)
while True:
if pyautogui.locateCenterOnScreen('key_a.png', region=(0,580,500,500), grayscale=True, confidence=0.7) != None:
pydirectinput.press('e')
time.sleep(sleeptime1)
if pyautogui.locateCenterOnScreen('key_b.png', region=(0,580,500,500), grayscale=True, confidence=0.7) != None:
pydirectinput.press('q')
time.sleep(sleeptime2)
else:
pydirectinput.click()
time.sleep(sleeptime3)
just run your game as administrator like your python, that should help.
As I can simulate a click as if it were made from the mouse, a game has detection of click events or simulated keys, I would like to know if it is possible to simulate a real click.
Translated.
My code:
import time
import keyboard
import pyautogui
from playsound import playsound
pyautogui.PAUSE = 0.2
sW, sH = pyautogui.size()
down = False
while True:
if keyboard.is_pressed('f4'):
if not down:
down = True
playsound('on.mp3')
pyautogui.moveTo(sW*0.67, sH*0.5)
pyautogui.click()
pyautogui.moveTo(sW*0.67, sH*0.6)
pyautogui.click()
pyautogui.moveTo(sW*0.67, sH*0.64)
else:
down = False
This code simulates a real click of the mouse.
import win32api, win32con
from time import sleep
def click(x,y, duration=30, delay=0.3):
win32api.SetCursorPos((x,y))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)
sleep(duration / 1000)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
sleep(delay)
Why?
Because the lib win32api is directly connected with the windows commands.
When u click with the mouse, you actualy triggers a function inside the windows.
win32api does de same.
Also...win32api is the fastest comunication with the windows for this stuff, like image rendering
I have created a simple OCR with python to recognize text. I want this function to run when a global hotkey is pressed even if the focus is not on the app. How can I achive this?
Code:
#!/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
import pytesseract
from PIL import Image
import os
import rumps
import pyperclip
#rumps.clicked('OCR')
def ocr(sender):
os.system('screencapture -i %s' % '/Users/jerryhu/Desktop/capture.png')
img = Image.open('/Users/jerryhu/Desktop/capture.png')
text = pytesseract.image_to_string(img)
pyperclip.copy(text)
rumps.Window(message='OCR Capture', default_text=text, title='Text Copied').run()
os.remove('/Users/jerryhu/Desktop/capture.png')
app = rumps.App('OCR', menu=[
'OCR',
None
])
app.run()
It's better to use a global hotkey instead of rumps for this project.
I am Trying to set an image as the background of my Tkinter window and everything I try doesn't seem to work. Here's the code I have:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
window.configure(background = '#007501')
If you want the background to be an image (without using canvas), use labels:
import random
import Tkinter # note use of caps
from Tkinter import *
from PIL import Image, ImageTk
import os.path
window = Tk()
window.title('Ask If You Dare')
photo=PhotoImage(file="path/to/your/file")
l=Label(window,image=photo)
l.image=photo #just keeping a reference
l.grid()
I tried this way but it is not working as expected,
import win32api
import win32con
import time
time.sleep(3)
win32api.keybd_event(win32con.VK_F5,0x74,0,0)