Press key to game window with Python - python

I try to pass keyboard event to game window but it doesn't work. For another program such as Notepad++ it is works.
from pynput.keyboard import Controller
keyboard = Controller()
keyboard.press('a')
keyboard.release('a')
The same a problem I have with mouse events. I tried use "Mouse and Keyboard Recorder" program and it work. Which is a problem?
I try to write bot to game for fun.

It's my answer:
Win32API Mouse vs Real Mouse Click
But I don't know how to write a custom driver :)

Related

pyautogui mouse click not work in a game?

I want to use mouse events with pyautogui, pydirectinput, win32api and win32 (C#). Normally, they all work, but when I run the game, the click events do not work whether the game is on the screen or not. Can you help me?
I want the click events to work while the game is running as well as when the game is not running.

Can't move minecraft character using pynput

I'm trying to use Pynput to move my Minecraft character, but when I press run, it finishes without moving my character. I've retested it on stuff like the Minecraft chat and it works fine, I've also tested it on the Google Chrome search and it works, but weirdly not in Minecraft. My code is very simple.
from pynput.keyboard import Key, Controller
import time
myKeyboard = Controller()
time.sleep(3)
myKeyboard.press('w')
myKeyboard.release('w')
time.sleep(3)
myKeyboard.press('s')
myKeyboard.release('s')
I don't see anything wrong with my code and I don't know if pynput even works with moving Minecraft characters, or any characters for that matter.
According to pynput documentation, keeping a button pressed is best used with the pressed method and a with statement, like this:
with myKeyboard.pressed('w'):
time.sleep(3)
It should in theory hold the w key pressed for 3 seconds.

Why does this auto-clicker program stop when it starts clicking?

I am in the process of writing an auto-clicker that will work by holding down a button. I've checked, I haven't seen anywhere in the python community an auto-clicker that works on this principle.
When the program detects that I press the left mouse button it automatically starts clicking. Then, it detects this clicking as if it was me clicking so the program stops.
I'd like the program to click with a given frequency so long as the left mouse button is pressed, and to stop when it is released. Is there a way to accomplish this?
import time
import mouse
from pynput.mouse import Button, Controller
start = Controller()
while True:
time.sleep(0.0001)
while mouse.is_pressed("left"):
start.click(Button.left)
time.sleep(0.1)

MacOS - Python 3.7 - GUI mouse control for DirectX 8 (2003) app - unable to simulate mouse clicks but I can simulate keyboard entry

I am trying to control an old DirectX application from a Python script. The application concerned is from 2003 (ish). Launches full screen (in a Desktop space) in OSX and requires the mouse be 'freed' via a command. Once the mouse is freed you can use it normally.
I have tried scripting to simulate a mouse click. I have used AppleScript and Python to no avail.
Keyboard simulation does work. For example the first thing any scripting does is swap into the relevant desktop space with a shortcut then hit a keyboard shortcut in the app. This works.
Moving the mouse around the app with scripting works.
However the following will not work.
1) Simulated clicks from code.
2) Turning on mouse keys and simulating a keyboard click in code. Note if I turn on mouse keys and manually hit the mouse click key in the app this does work.
Doesn't work -
import pyautogui
pyyautogui.click()
from pynput import Button,Controller
mouse = Controller()
mouse.click(Button.left, 2)
So basically simulating the keyboard works in the app but I am struggling to simulate a mouse click (moving the mouse works fine). Any ideas?
are you saying you want to write with pyautogui? you can use this:
import pyautogui
pyautogui.write('An example')

How do I use side mouse buttons with Pynput?

I am using the Python module Pynput to make a macro that will press one of my side buttons. Does anyone know what the side buttons are called in Pynput?
For example:
from pynput.mouse import Button, Controller
mouse = Controller()
mouse.press(Button.SIDEBUTTON)
mouse.release(Button.SIDEBUTTON)
What would go in the SIDEBUTTON part?
So this question is a bit old and i had the same problem. I figured out how these buttons are called:
its Button.x1 and Button.x2 for Mouse4 and Mouse5.
Hope I could help you. The script I used to find it is right here:
from pynput.mouse import Listener
def on_click(x, y, button, pressed):
if pressed:
print(button)
# Collect events until released
with Listener(on_click=on_click) as listener:
listener.join()
unfortunately there isn't such a function in pynput and if you run this code and press side mouse buttons you will realize that it wont give you any output:
from pynput import mouse
def on_click(x, y, button, pressed):
if pressed:
print ('Mouse clicked {2}'.format(x, y, button))
with mouse.Listener(on_click=on_click) as listener:
listener.join()
Additional buttons on mouse models are usually communicated as 'Button 6' and 'Button 7', etc. (buttons 4 and 5 are the scroll 'buttons'). Some mouse manufacturers send keyboard codes instead (like multimedia buttons or other custom codes).
For Windows and OS X Pynput only supports the left, right, middle mouse buttons however, so you'd be out of luck on those platforms as far as Pynput is concerned. If you are on Linux (with the X.org back-end), you can send and receive more buttons, from button8 all the way up to button31, as well as scroll_up, scroll_down, scroll_left and scroll_right.
So depending on the mouse model you are using and your operating system, you may be able to get the right events for those buttons, be they mouse buttons or keyboard events. Register both a mouse and a keyboard listener, and print out the button value for on-click events for the mouse, and the key for keyboard press or release events, and see if you can get your side button to show up.
When not on Linux, if the specific mouse buttons are sent as a keyboard events, you are in luck and can use the keyboard controller to send the same events. If not, then Pynput can't send such mouse button events either.
That's not to say you can't send such button clicks at all, but you'd have to study the source code of the controller used for Windows or OSX and then see how the underlying framework would accept other button presses besides left, right and middle.

Categories

Resources