❔ Global Listener for the media key (play/pause)

I made a music player in unity (because easy ui) it works great but i have no clue how to setup a global keybind for play/pause (unity inputs doesnt have it)
32 Replies
JakenVeina
JakenVeina14mo ago
is it the "global" part or the "play/pause" part that's troublesome? I'm guessing "global"?
MasterAirscrach
MasterAirscrachOP14mo ago
more the play/pause than the global, tbh i cant tell if the gobal is working until i can detect the play/pause
JakenVeina
JakenVeina14mo ago
basically, you need to bypass Unity and go straight to the OS so, RIP if you want this application to be cross-platform
MasterAirscrach
MasterAirscrachOP14mo ago
i dont care about that, its just a personal music player
JakenVeina
JakenVeina14mo ago
you'll have to write a unique implementation for this for each platform you want to support unless you can maybe find a cross-platform wrapper lib on github for Windows, you need to call SetWindowsHookEx in user32.dll as far as detecting play/pause, you just need to identify the key code I dunno why Unity wouldn't have those keycodes mapped in its own event handler system, but since you're bypassing it anyway, you'll follow the docs for SetWindowsHookEx refer to the infamous Stephen Toub
JakenVeina
JakenVeina14mo ago
Virtual-Key Codes (Winuser.h) - Win32 apps
The following table shows the symbolic constant names, hexadecimal values, and mouse or keyboard equivalents for the virtual-key codes used by the system. The codes are listed in numeric order.
Jester
Jester14mo ago
i prefer using raw input instead. im going to write a good c# wrapper for it soon
Jester
Jester14mo ago
Raw Input - Win32 apps
This section describes how the system provides raw input to your application and how an application receives and processes that input.
MasterAirscrach
MasterAirscrachOP14mo ago
im not very good with low level stuff so im not really sure whats going on there
Jester
Jester14mo ago
i get its confusing if ur not familiar with it hold on
MasterAirscrach
MasterAirscrachOP14mo ago
this is where i'm at the moment but it doesnt work.
Jester
Jester14mo ago
i thought i hade already shared my code in this server but i cant find it. people often come asking for this stuff
MasterAirscrach
MasterAirscrachOP14mo ago
i kinda thought this wasnt a common thing to do based on the lack of threads online
Jester
Jester14mo ago
for raw input you just have to register it to listen to the keyboard and then in your window message proc you get the data on WM_INPUT https://learn.microsoft.com/en-us/windows/win32/inputdev/using-raw-input
Using Raw Input - Win32 apps
This section includes sample code for tasks relating to raw input.
MasterAirscrach
MasterAirscrachOP14mo ago
i looked at this but its all in C++ ?
Jester
Jester14mo ago
well the windows api is in C (others in C++) so you have to get it from there into C# but im planning on making a C# wrapper now im thinking about it maybe you can use hotkeys instead
JakenVeina
JakenVeina14mo ago
P/Invoke
JakenVeina
JakenVeina14mo ago
all the examples are in C++, sure, but the OS APIs that you need to call are essentially just static methods that exist inside some native DLLs you can map those into static methods in C# pretty easily that's known as P/Invoke (for whatever reason)
Jester
Jester14mo ago
@MasterAirscrach have you tried hotkeys?
MasterAirscrach
MasterAirscrachOP14mo ago
im trying, im really lost when it comes to low level stuff so im getting chatGPT to generate template stuff in c# and figure it out from there.
Jester
Jester14mo ago
so you kind of just need to figure out how to get messages from the message loop
MasterAirscrach
MasterAirscrachOP14mo ago
yea i guessed there was somthing like that, i just gotta figure out what that means and how to do it
Jester
Jester14mo ago
perhaps like the bottom of this page you could make a new thread with its own message loop? every window in windows has a message loop, trough it it knows when to move, where you click on it, when it has to close and everything
MasterAirscrach
MasterAirscrachOP14mo ago
makes sense, ill look into how this can be accessed in unity
sibber
sibber14mo ago
you dont have to create a new sta thread for the message loop
Jester
Jester14mo ago
im working on my global keyboard input project in my spare time im just not sure how to make the api nicest to use. atm it runs on its own thread and calls your given delegate on every input. if its done you could maybe make use of it @MasterAirscrach
Jester
Jester14mo ago
GitHub
GitHub - QubitTooLate/Keylogging: Useful wrapper for Hot Keys and R...
Useful wrapper for Hot Keys and Raw Input in C#. Contribute to QubitTooLate/Keylogging development by creating an account on GitHub.
Will
Will14mo ago
there are a few popular libraries which are easy to use that do this too https://github.com/thomaslevesque/NHotkey https://github.com/TolikPylypchuk/SharpHook
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server