0

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')

minecraft reads board.press('2') as soh\soh"]"soh

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
Jelly Joe
  • 38
  • 1
  • 7

2 Answers2

2

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.

  • thanks for your answer. The win32 is a good explanation for what is happening. Unfortunately I can't use pywinauto because I am on python 3.7, but I will try some other libraries. pywinauto gave me a long error ending in: `TypeError: item 2 in _argtypes_ passes a union by value, which is unsupported.` – Jelly Joe May 24 '20 at 20:35
  • @JellyJoe Sorry about that, according to this article https://stackoverflow.com/questions/60588615/error-while-importing-the-pywinauto-module there is a bug in python 3.7.6 and 3.8.1 where it will give that error and 3.8.2 will revert to the wrong version. I was using 3.8.2 while testing, so it worked for me, but apparently not how it was supposed to. Also, sorry about the link just being a link, I can't get the link to be text on my phone. – thumperfuzzy May 27 '20 at 03:08
1

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)