Utilitaires
Scripts
Divers
Jeux
Rechercher
Quillevere.net
Computer techniques

Retroarch: use Hotkey and dedicated buttons at the same time

27/07/2025
We will see here how to load and save a game with a simple click of a button, while retaining the functionality of the Hotkey

On Retroarch, there are 'system' commands; they allow you to display the menu, pause, reset the ROM, load/save the game state...

However, there is a limitation to using these commands, but let's first see how they work before showing how to enhance their usage: assign a key for loading and another for saving!

Possible functions: the Hotkey

Two possibilities are offered to customize the trigger for these functions:

  1. Use the Hotkey button
    This involves designating a key that, when combined with another, triggers the system command. This key is called the 'Hotkey.'
    Therefore, you need to designate several other keys for each command and press the Hotkey + the command key simultaneously.
  2. Do not use a hotkey button
    You then need to assign a key specifically to a command. This key will only perform the indicated command and requires having as many keys as there are desired commands.

Disadvantages

The first method, with a configured Hotkey, is the default on Retroarch and is probably the most used because it offers many practical features by reusing existing keys for game commands (if there are 8 function keys and a stick, this allows for 11 commands). However, it has the disadvantage of a lack of speed.

Certainly, if you just want to exit the current game, pressing 2 keys at the same time is not a timing issue in itself. However, if you want to save a game quickly at a moment when time is crucial, locating and pressing 2 keys simultaneously takes a time lapse that can be critical depending on the game. And there is a risk of error (for example, you might confuse saving with loading).

The second method (no Hotkey) no longer has the disadvantage of the critical time lapse for saving but requires having as many keys available as there are functions to use: you need one key to exit the game, another to access Retroarch settings, another to load, another to save, and two more to navigate through the saves... In short, a whole keyboard!

A method at the crossroads

Ideal would have been to configure Retroarch to indicate that a certain system function is called either by a dedicated key or by a Hotkey+key combination.

By looking through the Retroarch documentation, we learn that it listens to certain ports, notably port 55355, to allow executing commands over the network: exit, take screenshots, adjust volume... This is indicated here.

In the Batocera documentation (which launches Retroarch), we learn that we can create a key configuration file to launch scripts. This documentation suggests adjusting the brightness or volume of Batocera when certain keys are pressed.

You understand the idea: when pressing a specific key, the goal is to send a local network command that will be processed by Retroarch. The Hotkey remains in use, but direct calls to certain keys are intercepted to send commands to Retroarch.

Implementation to load and save quickly

Know the name of the dedicated keys

The ideal is to have one key to save the state of a game and another key to load that state.

First, you will need to find the technical name of these 2 keys. One method to achieve this is to connect via SSH to your machine and launch the evtest software.

This will display, when you press a key, the status and name of that key. In my case, I get this for the 'save' key:

type : 1 (EV_KEY), code 296 (BTN_BASE3), value 1 => 0

And that for the 'Load' touch:

type : 1 (EV_KEY), code 297 (BTN_BASE4), value 1 => 0

Create a script

I wrote a script in two differents languages (Python and Bash). Both work in my case, but I have a preference for the Bash script as it is probably faster. I will provide both here, to choose according to individual preferences...

The different outputs (echo and print) will obviously not be displayed and are only for debugging purposes.

Both scripts check if Retroarch is running. If it is, they will initiate the loading or saving of the game state. If it is not, they will change the brightness on EmulationStation (note that this point may not work on all installations).

Python

Save the code below in the file /userdata/system/configs/game_state_changer.py.

#!/usr/bin/python3
# Si retroarch est lancé :
# Si argument=1, sauvegarde la partie
# Si argument=2, charge la dernière partie sauvée
# Si retroarch n'est pas lancé :
# Si argument=1, baisse la luminosité
# Si argument=2, augmente la luminosité

import sys # Pour récupérer le paramètre
import subprocess # Pour détecter si retroarch est en cours d'exécution
import socket # Pour sock.sendto

