4

I'm trying to create a python script to automate a game.

I already tried these libs:

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?

Lai32290
  • 8,062
  • 19
  • 65
  • 99
  • What's the game you're trying to use this script with? Perhaps the game is able to prevent the user from using a bot. – Flostin Jul 12 '18 at 18:35
  • The game is `Ran Online`, BTW, I'm playing private server just to learn how to do this. – Lai32290 Jul 12 '18 at 18:37
  • 1
    https://stackoverflow.com/questions/44886546/unable-to-receive-keystrokes-from-steam-and-a-few-other-programs This question might help? Check for admin privileges and make sure both your program and game are on equal grounds for that. – J0hn Jul 12 '18 at 18:38
  • Oh boy, I didn't try this way, let me try it than I'll let you know the result. thank you so much @J0hn – Lai32290 Jul 12 '18 at 18:41
  • @J0hn just to make sure, the way that I simulating keyboard press (`PressKey`) is sending and `Direct Input` type of input, right? – Lai32290 Jul 12 '18 at 18:43
  • 1
    Your second w in ReleaseKey is lowercase. – Evan Jul 12 '18 at 18:44
  • @Lai32290 I believe so, if you're using the library I google searched and found, I think it uses ctypes, which is how I simulated key presses before with python. EDIT: Another thing you can try is replacing W with the hex value for it. So like numlock has a hex value of 0x90. I think that's the type of input those functions want. – J0hn Jul 12 '18 at 18:46
  • @Evan, sorry about it, it's typo error. – Lai32290 Jul 12 '18 at 18:58
  • @J0hn I'm not sure, but I think hex value should not be the issue, because I'm able to type into a `notepad` using my script. it's not working only in the game. – Lai32290 Jul 12 '18 at 19:00
  • I'm at work now, I'll try to run the script as admin permission later, it seems the solution to my problem. – Lai32290 Jul 12 '18 at 19:00
  • 1
    Hey @J0hn! I executed the script as admin and it just working!!!! thank you so much!!! – Lai32290 Jul 13 '18 at 00:23
  • @J0hn, can you create a answer with this `run script as admin permission` solution? Than I vote on it. – Lai32290 Jul 17 '18 at 00:53

2 Answers2

1

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")
J0hn
  • 570
  • 4
  • 19
0

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.

  • 1
    Please add further details to expand on your answer, such as working code or documentation citations. – Community Sep 07 '21 at 15:26
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – Sparky05 Sep 08 '21 at 14:19