public bool TriggerHotkey(string obsKeyCode, bool ctrl, bool alt, bool shift, bool win) { try { if (IsConnected) { KeyModifier keyModifier = KeyModifier.None; if (ctrl) { keyModifier |= KeyModifier.Control; } if (alt) { keyModifier |= KeyModifier.Alt; } if (shift) { keyModifier |= KeyModifier.Shift; } if (win) { keyModifier |= KeyModifier.Command; } OBSHotkey hotkey = (OBSHotkey)Enum.Parse(typeof(OBSHotkey), obsKeyCode, true); obs.TriggerHotkeyBySequence(hotkey, keyModifier); return(true); } } catch (Exception ex) { Logger.Instance.LogMessage(TracingLevel.ERROR, $"TriggerHotkey Exception: {ex}"); } return(false); }
/// <summary> /// Executes hotkey routine, identified by bound combination of keys. A single key combination might trigger multiple hotkey routines depending on user settings /// </summary> /// <param name="key">Main key identifier (e.g. OBS_KEY_A for key "A"). Available identifiers are here: https://github.com/obsproject/obs-studio/blob/master/libobs/obs-hotkeys.h</param> /// <param name="keyModifier">Optional key modifiers object. You can combine multiple key operators. e.g. KeyModifier.Shift | KeyModifier.Control</param> public JObject TriggerHotkeyBySequence(OBSHotkey key, KeyModifier keyModifier = KeyModifier.None) { var requestFields = new JObject { { "keyId", key.ToString() }, { "keyModifiers", new JObject { { "shift", (keyModifier & KeyModifier.Shift) == KeyModifier.Shift }, { "alt", (keyModifier & KeyModifier.Alt) == KeyModifier.Alt }, { "control", (keyModifier & KeyModifier.Control) == KeyModifier.Control }, { "command", (keyModifier & KeyModifier.Command) == KeyModifier.Command } } } }; return(SendRequest("TriggerHotkeyBySequence", requestFields)); }
private static void Main(string[] args) { InitKeyCommands(); GetLocalIPAddress(); if (password == null) { Console.WriteLine("Enter OBS Websocket password"); password = Console.ReadLine(); var fileContent = JsonConvert.SerializeObject(new JObject { { "ipAddress", ipAddress }, { "portNumber", portNumber }, { "password", password } }); File.WriteAllText("config.json", fileContent); } Console.WriteLine($"Ipadres: {ipAddress}:{portNumber}"); var server = new TcpListener(IPAddress.Parse(ipAddress), portNumber); server.Start(); var connection = new OBSWebsocket(); connection.Connect($"ws://{"127.0.0.1"}:{4444}", password); var bytes = new byte[256]; // Enter the listening loop. while (true) { Console.Write("Waiting for a connection... "); // Perform a blocking call to accept requests. // You could also use server.AcceptSocket() here. var client = server.AcceptTcpClient(); Console.WriteLine("Connected!"); if (whiteListedIpAddresses.Count > 0) { // check if the ip is whitelisted var remoteIp = client.Client.RemoteEndPoint.ToString(); remoteIp = remoteIp.Substring(0, remoteIp.IndexOf(":")); var localIp = client.Client.LocalEndPoint.ToString(); localIp = localIp.Substring(0, localIp.IndexOf(":")); } // Get a stream object for reading and writing. var stream = client.GetStream(); int i; // Loop to receive all the data sent by the client. while ((i = stream.Read(bytes, 0, bytes.Length)) != 0) { // Translate data bytes to a ASCII string. var data = Encoding.ASCII.GetString(bytes, 0, i); Console.WriteLine("Received: {0} from {1}", data, client.Client.RemoteEndPoint.ToString()); if (CommandSupported(data)) { var keyCommand = keyCommands.First(kc => kc.CommandId == data); var control = false; var shift = false; var alt = false; OBSHotkey key = OBSHotkey.OBS_KEY_NONE; foreach (var keyPress in keyCommand.KeyPresses.Where(kp => kp.Pause == 0 && kp.KeyDown)) { if ((VirtualKeyCode)keyPress.Key == VirtualKeyCode.LCONTROL) { control = true; continue; } if ((VirtualKeyCode)keyPress.Key == VirtualKeyCode.MENU) { alt = true; continue; } if ((VirtualKeyCode)keyPress.Key == VirtualKeyCode.LSHIFT) { shift = true; continue; } key = keyMapper[(VirtualKeyCode)keyPress.Key]; } if (key != OBSHotkey.OBS_KEY_NONE) { KeyModifier modifiers = KeyModifier.None; if (control) { modifiers |= KeyModifier.Control; } if (alt) { modifiers |= KeyModifier.Alt; } if (shift) { modifiers |= KeyModifier.Shift; } var response = connection.TriggerHotkeyBySequence(key, modifiers); } /*var sim = new InputSimulator(); * * foreach (var keyPress in keyCommand.KeyPresses) * { * if (keyPress.Pause > 0) * { * sim.Keyboard.Sleep(keyPress.Pause); * } * else if (keyPress.KeyDown) * { * sim.Keyboard.KeyDown((VirtualKeyCode)keyPress.Key); * } * else * { * sim.Keyboard.KeyUp((VirtualKeyCode)keyPress.Key); * } * }*/ } // Process the data sent by the client. data = data.ToUpper(); var msg = Encoding.ASCII.GetBytes(data); // Send back a response. stream.Write(msg, 0, msg.Length); Console.WriteLine("Sent: {0}", data); } // Shutdown and end connection client.Close(); } }