# Hotkey Not Saved for Script



## dmadison (Feb 27, 2018)

I'm working on a small Python script using the new scripting options available in 21.x. The script is nothing special, but it relies on having an OBS hotkey set up. I've figured-out how to register a hotkey and everything works swimmingly while the program is open. _But_ as soon as OBS is closed and re-launched, the registered hotkey disappears and needs to be set again in settings.

The script is otherwise saving all of the settings from its properties, but the hotkey is not saved. I'm registering the hotkey in the "script_load" function.

Is this a bug, or is there something I'm missing? Any advice would be appreciated.


----------



## dmadison (Mar 5, 2018)

As it turns out, there _was_ something I was missing. If you want to save the hotkeys you need to keep track of them yourself.

There is a sample of this code in the `instant-replay.lua` script that comes bundled with OBS. Here it is for anyone that runs into a similar problem:


```
-- A function named script_load will be called on startup
function script_load(settings)
    hotkey_id = obs.obs_hotkey_register_frontend("instant_replay.trigger", "Instant Replay", instant_replay)
    local hotkey_save_array = obs.obs_data_get_array(settings, "instant_replay.trigger")
    obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)
end

-- A function named script_save will be called when the script is saved
--
-- NOTE: This function is usually used for saving extra data (such as in this
-- case, a hotkey's save data).  Settings set via the properties are saved
-- automatically.
function script_save(settings)
    local hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
    obs.obs_data_set_array(settings, "instant_replay.trigger", hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)
end
```

And here it is in Python:

```
# A function named script_load will be called on startup
def script_load(settings):
    hotkey_id = obs.obs_hotkey_register_frontend("instant_replay.trigger", "Instant Replay", instant_replay)
    hotkey_save_array = obs.obs_data_get_array(settings, "instant_replay.trigger")
    obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)

# A function named script_save will be called when the script is saved
#
# NOTE: This function is usually used for saving extra data (such as in this
# case, a hotkey's save data).  Settings set via the properties are saved
# automatically.
def script_save(settings):
    hotkey_save_array = obs.obs_hotkey_save(hotkey_id)
    obs.obs_data_set_array(settings, "instant_replay.trigger", hotkey_save_array)
    obs.obs_data_array_release(hotkey_save_array)
```


----------



## bozniauk (Feb 2, 2021)

Hi. I wonder if you might possibly help me. I have written some Python script which imports a list of names from a txt file and then randomly chooses one. I want a hotkey to activate it and a text source to be updated with the name. I am struggling to find any help, but your post suggests you might be able to help with hotkeys in python. Any help you might be able to give would be gratefully received. Thanks.


----------

