I made script for browser game. In this game, I am standing still and my script clicks on certain places bksononechno, but I need that if I clicked on the letter k This script would kill itself and not try to close the browser and its tab in the browser and something similar.
Sorry for mistakes.
You can add sys.exit(1) in the program itself and do Ctrl-C in the command line.
Related
I am searching for a way to close a specific window of a program immediately after it opens. Specifically, I am using the BUFF addon for Overwolf and after every game I play, this annoying overlay window opens up and blocks part of my screen. To close it windows Alt+Tab'ing out, I need to wait 10 seconds (that means if I'm not willing to subscribe to BUFF premium), and Alt+Tab'ing out after every game is just as annoying.
Is there any way that I can run a Python script while I'm playing that closes this window every time it opens immediately? I'm thinking of something that continously searches for a window with a specific name or maybe something that listens for the event of a window opening and then checking its name/title. I don't even know if Python can access these processes, but I guess it's worth to just ask.
And before I need to edit the post, I'm using Windows 11.
So I've been attempting to get a script to type out a string of text in a video game (Guild Wars 2). Mainly I'm using pyautogui and for the most part it works fine. My issue is it seems I can't get the game to recognize 'enter'. For example if I have the code:
import pyautogui, time
time.sleep(2) #to allow me to have time to switch windows
pyautogui.press('enter')
pyautogui.typewrite("This is a test")
pyautogui.press('enter')
The two "press enter" function will not open and submit the text. If however I manually hit enter, the 3rd line types things out just fine.
I've also tried replacing press('enter') with keyDown followed by keyUp, but with still no results.
I've managed to create a workaround by having python hit F10, and then a separate Autohotkey script hitting enter when F10 is hit, but that is far from ideal. Are there any suggestions?
Extra note from comments: The script by itself works fine in other programs such as notepad. It seems to fail exclusively for the game client.
For anyone finding their way to this post. The answer is essentially that this can't be done in this fashion due to how most games interpret keypresses. A working system is shown here: Simulate Python keypresses for controlling a game
None of \n nor return works to me, so I have to use pydirectinput: https://github.com/learncodebygaming/pydirectinput
Install it using pip install pydirectinput, then import it and use as the README. Note that the file also needs to run as admin.
I'm learning python and playing with the subprocess module. After reading several different tutorials and descriptions of how to use the module, my understanding was still not incredibly solid so I started working up some simple code to test the various functions and see how everything worked.
I wanted to see how it might behave if I tried to start the same process multiple times, using this code:
import subprocess
def tpltest(x):
while x > 0:
try:
subprocess.Popen('wordgrinder')
except:
print 'something broke!'
x -= 1
x = raw_input('how many?')
tpltest(x)
When I ran this in my terminal, wordgrinder (terminal word processor) opened normally, but then the cursor advanced down the screen at a rate of about one line per second. It appeared to be movement only, rather than any characters being added to the blank file onscreen. I pressed ctrl+c to see if a wordgrinder remained after closing the first instance and was met with my regular command line prompt, but the cursor continued to scroll. I ran top and did not see extra wordgrinder instances open, but the cursor continued scrolling.
Then I discovered the oddest part. If I rolled my mouse scroll wheel up in the window, chunks of text appeared over the window's contents. Some of the chunks were commands I had entered in the console over the past week, some of them were commands I had entered in my python shell but not the regular terminal, and some of the text was my WiFi security information, including the plaintext WPA2 password for my network. Scrolling the mousewheel down had no effect but scrolling up would overwrite a few lines with new text, seemingly from other random places in my system. After closing the terminal window and opening a new one, things were back to normal.
I'm running Debian 8 64bit with XFCE and using the default terminal emulator.
What did my code do, and why?
The explanation that I can offer is that what subprocess does is to run a background process. Additionally, wordgrinder is a program that takes full control of the terminal session by capturing input from the keyboard (stdin) and controlling the command-line (stdout) in a very particular way.
When you ran wordgrinder in the background through your script, it caused some funky things to happen. That's pretty crazy that your WiFi password ended up getting displayed from your experiment.
I'm running a python script that takes a data measurement once every 10 minutes forever. The problem is that it requires I run a httpget.exe that crashes constantly. When it crashes, a popup window appears and tells me that it's "Not Responding" and puts my python script on hold until I press "enter" in the popup or click "ok", then the script continues normally.
Is there a way to make certain programs in windows 7 not have those popup menus appear? So my script would just run over any "Not Responding" errors?
I've tried running a "kill()" in python, but as soon as the httpget.exe stops responding, the python script stops too. I just cant be around my computer all the time pressing "enter" when the window pops up.
What's the best way to go about this?
Thank you!
Perhaps you should not use an external program and use something on the standard library, as in get the content of the page and strip it using string functions:
import urllib2
url = "http://httpbin.org/get"
response = urllib2.urlopen(url)
print response.read()
Or the file:
import urllib
urllib.urlretrieve ("http://www.example.com/songs/mp3.mp3", "mp3.mp3")
More about this: https://stackoverflow.com/a/22776/349420
I've done a reasonable amount of coding with the libtcod library, both the C# and python wrappers. My current setup is KUbuntu 14.10, python 2.7.8, and libtcod 1.5.2.
I've made a few programs that work fine, but the latest I've just started doesn't seem to want to allow me to close the console window.
I can send a CTRL+C from the console that I run the program from, and it will close, but, no amount of clicking on the window's "x" button, or Alt+F4s seem to work.
My code is as follows:
'''
justclose.py
'''
import sys
import time
import libtcodpy as libtcod
libtcod.console_set_custom_font(b'lucida12x12_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(50,50, "The ever-present window", False)
libtcod.console_flush()
while not libtcod.console_is_window_closed():
time.sleep(1)
sys.exit
When I run the program, the console comes up, as expected, and sits around waiting for console_is_window_closed to return true, which it never does. I'm not sure where the problem lies. I can run other programs that use the same initialisation code, and same while loop and which respond just fine to me clicking the close button on the console window.
I've tried looking through an strace of the process, but, I'm not sure I'm up to the task of deciphering it. Nothing looked immediately out of the ordinary.
I'd like some advice on how to track down what's going wrong. Thanks.
EDIT: specifically, I'd like to know how I can check that the close window event is propagating at all, and if so, how far, where it's getting trapped/ignored, that sort of thing. When I run through strace, I see absolutely nothing happening when I click the close button. Is there some better way to debug this?
Replace time.sleep(1) with libtcod.console_check_for_keypress(). When the program sleeps 1 millisecond for each iteration, the program can not respond when you press X. It exits when you press CTRL+C because the program receives the SIGINT signal and it exits immediately. Replacing time.sleep(1) with libtcod.console_check_for_keypress() makes the program check the key pressed on the keyboard, if there is one. That way, the program doesn't block the execution.