Reputation: 8578
I'm trying to create a python script to automate a game.
I already tried these libs:
pyautogui
pywin32
ctypes
(I imported this code and calling the function PressKey
, https://github.com/Sentdex/pygta5/blob/master/directkeys.py)My code is like this:
from directkeys import PressKey, ReleaseKey, W
import time
print("Script is gonna start in 5 seconds")
time.sleep(5)
PressKey(W)
time.sleep(10)
ReleaseKey(W)
print("Script finished")
I tested this script at notepad
, it's working pretty well, but it's not working in the game at all.
I thought it was because of need to be direct input, but I think ctypes
is already sending input as direct input
.
How can I automate the game using python?
Upvotes: 4
Views: 6903
Reputation: 1
What I did was to make sure it was running either the script or the IDE in Administrator mode. For some reason, games I wrote scripts for like Black Desert Online or CSGO only detect key presses when I am running it in Administrator.
Upvotes: 0
Reputation: 580
I'm going to elevate my comment to an answer since I believe it's what you're looking for.
The W is not the proper input for the PressKey and ReleaseKey functions. They're looking for hex code input. You can find the proper hex codes here: https://learn.microsoft.com/en-us/windows/desktop/inputdev/virtual-key-codes
The W hex code particularly is 0x57. So replace "W" with "0x57":
from directkeys import PressKey, ReleaseKey, W
import time
print("Script is gonna start in 5 seconds")
time.sleep(5)
PressKey(0x57)
time.sleep(10)
ReleaseKey(0x57)
print("Script finished")
Upvotes: 1