Requests: Add ProcessHotkeyByCombination

Adds a new request called `ProcessHotkeyByCombination` which calls the
routine associated with a given hotkey. The hotkey is identified by the
combination of keys set by the user to trigger it. Multiple routines
are called if the same combination is bound to different hotkeys.
This commit is contained in:
LorenaGdL
2020-09-29 13:43:00 +00:00
parent bac5b1551b
commit 9daae857de
3 changed files with 47 additions and 0 deletions

View File

@ -44,6 +44,7 @@ const QHash<QString, RpcMethodHandler> WSRequestHandler::messageMap {
{ "BroadcastCustomMessage", &WSRequestHandler::BroadcastCustomMessage },
{ "ProcessHotkeyByName", &WSRequestHandler::ProcessHotkeyByName },
{ "ProcessHotkeyByCombination", &WSRequestHandler::ProcessHotkeyByCombination },
{ "SetCurrentScene", &WSRequestHandler::SetCurrentScene },
{ "GetCurrentScene", &WSRequestHandler::GetCurrentScene },

View File

@ -62,6 +62,7 @@ class WSRequestHandler {
RpcResponse BroadcastCustomMessage(const RpcRequest&);
RpcResponse ProcessHotkeyByName(const RpcRequest&);
RpcResponse ProcessHotkeyByCombination(const RpcRequest&);
RpcResponse SetCurrentScene(const RpcRequest&);
RpcResponse GetCurrentScene(const RpcRequest&);

View File

@ -366,3 +366,48 @@ RpcResponse WSRequestHandler::ProcessHotkeyByName(const RpcRequest& request) {
obs_hotkey_trigger_routed_callback(obs_hotkey_get_id(hk), true);
return request.success();
}
/**
* Executes hotkey routine, identified by bound combination of keys. A single key combination might trigger multiple hotkey routines depending on user settings
*
* @param {String} `key` Main key identifier (e.g. `OBS_KEY_A` for key "A"). Available identifiers [here](https://github.com/obsproject/obs-studio/blob/master/libobs/obs-hotkeys.h)
* @param {Object (Optional)} `modifiers` Optional key modifiers object: `{"shift":true/false, "alt":true/false, "control":true/false, "command":true/false}`. False entries can be ommitted
*
* @api requests
* @name ProcessHotkeyByCombination
* @category general
* @since unreleased
*/
RpcResponse WSRequestHandler::ProcessHotkeyByCombination(const RpcRequest& request) {
if (!request.hasField("key")) {
return request.failed("Missing request key parameter");
}
OBSDataAutoRelease data = obs_data_get_obj(request.parameters(), "modifiers");
obs_key_combination_t combo = {0};
uint32_t modifiers = 0;
if (obs_data_get_bool(data, "shift"))
modifiers |= INTERACT_SHIFT_KEY;
if (obs_data_get_bool(data, "control"))
modifiers |= INTERACT_CONTROL_KEY;
if (obs_data_get_bool(data, "alt"))
modifiers |= INTERACT_ALT_KEY;
if (obs_data_get_bool(data, "command"))
modifiers |= INTERACT_COMMAND_KEY;
combo.modifiers = modifiers;
combo.key = obs_key_from_name(obs_data_get_string(request.parameters(), "key"));
if (!modifiers
&& (combo.key == OBS_KEY_NONE || combo.key >= OBS_KEY_LAST_VALUE)) {
return request.failed("Invalid key-modifier combination");
}
// Inject hotkey press-release sequence
obs_hotkey_inject_event(combo, false);
obs_hotkey_inject_event(combo, true);
obs_hotkey_inject_event(combo, false);
return request.success();
}