How to control keyboard in Minecraft with Pynput - python

I am trying to make a macro in Minecraft using pynput, but it seems that pynput does not press the correct buttons on the keyboard. I know this because when I ran (code snippet 1) while setting the controls of Hotbar slot 3 it showed as a weird character instead of a number. (see picture) Is there a way to make pynput press the correct buttons?
# Code snippet 1
import time
from pynput.keyboard import Controller
time.sleep(7) # this is just so I have time to switch to minecraft before it activates
board = Controller()
board.press('2')
time.sleep(.1)
board.release('2')
In place of '2' I have also tried KeyCode(49) and gotten the same result. It is properly recognised by the computer, as shown by keycode.info and notepad.
When I replaced '2' with 'e' Minecraft recognised it as the same as '2', and it even pressed the button, so I am extra confused now.
Extra notes:
For now I will just set the controls to what pynput gives minecraft to make my macro work, but I would prefer to be able to use the button(s) outside of the macro as well.
Pynput's click and scroll seem to work fine.
I am not using the mouse for my macro, but if you have knowledge about the mouse there is a stackOverflow question about it here: How to control the mouse in Minecraft using Python?
For anyone wondering, I am making this macro to empty water buckets quickly after I have used them to remove an area of water in the ocean

While I could be wrong about this, based on this stack overflow post I believe that pynput outputs the characters as win32 keycodes, which while they work for programs like notepad or keycode.info, don't seem to work with setting minecraft controls.
If you are ok with using a different library than pynput, I have found that pywinauto correctly inputs keys into minecraft.
from pywinauto import keyboard
import time
time.sleep(3) #A short wait to allow me to switch to minecraft
keyboard.send_keys("{2 down}" "{2 up}") #replace 2 with whatever key you want to press
Above is the code that I used while testing and I have found it works correctly when trying to set controls.

The easiest way i think is using pydirectinput.
pydirectinput is exactly like pyautogui. Its just that its name changed and that it can work with games using DirectX (minecraft, roblox)
To use it you can just do
import pydirectinput
import pyautogui
import time
time.sleep(2)#so u can switch to your game
pydirectinput.keyDown('v')
time.sleep(0.5)
pydirectinput.keyUp('v)

Related

How can I click buttons in a windows GUI program using python?

I am writing a python script that automates running a program and performing different tasks within the program. My main problem is figuring out how to click buttons and interact with the GUI of the program to be controlled.
I am currently using the pyautogui library and using pyautogui.click(X,Y) to advance through prompts and click on different menus and menu items. The problem with this approach is that I am relying on a separate script to inform me of the coordinates of interest in my environment by telling me the coordinates of where my cursor is hovering. This probably will not work on other machines and just seems like a one case solution.
My question is how can I automate using a program in windows (clicking around) without having to hard code the exact position of the items I need to click?
For example, If I need to click a "ok" box to accept some setting, how can I make Windows grab the program window, read through the options and click what I need without any prior knowledge of the position of the dialog box and where the "Ok" button is located?
Code:
import pyautogui as gui
gui.click(x,y)
The way you can do this using pyautogui is with their locating methods. You will need a picture (for example of the OK box) and then you can have pyautogui find it on the screen and give you its coordinates. Check out the official documentation on this.

Python SendInput() keypress doesn't get recognised

I tried to simulate keypresses in python, but they dont get recognised the programm. All work in simple editors, some even in an open admin cmd window, but nothing here.
What I tried so far:
-librarys: pyautogui, keyboard, pydirectinput, pynput, pyKey
Solutions with ctypes and SendInput:
Python simulate keydown
Use Python to send keystrokes to games in Windows?
Simulating a keypress in python
All other solutions i found were all using win32api or SendInput and didn't work either.
The usecase is the programm Parsec, if that helps. Also mouse movement and clicks work fine with all of the solutions above.
Could anyone help me with that, or point me in a direction? Because i don't know how to continue.
Some programs, especially video games, are specifically made to be resistant to what you are trying to do. This is to prevent bots and cheating within the game or program. You could find a way to make a driver that simulates key presses, but considering that the program has taken specific measures to defend against this, it is likely bannable or violates the Terms and Service agreement.

I am trying to move my camera in minecraft with python

I have successfully found out how to move, drag, and click in Minecraft with python code. The only thing is that I have gotten the cursor to work in the inventory/crafting menu. One line I used to go up was
pyautogui.moveRel(0, -33, duration=0.1)
For some reason, this does not work outside the crafting menu. I am not able to move the camera in the open-world part of Minecraft. I had a problem with the keyboard as well, it would type the keys I would want it to in a text box but not in the real game. I fixed this by using keyboard.press() and keyboard.release() instead of using pyautogui. Pyautogui works for clicking and moving in the crafting menu but not in the real game.
Any thoughts on how to move the camera in-game?
Pyautogui does not work on video games on Windows or any other common OS. I suggest trying https://pypi.org/project/mouse/, as the keyboard function worked for other people.
Pyhon DirectInput Mouse Relative Moving act not as expected
Looking at this, I found the answer. I was trying to move the mouse where the game has a specific directinput I needed to use. I just called MouseMoveTo() with the desired numbers.
For some reason it only works like this MouseMoveTo(0,100,) with the added comma at the end
I got it to work by turning off Raw Input in the Mouse Settings. Which are in Options > Controls > Mouse Settings > Raw Input

How to change driver level keyboard interaction?(using Python or C++)

I am beginner level in programming(less than a year of experience). I am trying to use https://github.com/Sentdex/pygta5/blob/master/directkeys.py to imitate keyboard inputs in one of RPGs. I successfully managed to implement simple movements(WASD) but when I try to use game specific command as example "choose a closest enemy(Using TAB)" game doesn't react to the input. By doing some research I realized that the game most likely have internal driver level keyboard interaction which cannot be activated using Direct Inputs. I am asking for possible ways to solve the issue.
I tried to use virtual keys to try to imitate the keyboard inputs. Also, I tried to reassign one of the movement keys to "chose closest enemy button" but it produces no inputs as well. I am might start extensive research towards even lower-level keyboard interactions and see what I can do from there.
Here is small code sample
from directkeys import KeyPress, PressKey, ReleaseKey, W, ESC, C, TAB
import time
import win32gui
# open a window
handle = win32gui.FindWindow(0, "Game window")
win32gui.SetForegroundWindow(handle)
# Press and Release key
PressKey(W)
time.sleep(3)
ReleaseKey(W)
print("Script finished")

Detect mouse side button events in Python

I am searching for a way to get the events when clicking the 'Back' and 'Forward' buttons on the side of a normal mouse.
This function can be found in Firefox (for going back/forward) and Sublime Text 2 (for switching between tabs).
I know that there is a Windows program set that is called pyHook, but I would like to have something for Python in Linux.
Thanks in advance.
I think the example code in the answer to this question might be useful to you: Mouse wheel in python curses

Categories