cpyarger / obs-midi

Use MIDI devices to trigger events in OBS and vice versa
https://obs-midi.org
GNU General Public License v2.0
168 stars 20 forks source link

Add ability to map to Hotkeys functions #115

Closed lorado closed 3 years ago

lorado commented 3 years ago

In #111 and #112, @gdhgdhgdh an me wished the ability to run scripts via MIDI Messages.

This PR is a quick (and probably dirty) solution for this.

There is new action Trigger_Hotkey_By_Name defined in utils.h (I also found actions definition in actions.h, but they are not used at all, so I extended it only in utils.h)

There is also a new UI row for selection hotkey, which becomes visible, if you select Trigger_Hotkey_By_Name action. All Hotkeys, which are defined with obs_hotkey_register_frontend() function, are available. So if you want to run a script via MIDI Message, you have to define some hotkeys in your function script_load(settings) {...}. The first argument of obs_hotkey_register_frontend() defines the name of the hotkey, which is currently used to display hotkey in the UI options list.

So at the end, it acts like Hotkey binding for keyboard, but for MIDI Device.

With this implementation it is actually possible to achieve what we wanted.

I tested it locally on macOs with virtual MIDI Device (StreamDeck), it workes fine

Some possible improvements:

lorado commented 3 years ago

For someone, who want to test it locally - here is a build for Windows and mac: https://www.dropbox.com/s/nkq9mhwuv52v2wc/obs-midi-addjusted-by-lorado.zip?dl=0

cpyarger commented 3 years ago

Dont forget to add a translation for the action name in data/locale/en-us.ini once it is in that file, crowdin will go and add it to the rest of the translation files (:

cpyarger commented 3 years ago

Once we figure out how to translate the strings for the hotkeys I'll pull this in and make a release

lorado commented 3 years ago

Ok nice, thank you for cleaning the code up. I knew there is a possibility to do only one loop, to create a QStringList for Hotkeys, but as I said, I am not good at c++ :D

About Translations - we don't really need a translation, I guess. Hotkey has a description property, where human readable text is stored. This description is defined by obs_hotkey_register_frontend()as second parameter, which is actually also used in a regular Keyboard-Hotkey-Settings in OBS. We don't have to translate them on our own, but we have to store the name property of the hotkey object.

I'll try to solve this somehow...

cpyarger commented 3 years ago

So, there is one big problem I see, The list of triggers contains many duplicates, not just of existing actions which I have implemented, but also image

lorado commented 3 years ago

Hi @cpyarger,

as I said, I would not pull description directly. It is not really safe, as after changing UI language in OBS, hotkeys descriptions will be different (I guess, but it makes sense to me). So after switching language, mapping will not work anymore

lorado commented 3 years ago

I am working right now on the solution - using QPair, should work. Give me a time please

cpyarger commented 3 years ago

No problem, I just was pushing up changes to see if it worked, Also we need to filter out the excess hotkeys, as it currently stands there are way to many extras being added at the moment.

cpyarger commented 3 years ago

one last push to refix the break i had

lorado commented 3 years ago

The problem with apparently duplicated hotkeys - as I said, it polls all the hotkeys. So you can take a look into Settings -> Hotkey section, to see which hotkeys you actually have. They are there separated into different blocks (see screenshot)

Bildschirmfoto 2021-03-13 um 21 08 04

We could probably filter some basic stuff, which you already implemented using other actions. I mean those buttons names start with OBSBasic.*, libobs.*.

I will test it ;)

cpyarger commented 3 years ago

What about triggering scripts, since this is about doing that, instead of having it be a generic "trigger hotkeys" why not filter out everything but script triggers and have it be a "trigger script" action

cpyarger commented 3 years ago

most everything else I can build into the normal actions if I haven't already (:

lorado commented 3 years ago

I think using hotkeys makes it actually easier to run custom stuff. So to be able to run some script - simply add a hotkey. Also, when you define a hotkey, you define what function should be executed by triggering this hotkey. Also I don't know how to poll script list, and then there is also a question - what function is the entry point of the script? Using hotkey in script you don't have these issues.

cpyarger commented 3 years ago

How do scripts show up in our hotkeys list? they should be there correct? I dont have any scripts that trigger on a hotkey. so I can't test that

lorado commented 3 years ago

You can take a look into default instant-replay.lua shipped with current OBS by default.

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

obs_hotkey_register_frontend() defines a hotkey.

And I catched up this idea, because I found a way to tell OBS programmatically, that some hotkey was triggered - obs_hotkey_trigger_routed_callback()

cpyarger commented 3 years ago

I have cleaned up my aditions and removed description.

cpyarger commented 3 years ago

I would have been too easy if obs pretended script hotkey names with .script XD

lorado commented 3 years ago

Could you somehow help me to get build working on my macos machine? It is really annoying I am not able to build locally =\

cpyarger commented 3 years ago

Added the filter

cpyarger commented 3 years ago

sure want to hop into discord? ill be in the common room https://discord.gg/2QXwwwQS

cpyarger commented 3 years ago

Since we will be saving the hotkeys by their name, and not description, I am going to pull this in for now, When you have the hotkey description for the ui fixes complete, Just issue a new PR