# Sending hotkeys to OBS using PostMessage in C#



## hinough (Jan 4, 2019)

Hey! Not sure if this is the right place to post this, but thought Id give it a shot.
I'm making a chatbot which sends hotkeys to OBS using the PostMessage function from user32.dll in C#. Im getting it to work... sort of.
It works only after I open and close the setting window in OBS. And it stays working after that until I click and manually change something (Like a scene). At that point Ill have to open and close the Settings window again to get the hotkeys to work from my app.
They work as they should without issues when I actually click the keys on my keyboard.
So my question is. Is there a message, command or in some way something Im missing to send to OBS that makes it bug out?

As an example. Pushing the A key:
When actually pusing the key on my keyboard it registers the following keyboard events in spy++
WM_KEYDOWN with a wParam 00000041 and lParam 001E0001
WM_CHAR with the A character
WM_KEYUP with a wParam 00000041 and a lParam C01E0001

Ive been able to recreate these very messages in the bot Im creating. And as mentioned over, they work. BUT its that issue with having to click the settings button once to make it work in the first place and any time I click anything else I have to reopen the settings window.

Am I missing something stupidly easy here like a message of sorts? Is there a bug there? 
*If you want the whole sourcecode just poke at me, but the part that does the actual message sending (the "keystrokes") looks like so:*

```
[DllImport("user32.dll")]                                                                 
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
...
PostMessage(*mainwindowhandle from OBS proc*, 0x0100, (IntPtr)*hotkeyvalue(0x41 for a), (IntPtr)0x001E0001);
PostMessage(*mainwindowhandle from OBS proc*, 0x0101, (IntPtr)*hotkeyvalue(0x41 for a), (IntPtr)0xC01E0001);
```


----------



## dodgepong (Jan 5, 2019)

If you're trying to build an app that can control OBS from C#, it honestly would probably be better to install the websocket plugin and then communicate with OBS over websockets from C# rather than try to simulate keypresses.


----------



## hinough (Jan 5, 2019)

dodgepong said:


> If you're trying to build an app that can control OBS from C#, it honestly would probably be better to install the websocket plugin and then communicate with OBS over websockets from C# rather than try to simulate keypresses.


Yes and I have. Using the OBS-Websocket plugin, but that doesnt support sourcemuting/unmuting. So my plan is to make it to where it changes scenes with muting/unmuting a source in a scene. At least unless I'm blind and or didnt catch that it can do just that.

Edit: Thinking about it now that might just be a restriction from the C# wrapper Im using. Checking into that now


----------



## dodgepong (Jan 5, 2019)

I believe you can set or toggle the mute status of a source with these:

https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md#setmute
https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md#togglemute


----------



## hinough (Jan 6, 2019)

dodgepong said:


> I believe you can set or toggle the mute status of a source with these:
> 
> https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md#setmute
> https://github.com/Palakis/obs-websocket/blob/4.x-current/docs/generated/protocol.md#togglemute


Indeed I can. Guess I went a horribly wrong direction with not making the communcation bit of the bot myself to fit my needs. Thanks for the heads up!


----------



## cyberpunkoff (Aug 31, 2020)

I don't know why but your code works perfectly fine for me. Thanks!


----------



## knaufinator (Feb 11, 2021)

I have found this package to work sending key press to OBS ,.. websocket simply is not sufficient for interacting with OBS. 









						Henooh.DeviceEmulator 1.1.7
					

Using Windows Forms or Windows WPF SendKey methods can emulate text entry, but not actual key strokes. Other available input simulators or emulators lack ability to send key strokes that is behind DirectX layers, and rarely provides output features as one package. HenoohDeviceEmulator provides...




					www.nuget.org
				




This will send a ctrl+A in the background. 

KeyboardController keyboardController = new Henooh.DeviceEmulator.KeyboardController();
                    keyboardController.Control(Henooh.DeviceEmulator.Native.VirtualKeyCode.VK_A);


----------

