pydirectinput icon indicating copy to clipboard operation
pydirectinput copied to clipboard

Clicks not working in-game.

Open PhilippAE opened this issue 4 years ago • 14 comments

Whenever I click it doesn't work:

pydirectinput.click(1751,827,button="right)

and what this does in the game I'm playing, it's basically cancel for example auto-pathing, but when I right click using that line of code, it's hold so it rotates de camerca, and I tried clicking again with Python to cancel the camera moving, but it gets stuck, it probably has a way to detect outside inputs. (the keyboard seems to work just fine, but the mouse is troubling)

And witht the left click it's the same:

pydirectinput.click(1751,827)

And by click 1 time the character moves to a certain place, but instead of doing it, since it's holding down, it starts "running mode".

So you can already imagine my problem, I want to click an item, and instead of doing that, the character starts running, that would normally be canceled by right-clicking, but since it's also bad, it now starts moving the camera and there's no way out of there unless I intervene. Any ideas? The game is pretty old around 2007, so I don't know what else info I should give you, other than the fact that it uses DirectX 9, so yeah, pretty old.

PhilippAE avatar Jan 11 '21 19:01 PhilippAE

Have you tried separating mouse move and click?

Try something like:

pydirectinput.move(1751, 827)
pydirectinput.click()

cetteup avatar Feb 02 '21 17:02 cetteup

Hello cetteup,

I also discovered that certain games only accept the number as input. For instance: time.sleep(0.01) pydirectinput.keyDown('ctrl') <--- doesn't work time.sleep(0.05) pydirectinput.keyUp('ctrl') <--- doesn't work (neither do any keyboard input like 'a','z', etc.) time.sleep(0.01) pydirectinput.keyDown('6') <--- works time.sleep(0.05) pydirectinput.keyUp('6') <--- works

Could you please explain me the reason behind that? Best ZaoTX

ZaoTX avatar Feb 08 '21 14:02 ZaoTX

@ZaoTX Could you please provide a few more details? What game does ctrl not work in, but 6 does? What's they action in game you expect?

cetteup avatar Feb 08 '21 21:02 cetteup

@cetteup Thank you for your quick reply, I used a listener ( pynput.keyboard Listener) to detect my keyboard input. And if I press the event key (in my case is 'e'). I want to use your package to automatically press the ctrl and number 6 in very short time interval. However, only the number works and the order of the command doesn't matter. For example: time.sleep(0.01) pydirectinput.keyDown('6')
time.sleep(0.05) pydirectinput.keyUp('6') time.sleep(0.01) pydirectinput.keyDown('ctrl')
time.sleep(0.05) pydirectinput.keyUp('ctrl')
time.sleep(0.01) pydirectinput.keyDown('6')
time.sleep(0.05) pydirectinput.keyUp('6') This will only get 2 times 6 in game. However, if I try with txt file or something else everything work again. Is that possible that the game can block the fake keyboard input still (I have no ideas)?

Best ZaoTX

ZaoTX avatar Feb 08 '21 21:02 ZaoTX

A few questions, please answer/supply details in the same order.

  1. Which game are you working on?
  2. What action is bound to ctrl (make sure it's left ctrl as that's what you pressing according to your code snippet)?
  3. What action is bound to 6?
  4. Is there a particular reason you are not using pydirectinput.press?
  5. Have you tested this with longer pauses?

I believe the game could (theoretically) block the input or limit the input interval, but would expect it to block everything and not block ctrl and allow 6.

cetteup avatar Feb 09 '21 17:02 cetteup

@cetteup Thank you! below is my answer:

1.League of Legends 2 and 3. ctrl+6 will show mastery emote in game, I use this to check whether the code works. I can also use the item(number 1 to 6) to check the functionality. by the way, it works if I open chat in game to enter some messages. 4. The reason for using KeyDown and KeyUp: to control the order of different keys, to make it looks more real(first down then up). But I never tested with pydirectinput.press() I can do it later. 5. Yes I started with long pauses and I found the number works, then I try to lower the pauses.

I really agree with your idea. I hope there is something wrong with my implementation. I am trying to implement some autocombos in game. It doesn't have to be perfect, I don't sell them, I just want to learn in a fun way.

Best ZaoTX

ZaoTX avatar Feb 09 '21 18:02 ZaoTX

So you are trying to press a combination of keys? As in, press ctrl and 6 together and not one after the other?

Right now, you are pressing 6 and then ctrl. What you need to do instead is:

  1. press ctrl
  2. press 6
  3. release 6
  4. release ctrl
pydirectinput.keyDown('ctrl')
pydirectinput.keyDown('6')
pydirectinput.keyUp('6')
pydirectinput.keyUp('ctrl')

Just tested it works, works fine for emote spamming in game.

cetteup avatar Feb 09 '21 22:02 cetteup

@cetteup I am terribly sorry, I forgot the order of these interactions. Thank you for doing everything! Best ZaoTX

ZaoTX avatar Feb 09 '21 22:02 ZaoTX

Whenever I click it doesn't work:

pydirectinput.click(1751,827,button="right)

and what this does in the game I'm playing, it's basically cancel for example auto-pathing, but when I right click using that line of code, it's hold so it rotates de camerca, and I tried clicking again with Python to cancel the camera moving, but it gets stuck, it probably has a way to detect outside inputs. (the keyboard seems to work just fine, but the mouse is troubling)

And witht the left click it's the same:

pydirectinput.click(1751,827)

And by click 1 time the character moves to a certain place, but instead of doing it, since it's holding down, it starts "running mode".

So you can already imagine my problem, I want to click an item, and instead of doing that, the character starts running, that would normally be canceled by right-clicking, but since it's also bad, it now starts moving the camera and there's no way out of there unless I intervene. Any ideas? The game is pretty old around 2007, so I don't know what else info I should give you, other than the fact that it uses DirectX 9, so yeah, pretty old.

(Not sure if this info would still be relevant to you, but I had the same problem, and I guess others might find this useful too)

The package includes mouseDown() and mouseUp() functions which seem to do the trick, for me at-least. I had to make my bot interact with UI elements in the game I play, but click() would click and hold rather than click and release, but the 2 functions mentioned above executed in intervals seem to interact with the UI elements properly.

bhuv-z avatar Feb 22 '21 06:02 bhuv-z

Thanks a lot, this also solved my problem.

firtinahg avatar Dec 14 '22 21:12 firtinahg

@bhuv-z's approach worked for me!

mmafic avatar Aug 22 '23 22:08 mmafic

Hello, I would like to use mouse left-click in game 'Stronghold Kingdoms'. Don't know if you know the game, but when I try to click inside the map on any object, it just doesn't work. When click is executed on gui, it works. pydriectinput, pyautogui, none of them helped me. I am furious, can anyone help? <3

rakxmm avatar Mar 21 '24 20:03 rakxmm

@rakxmm Some games require you to use legacy Windows APIs for mouse control in game. I needed to do this for Battlefield 2, for example. Same issue: The usual tools work in the menu, but not when in game. Using win32api like this might work (it does for Battlefield 2). win32api is provided by pywin32.

cetteup avatar Mar 22 '24 11:03 cetteup

@rakxmm Some games require you to use legacy Windows APIs for mouse control in game. I needed to do this for Battlefield 2, for example. Same issue: The usual tools work in the menu, but not when in game. Using win32api like this might work (it does for Battlefield 2). win32api is provided by pywin32.

mouseDown() and mouseUp() worked for me to click in this game. Thanks, i will definitely try your idea

rakxmm avatar Mar 22 '24 11:03 rakxmm