clockworkpi

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.

5 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 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!

8 Likes

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

6 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.
4 Likes