Skip to content

Instantly share code, notes, and snippets.

@upgradeQ
Created November 22, 2020 18:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save upgradeQ/654914cdc0bc180f1617a9b16eefc948 to your computer and use it in GitHub Desktop.
Save upgradeQ/654914cdc0bc180f1617a9b16eefc948 to your computer and use it in GitHub Desktop.
Hotkey enumeration via obs_enum_hotkeys
local obs = obslua
local ffi = require 'ffi'
local obsffi
if ffi.os == "OSX" then
obsffi = ffi.load("obs.0.dylib")
else
obsffi = ffi.load("obs")
end
ffi.cdef[[
typedef struct obs_hotkey obs_hotkey_t;
typedef size_t obs_hotkey_id;
const char *obs_hotkey_get_name(const obs_hotkey_t *key);
typedef bool (*obs_hotkey_enum_func)(void *data, obs_hotkey_id id, obs_hotkey_t *key);
void obs_enum_hotkeys(obs_hotkey_enum_func func, void *data);
]]
function trigger()
local target = 'OBSBasic.StartVirtualCam'
local htk_id
function callback_htk(data,id,key)
local name = obsffi.obs_hotkey_get_name(key)
if ffi.string(name) == target then -- (1)
htk_id = tonumber(id) -- (2)
print('found target hotkey id: ' .. htk_id)
return false
else
return true
end
end
local cb = ffi.cast("obs_hotkey_enum_func",callback_htk) -- (3)
obsffi.obs_enum_hotkeys(cb,data)
if htk_id then
obs.obs_hotkey_trigger_routed_callback(htk_id,false)
obs.obs_hotkey_trigger_routed_callback(htk_id,true) -- (4)
obs.obs_hotkey_trigger_routed_callback(htk_id,false)
end
end
function htk_1_cb(pressed)
if pressed then
trigger()
end
end
key_1 = '{"htk_1": [ { "key": "OBS_KEY_1" } ]}'
json_s = key_1
default_hotkeys = {
{id='htk_1',des='launch virtual cam',callback=htk_1_cb},
}
function script_load(settings)
s = obs.obs_data_create_from_json(json_s)
for _,v in pairs(default_hotkeys) do
local a = obs.obs_data_get_array(s,v.id)
h = obs.obs_hotkey_register_frontend(v.id,v.des,v.callback)
obs.obs_hotkey_load(h,a)
obs.obs_data_array_release(a)
end
obs.obs_data_release(s)
end
@upgradeQ
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment