Click here to Skip to main content
16,014,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an software which starts playing when the F5 key is pressed and stop playing when F7 is pressed. what I need is detect the same F5 and F7 to start and stop my application. Is this possible??

What I have tried:

I tried using hot key in C# using "user32" dll file. it is working only when my another software is not in focus. Can anyone guide me.
Posted
Updated 16-Sep-20 20:31pm

1 solution

Hotkeys only work when your app has the focus - otherwise we couldn't use them at all - think how many key combinations Excel uses and how many of them are also used (for a different purpose) by Word. If hotkeys were spread across all apps, then which one gets F2 when I press it?

What you are trying to do needs something a lot more complicated: Global Hooks - and there are problems here.
1) Global hooking in C# is possible, but very complicated and prone to error. When errors happen, your whole system can easily become unstable. And that includes while you are trying to develop the code! Backup well, backup often.
2) Using F5 and F7 as "global keys" will mess you up, not just users. F5 is Visual Studio for "run" so if your app grabs it, you can't develop software at all easily any more.
3) Global hooking is one way to allow keyloggers to work, so do not expect it to work in future versions of Windows - security concerns may mean it gets disabled or requires elevation.
4) You start grabbing keys off users to make your app work and some - if not most - are going to get very, very annoyed. Many of my apps use most of the function keys (with and without SHIFT, CTRL, AN DALT combinations) as "Power user shortcuts" and if you start grabbing them your software is goign to get uninstalled with extreme prejudice ...

If after that you decide to go ahead, then start here: Global Hooks C#[^] - but don't say you weren't warned!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900