There is a WPF application (audio player). It is necessary to add the ability to catch the pressing of certain keys, even when the application is minimized. For example, when you press Num4 , to stop playback, when you press other keys (depending on the settings, to continue playing music, etc.). I tried to implement it through the GetAsyncKeyState(int vkey) function from user32.dll , but this does not quite suit me, here is a sample code:
public static class KeyState { public static void Wait(Key key, Action<KeyStates> action) { var asyncOperation = AsyncOperationManager.CreateOperation(null); SendOrPostCallback callBack = state => action((KeyStates) state); ThreadPool.QueueUserWorkItem(state => { var vk = KeyInterop.VirtualKeyFromKey(key); var prev = ((GetAsyncKeyState(vk) & 0x8000) == 0x8000); if (prev) { asyncOperation.Post(callBack, KeyStates.Down); } while (true) { var res = ((GetAsyncKeyState(vk) & 0x8000) == 0x8000); if (res != prev && res) { asyncOperation.Post(callBack, KeyStates.Down); } prev = res; } }); } [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)] private static extern short GetAsyncKeyState(int vkey); } The problem here is that I not only need to catch the keystroke, but also prevent the “advancement” of the keystroke further. Those. keystroke should be intercepted by my application and not fall into others (for example, in a text editor). I would be grateful for the help. If someone else tells you how to correctly implement this for an application that is deployed through ClickOnce , it would be great at all. Thank!