❔ 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
is it the "global" part or the "play/pause" part that's troublesome?
I'm guessing "global"?
more the play/pause than the global,
tbh i cant tell if the gobal is working until i can detect the play/pause
basically, you need to bypass Unity and go straight to the OS
so, RIP if you want this application to be cross-platform
i dont care about that, its just a personal music player
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 ToubVirtual-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.
i prefer using raw input instead. im going to write a good c# wrapper for it soon
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.
im not very good with low level stuff so im not really sure whats going on there
i get its confusing if ur not familiar with it
hold on
this is where i'm at the moment but it doesnt work.
i thought i hade already shared my code in this server but i cant find it. people often come asking for this stuff
i kinda thought this wasnt a common thing to do based on the lack of threads online
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.
i looked at this but its all in C++ ?
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
P/Invoke
@MasterAirscrach this should be easiest
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-registerhotkey
RegisterHotKey function (winuser.h) - Win32 apps
Defines a system-wide hot key.
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)
@MasterAirscrach have you tried hotkeys?
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.
so you kind of just need to figure out how to get messages from the message loop
yea i guessed there was somthing like that, i just gotta figure out what that means and how to do it
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
makes sense, ill look into how this can be accessed in unity
you dont have to create a new sta thread for the message loop
SetWindowSubclass function (commctrl.h) - Win32 apps
Installs or updates a window subclass callback.
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
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.
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
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.