# Vérifie si retroarch est en cours d'exécution
def is_retroarch_running():
    try:
        result = subprocess.run(['pgrep', '-x', 'retroarch'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        if result.stdout:
            return True # RetroArch est en cours d'exécution
        else:
            return False # RetroArch n'est pas en cours d'exécution
    except Exception as e:
        print(f"Une erreur s'est produite : {e}")
        return False

def modifie_luminosite(valeur):
    try:
        result = subprocess.run(['/overlay/base/usr/bin/batocera-brightness', valeur], check=True, text=True, capture_output=True)
        print("Sortie standard :", result.stdout)
        print("Erreur standard :", result.stderr)
    except subprocess.CalledProcessError as e:
        print("Une erreur s'est produite :", e)

# Récupérer le paramètre (1=sauver, 2=charger)
fonction = sys.argv[1]

if is_retroarch_running():
    print("RetroArch est en cours d'exécution.")

 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 if fonction=='1':
    sock.sendto(b"SAVE_STATE", ("127.0.0.1", 55355))
 elif fonction=='2':
    sock.sendto(b"LOAD_STATE", ("127.0.0.1", 55355))
else:
    print("RetroArch n'est pas en cours d'exécution.")
    if fonction=='1':
        modifie_luminosite(-5)
    elif fonction=='2':
        modifie_luminosite(+5)

Puis accordez les droits d'exécution :

chmod +x /userdata/system/configs/game_state_changer.py

Bash

Save the code below in the file /userdata/system/configs/game_state_changer.sh.

#!/bin/bash
# Si retroarch est lancé :
# Si argument=1, sauvegarde la partie
# Si argument=2, charge la dernière partie sauvée
# Si retroarch n'est pas lancé :
# Si argument=1, baisse la luminosité
# Si argument=2, augmente la luminosité

# Vérifie si retroarch est en cours d'exécution
is_retroarch_running() {
    pgrep -x "retroarch" > /dev/null 2>&1
    return $?
    }

modifie_luminosite() {
    local valeur=$1
    /overlay/base/usr/bin/batocera-brightness "$valeur"
    }

# Récupérer le paramètre (1=sauver, 2=charger)
fonction=$1

if is_retroarch_running; then
    echo "RetroArch est en cours d'exécution."

    if [ "$fonction" == "1" ]; then
        echo "Sauvegarde de l'état..."
        printf "SAVE_STATE" > /dev/udp/127.0.0.1/55355
    elif [ "$fonction" == "2" ]; then
        echo "Chargement de l'état..."
        printf "LOAD_STATE" > /dev/udp/127.0.0.1/55355
    fi
else
    echo "RetroArch n'est pas en cours d'exécution."
    if [ "$fonction" == "1" ]; then
        echo "Baisse de la luminosité..."
        modifie_luminosite -5
    elif [ "$fonction" == "2" ]; then
        echo "Augmentation de la luminosité..."
        modifie_luminosite +5
    fi
fi

Puis accordez les droits d'exécution :

chmod +x /userdata/system/configs/game_state_changer.sh

Configure Batocera

To capture keys on Batocera, create the file /share/system/configs/multimedia_keys.conf.

This file is automatically sought and loaded at start -up, nothing else to do.

For Python

BTN_BASE3 1 python /userdata/system/configs/game_state_changer.py 1
BTN_BASE4 1 python /userdata/system/configs/game_state_changer.py 2

For Bash

BTN_BASE3 1 /userdata/system/configs/game_state_changer.sh
BTN_BASE4 1 /userdata/system/configs/game_state_changer.sh

Check commands

Batocera needs to be restarted so that the configuration of the keys is effective.

Once a game is launched, if you press 'save' or 'load' button, you should have a message at the bottom of the screen indicating this condition.

Dernière modification le 27/07/2025 - Quillevere.net

Commentaires

No inscription needed if you wish to

Search in this website

fr en rss RSS info Informations