Using Gamepad arrows and buttons in command line apps

DevTerm’s arrows and buttons are good for navigating in command line. But they don’t react to key press.
Luckly we can borrow idea from RetroPie distribution: joy2key.py

In the following bash script, ‘joy2key.py’ will be forked and run along with Midnight Commander.
And the python script will push keycode to programs that you are using in background. So they can react to arrows and buttons without C/C++ coding.

Here is steps:

((1)) clone RetroPie repository to /home/pi/RetroPie-Setup
You don’t need to proceed the long installation process. Since we will just borrow only some scripts.

cd
git clone --depth=1 https://github.com/RetroPie/RetroPie-Setup.git

((2)) save the following script to file and run. (Actually I saved the following script to ~/.zshrc so that whenever I run ‘zsh’, Midnight commander will launch.)

#!/bin/bash

# Joystick information
export SDL_GAMECONTROLLERCONFIG="03000000af1e00002400000010010000,ClockworkPI DevTerm,platform:Linux,a:b2,b:b1,x:b3,y:b0,back:b8,start:b9,leftx:a0,lefty:a1,"

#                left   right   up      down    a:<CR>  b:<space>  x:<F10>  y:<F9>
readonly CONFIG=(kcub1  kcuf1   kcuu1   kcud1   0x0d   0x20        kf10     kf9)
readonly JOY2KEY_SCRIPTLOC="$HOME/RetroPie-Setup/scriptmodules/helpers.sh"

if [[ -f $JOY2KEY_SCRIPTLOC ]]; then
    source "$JOY2KEY_SCRIPTLOC"
    scriptdir="$HOME/RetroPie-Setup"
    joy2keyStart ${CONFIG[@]}
else
    echo "Can't import Joy2Key Script! Error!"
    echo "Script not found in: $JOY2KEY_SCRIPTLOC"
    sleep 5
    exit 0
fi

# hide cursor
echo -en "\e[?25l"

# Run Midnight Commander. 
mc "${HOME}"

# If mc is closed, then stop the joy2key process in background.
joy2keyStop

exit 0

By this simple script, we can use arrows/buttons in command line. And it may work in X-Window.


EDIT:

I found the above script does not run latest RetroPie-Setup. I’ll try to adjust with new retropie.

8 Likes

There is an easier way if you really don’t want the game pad to act like a game pad but send key press, take out the keyboard, return it, search foe the small switches:

If you never touch them there will be a yellow/orange tape on top, you can remove it safely.

Change the switch #2 (the left one on the image) to be on the bottom, if you look at it like on the image.

Then put back the keyboard, and voila, the game pad now send key press!

Bonus, you can so that while the DevTerm is running!

18 Likes

That’s the kind of stuff that should be on the Wiki I think!

7 Likes

Does this work for both arrow keys and game pad?

Not sure what you mean here?

The arrow key always send arrow keys?!
The switch change the gamepad into sending key event instead of joystick event, and the “D Pad” will send arrow key event, Start/Select and ABXY will send other key event.

You can try yourself really fast, it takes litteraly less than a minute to move the switch and see what it is doing.

Sorry, terminology problem (not a gamer here). I refered to the “D-Pad” as “arrow keys” because they’re keys with arrows, which is of course very ambiguous since another set of “arrow keys” is already present on the keyboard.

1 Like

The switch on the back change the comportment of the “d-pad” and the ABXY buttons and the two start/select keys, but nothing more

3 Likes

I wanted to use D-Pad arrows and Buttons (A/B/X/Y) in command line without X-window env.
It is quite useful for me to use them because they are at top-most in keypad. It is better than Arrows at right-bottom.
And also I can reassign custom keycode on each buttons by adjusting ascii code as arguments to joy2key_sdl.py.

Actually it was simple. Here is the steps .

Setup

<1>
I installed latest RetroPie-Setup from github (Sep.1st, master branch hash: 33bc26fafe496c595e92ff470ee728e45b3c49ae)

And finished to setup input configuration. The configuration file genreated and saved to
/opt/retropie/configs/all/retroarch-joypads/'ClockworkPI DevTerm.cfg

input_device = "ClockworkPI DevTerm"
input_driver = "udev"
input_enable_hotkey_btn = "8"
input_up_axis = "-1"
input_left_axis = "-0"
input_state_slot_decrease_axis = "-0"
input_select_btn = "8"
input_right_axis = "+0"
input_state_slot_increase_axis = "+0"
input_y_btn = "3"
input_x_btn = "0"
input_menu_toggle_btn = "0"
input_down_axis = "+1"
input_start_btn = "9"
input_exit_emulator_btn = "9"
input_b_btn = "2"
input_reset_btn = "2"
input_a_btn = "1"

<2>
In command line, run the joy2key script. This will enable to share joystick within the shell session:

/opt/retropie/admin/joy2key/joy2key start

If you finish the shell session, joy2key process will end.
To stop manually, run:

/opt/retropie/admin/joy2key/joy2key stop

Test

  • start joy2key
  • Run mc (midnight command), and try to use buttons.
5 Likes

According to xev(1), the buttons cause these key pressed events after changing the switch:

  • Arrow Dpad identical to arrow key
  • A button equals j key
  • B button equals k key
  • X button equals nothing u key
  • Y button equals i key
  • Select button equals space bar
  • Start button equals Return key

Any idea what’s wrong with my X button? Will try it again after setting the switch back to default position.

Edit: after pressing the X button a bit harder for a while, it seems to equal the u key. Will take it apart and clean it a bit, hopefully improving it.

2 Likes

Press X harder, it seems there is a small mechanical issue with the X button that make it harder to register a press…

1 Like

You can also use AntiMicroX (in the repos as antimicro) to configure keyboard emulation if that’s more comfortable for you. You will, however, need to ignore the rather excessive set of axis and buttons the device descriptor reports.

Not compatible with pure text mode, but if you usually just put a terminal fullscreen in X, it does the trick.

1 Like

Hi, when I try to run the retropi setup I get the message 'Unknown platform - please manually set the __platform variable to one of the following", and then a long list of platforms. Did you have to do this? If so, what platform did you pick?

Thank you!

I wonder about the overhead and responsiveness of running the joy2key.py script in the background.

Because RetroPie is being actively developed, I chose to stick to specific version of Repository.
It’s hash value is “c70801758a42fc0d017d9022e68c524578602b25”.
You may have to dig into for other version.
good luck.

I don’t feel any overhead since there is only one instance of joy2key.py

1 Like

Well that was easy! Now chocolate-doom is totally playable :slight_smile:

1 Like

This is a very useful feature!

1 Like

Chocolate Doom has native gamepad support if you run chocolate-setup and configure it.

1 Like