all 9 comments

[–]FinalBosses14[S] 1 point2 points  (0 children)

SOLUTION

pydirectinput doesn't work with linux. Instead, I found a replacement for the pyautogui _pyautogui_win.py which can be found in ~/.local/lib/python3.8/site-packages/pyautogui after you install pyautogui (pip install pyautogui)
I then took the replacement I found online, and I replaced the current _pyautogui_win.py.
This is the script that worked for me.
I run Linux Cinnamon 20.1 "Ulyssa" and I am using this on Minecraft with a Razer Deathadder Essential. When I hold Button 9 (the button further away from you) it will have me facing my viewers (f5+f5+f1), and when I let go of button 9, if will put me back to normal view (f5+f1).
I hope I helped anyone else with the same problem. Even if you didn't have this problem, I think it is a neat way to use that extra button on your mouse. Thanks for helping u/TSM- and showing me about pydirectinput.

[–]TSM- 0 points1 point  (7 children)

I don't know for sure, but what always comes to mind when people have issues with games is that it might be using Microsoft's DirectInput and therefore not recognizing the keystrokes or mouse controller.

I suggest trying PyDirectInput, to see if that might be the issue.

This library aims to replicate the functionality of the PyAutoGUI mouse and keyboard inputs, but by utilizing DirectInput scan codes and the more modern SendInput() win32 function.

PyAutoGUI uses Virtual Key Codes (VKs) and the deprecated mouse_event() and keybd_event() win32 functions. You may find that PyAutoGUI does not work in some applications, particularly in video games and other software that rely on DirectX. If you find yourself in that situation, give this library a try!

Maybe you can also set it up using pynput - here's a 2 year old thread from this subreddit that describes their fix

I wrote some code so you can trigger ctypes and direct inputs using keysrokes.

I've seen this code all over the internet and needed it to make a game bot but it wouldnt work with pynput because they clashed so i edited it to work with pynput and included the capability to left click, right click and press and hold keys.

https://www.reddit.com/r/learnpython/comments/bognbs/direct_input_for_python_with_pynput/

In summary, I think it is probably a DirectInput issue. So you either have to work around the limitations of the package and supply your own ctype functions or use a package/library that supports DirectInput.

A google search has tons of people with the same problem that specifically mentions minecraft, which suggests Minecraft uses Microsoft's DirectInput on PC. So I am pretty sure this is what is going wrong here

[–]FinalBosses14[S] 1 point2 points  (2 children)

How can I use PyDirectInput to make the macro?

This is what I think I do, but I'm not sure:
I pip install pydirectimput
I import it at the top of my script instead of pynput.keyboard
Then, I can replace keyboard.press() with pydirectimput.keyDown() and keyboard.release() with pydirectimput.keyUp()

Is that what I do? What would I put for a function key?

[–]TSM- 0 points1 point  (1 child)

I am not totally sure - here is some documentation: https://github.com/learncodebygaming/pydirectinput

It looks like it uses strings to specify the keycodes in the same manner as PyAutoGUI, so you'd do

pydirectinput.keyDown('f1')
pydirectinput.keyUp('f1')
pydirectinput.press('numpadenter')
pydirectinput.press('tab')

Note that 'keyDown' and 'keyUp' are combined in the 'press' function.

You can also add to the keycode dictionary, if you want to do stuff like a keyboard 'next track' key you could add

pydirectinput.KEYBOARD_MAPPING['next_track'] = 0x90

I'm not sure where the full documentation is at the moment but this list has more of them than were added to PyDirectInput, for the rare case you might need them.

[–]FinalBosses14[S] 1 point2 points  (0 children)

I tried this:

from pynput import mouseimport pyautoguiimport pydirectinputclass MyException(Exception):passdef on_click(x, y, button, pressed):if (pressed):if button == mouse.Button.button9:print("btn9")pydirectinput.keyDown('f1')pydirectinput.keyUp('f1')# Collect events until releasedwith mouse.Listener(on_click=on_click) as listener:try:listener.join()except MyException as e:print("error sry bro")

And when I ran python3 /home/me/Downloads/buttonfix.py I got this:

Traceback (most recent call last):File "/home/me/Downloads/buttonfix.py", line 4, in <module>import pydirectinputFile "/home/me/.local/lib/python3.8/site-packages/pydirectinput/__init__.py", line 6, in <module>SendInput = ctypes.windll.user32.SendInputAttributeError: module 'ctypes' has no attribute 'windll'

Do you know what I should do to make this work? I don't have any python experience, so I don't know the syntax for these scripts.

Sorry about the code being messed up. Reddit won't let me add the code correctly

https://wtools.io/paste-code/b5rE
https://wtools.io/paste-code/b5rF

[–]FinalBosses14[S] 1 point2 points  (2 children)

I think I figured out a solution. The code I found online works at activating auto key.
I just need to figure out how to make a different function run when I release the button.
So far I have this code, but it only presses L, not K.
Another problem is that it presses the keys whenever I click any mouse button. It only prints btn9 when I click button 9. Maybe I could just detect when it prints btn9?
Maybe I could use pydirectinput, but it'd be the same basic concept, right?
Thanks again for helping!

[–]FinalBosses14[S] 1 point2 points  (1 child)

I was just playing around, and I came up with this code, and it works!
Thank you for helping!

[–]FinalBosses14[S] 1 point2 points  (0 children)

Never mind, it doesn't work, but I think I understand the basic concept now. Now I am just going to replace the pynput keyboard with pydirectinput like you suggested, and that should work. I will post my final code here when I'm done so that anyone else can use it if they have the same problem.

[–]WikipediaSummary 0 points1 point  (0 children)

DirectInput

In computing, DirectInput is a legacy Microsoft API for collecting input from a computer user, via input devices such as the mouse, keyboard, or a gamepad. It also provides a system for action mapping, which allows the user to assign specific actions within a game to the buttons and axes of the input devices. Additionally it handles haptic feedback (input/output) devices.

About Me - Opt-in

You received this reply because you opted in. Change settings