sorry in advance for my bad english.
I'm writing a game automation program , specifically the game requires a key press to start doing (something) , but when i use KeyBoard.press() function from KeyBoard module i have problem where game doesn't recognize my key i tried in game chat box and it still works , it means the key is still pressed down but the game doesn't pick up , i tried using some other functions like " KeyBoard.press_and_release() , KeyBoard.send() " and some other modules but the result is the same,
here is the example code
While True:
if (...):
keyboard.press('e')
keyboard.release('e')
but what confuses me is when i add time.sleep() between the press and release commands it works but what confuses me is when i add time.sleep() between the press and release commands it works but there is a big delay
Anyone have any solution for me?
While True:
if (...):
keyboard.press('e')
time.sleep(0.05)
keyboard.release('e')
If I understand properly (and, I might not, sorry) it could be an issue with the game rather than python. Some games, especially online games, implement features specifically to stop automation.
Have you tried your code in a different context - another game or, seeing as it simulates key presses, a text editor? I think you are saying it works in the chat box but not for game control inputs. If it works as intended outside of the game but not inside that would suggest the problem is something to do with how the game handles key presses.
In that while loop, you might be simulating 20 key presses a second, while a more usual average would be between about 5 and 7 with the world record reported at 16. Perhaps the game detects if the rate is too high and discounts those key presses. Also, a physical keyboard will be subject to repeat rate and repeat delay, which the game may be testing to specifically try and eliminate this type of virtualized automation. Or, something else entirely - without knowing which game you are trying to automate I am left with guess work.
That said, with your reference to an in-game chat box, I strongly suspect you are trying to automate an online multi-player game. Even if you are just doing it for your own personal interest (and not to get an advantage over other players), that kind of thing is frowned upon and can potentially get your game account banned. I would normally suggest you share which game you are trying to automate with the community to get more specific information, but I doubt you will be happy to do that.
Related
Thank you for reading this.
So e.g I start csgo, minecraft, or whatever game. When I'm in game I want to open the chat by pressing the key "y", nevertheless, I've tried using pyautogui, keyboard, win32api and win32con, they only simulate virtual key presses and I guess that's the problem since the games don't accept it as real key presses.
Is there any way I could simulate it as actual key presses?
I appreciate any answer!
Have a good day :)
I don't think Python is the right tool for this, try looking for key mappers (like KeyMapper), they simulate keypresses better afaik and you can easily bound 'y' to press whatever button opens chat in the game you want.
I've recently realized that Python's pynput module's key click doesn't get registered in certain software. However, physical key clicks work perfectly fine. This raises the question of how are the two different methods been recognized. I thought this would've been an answered question from a long time ago but I can't seem to find any post about this.
Let's take a random game such as Genshin for example. The "w" key on the keyboard would result in the character in the game moving forward. However, a program such as the following that "holds" down the "w" key doesn't get registered by the game.
from pynput.keyboard import Key, Controller
keyboard = Controller()
while True:
keyboard.press("w")
keyboard.release("w")
The two big questions that I have are:
What is the difference between physical key clicks and the program generate key clicks that allowed these softwares to distinguish which method is used?
Based on the answer to the first question, is there a way to bypass such a difference without tempering with the software's code (aka disguise program generated key clicks as physical key clicks)?
Thanks in advance!
Edit: If anyone has any idea on how this works, please let me know. Any help would be much appreciated!
I also encountered this problem when I was trying to write a small program to help myself in Genshin. After an excruciating search, I found these two questions: question1, question2
I'm not very sure how it exactly works, so I can only give you my conclusion: When we run Genshin, we give it admin rights. Programs without admin rights sort of can't interact with it.
To solve this problem, you can merge this into your code:
import ctypes, sys
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
if is_admin():
# Code of your program here
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)
Then you need to make the code an executable with pyinstaller module. When you execute it, it will re-run and ask you for admin rights, then it can do its job on Genshin or other program.
There are certainly games that intentionally try to prevent the kind of cheating you're trying to do. How they do so depends on the operating system.
pynput on Windows, for example, uses the send_input API to inject keystrokes into the input system, but a sufficiently motivated application could be reading at a lower level, by using a keyboard filter driver to pull keystrokes before they get into the input system.
With such a design, there's nothing you can do in user mode to cheat the keys. You would need your own kernel driver, and even then it's not trivial to guarantee the order that the filters load.
I got my script to work with typing out keys, but for some reason, those keys do not work in Minecraft. Although, typing Minecraft messages in the chat are no problem. I'm trying to get python to move my own character.
In my script, I tried to use pyautogui, the mouse in Minecraft works but not keys such as w, a, s, or d. Is there another import or way to hold down a key that actually works in Minecraft?
def hold_W (hold_time):
start = time.time()
while time.time() - start < hold_time:
pyautogui.press('w')
Doing this and calling it worked for writing texts, but for some reason does not register in Minecraft? Any help?
If you used "Automate the Boring Stuff With Python" to learn this stuff, the book states that pyautogui cannot be used on video games on Windows. I am not sure how to fix this, but the link to the book is here: https://automatetheboringstuff.com/2e/chapter20/
Also, you can go to the documentation of pyautogui here: https://pyautogui.readthedocs.io/en/latest/ to see if the games feature has been added. My greatest regrets that I cannot solve this problem for you. If it makes you feel better, pyautogui does work on .io games.
Different video games have different ways of detecting keystrokes, so to find a thing that will work on all video games you might need to get your hands dirty and do some stuff I have no clue how to do.
My laptop keyboard has a bug and it sometimes presses the number 5 randomly so i tried many things and they didn't work, I tried programming a code that can stop it but i couldn't because i am a beginner.
from pynput.keyboard import Key, Controller
keyboard = Controller()
disable = keyboard.pressed('5')
if deni:
KeyboardInterrupt
continue
hopefully this doesn't need imports
This question does not belong here. You should have put it in https://superuser.com/ as it would have been a whole lot easier and more effective to do it in some other programming language. But if you want to do it in python, first learn python.
Other Options
Try pressing "Fn and Num Lock" or if you don't have "Num Lock" do "Fn and Caps Lock.
Try reinstalling your keyboard driver.
If your keyboard has gotten water inside of it, then put it in rice and let it sit for 2 to 3 days
Buy a new keyboard
Resources
http://www.tomshardware.com/answers/id-1972361/keyboard-keys-typing-wrong-multiple-letters.html
I recently finished a big project i have been working on, and i am looking for a new challenge. I was thinking about making python play a game, and making it unbeatable. to do this, i need an easy way of entering the other players move, and i was thinking about something where i can create multiple buttons which the user can press. (for example: if i want to make tic tac toe i would want 9 individual buttons that are triggered by a mouse click, to get the input).
I have no idea if this is possible in python. i have seen multiple pages about this, but some pages have such a large amount of information on them that i don't know where to start.
thanks in advance for any answers,
Harm