LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Slackware (https://www.linuxquestions.org/questions/slackware-14/)
-   -   DPAD on Microsoft Xbox 360 controller (https://www.linuxquestions.org/questions/slackware-14/dpad-on-microsoft-xbox-360-controller-4175619380/)

bifferos 12-11-2017 11:17 AM

DPAD on Microsoft Xbox 360 controller
 
I wonder if anyone has come across this problem before. After plugging in my controller and doing:

modprobe xpad
modprobe joydev

sdl-jstest --test 0
tells me that everything works, however the D-PAD is recognised as a 'hat' with various different values printed for the different directions.

So I tried to run Frozen Bubble and configure it to use the controller but found it seems to not recognise any 'hat' events.

Next I installed Rejoystick to try to map the hat to some keys, however it just selects multiple axes when I move the DPAD, so I think it doesn't properly support the hat either.

I had a look at the Frozen Bubble source, but it's pretty hard for me to read with my limited Perl skills.

Is there any other solution to using the DPAD (hat) as buttons - or keys - in games?

thanks,
Biff.

dugan 12-11-2017 11:50 AM

Quote:

Originally Posted by bifferos (Post 5791930)
Is there any other solution to using the DPAD (hat) as buttons - or keys - in games?

You can actually set up these overrides (joystick actions to keyboard events) in Steam, using the Big Picture Mode. Add the game to Steam as a non-Steam game first.

This is another way:

https://github.com/AntiMicro/antimicro

This was popular a couple of years ago. I recommend only using it as a last resort:

https://pingus.seul.org/~grumbel/xboxdrv/

bifferos 12-11-2017 04:47 PM

I went with a hacked up solution in the end. I got hold of the Python 'inputs' module, had a look at what the example gameport script was printing, then tried to marry this up with the kernel documentation (/usr/src/linux/Documentation/input/event-codes.txt), because that seemed to be the only place that explained about the events.

With a bit of trial-and-error I could map left and right to the z and x keys. The up and down are presumably ABS_HAT0Y.

I played around with this code, and bad things happen if you do too many release events for a key in pynput, but there seems to be no problem pushing a release event from pynput in combination with release events from the real keyboard (pressing both DPAD left + keyboard 'z' and other combinations).

Code:

#!/usr/bin/env python
#
# Remap hat L-R keys to keyboard 'z' and 'x'
# pip install inputs
# pip install pynput

from inputs import get_gamepad
from pynput.keyboard import Key, Controller

keyboard = Controller()

while 1:
        events = get_gamepad()
        for event in events:
                if event.ev_type == "Absolute" and event.code == "ABS_HAT0X":
                        if event.state == -1:
                                keyboard.press('z')
                        elif event.state == 1:
                                keyboard.press('x')
                        else:  # state == 0, centre position
                                if keyboard.pressed('z'):
                                        keyboard.release('z')
                                if keyboard.pressed('x'):
                                        keyboard.release('x')

And Frozen Bubble is happy :-)


All times are GMT -5. The time now is 03:50 PM.