Sending hotkeys to OBS using PostMessage in C#

hinough

New Member
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:
Code:
[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

Administrator
Forum Admin
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

New Member
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
 
Last edited:

knaufinator

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


This will send a ctrl+A in the background.

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