Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi!

Okay so it was kind of hard to summarize in topic but what I'm doing atm is an mp3 player that will allow me to change songs without having it focused. This is because I am often playing games and listening to music and every now and then i want to switch songs. Currently using spotify and it wont allow me to do it - so I figured I'd do it myself.

Now I've managed to write something using a tutorial project so i can indeed hijack a button like Ctrl + 4 or whatever but thing is if the game requires that particular combination of buttons it wont execute in the game.. Which sucks since Iw ant to make it so it functions regardless..

Basically, the way a keylogger works I guess. So I just "borrow" the keystroke and not straight up steal it.

Ideas?
Posted
Updated 24-Apr-11 22:39pm
v2

See Beginner's Tutorial - Using global hotkeys[^]

I wrote that 9 years ago, but I believe the concepts should still be the same. [unless they added a fully managed class in the framework in recent versions]

[Update]
-------------

Okay, my bad. I just realized that article is purely for native C++. The APIs will need to be P/Invoked from C#. I trust you can do that part.

[Update-2]
-------------

Here's some sample P/Invoke code: http://www.pinvoke.net/default.aspx/user32/RegisterHotKey.html[^]
 
Share this answer
 
v3
Comments
Ullvantar 25-Apr-11 6:34am    
This is sort of what I had working, but the problem remains. Say I use the code in your example there and press ctrl + a it does indeed bring it back even though it's not focused - however it removes the original function of ctrl + a (select all)

With this solution it is ofc possible to solve the problem of activating functions from another window however it doesn't solve the problem of removing the original function.. Thanks though!
Nish Nishant 25-Apr-11 8:56am    
You should not use standard keys as global short cuts. Typically people use Ctrl+Alt or Ctrl-Shift combinations that are usually safe to use as they don't have standard uses.
Ullvantar 25-Apr-11 13:00pm    
Yep, I guess I will just have to go for that. It's close enough to what I want :)
Thanks!
Nish Nishant 25-Apr-11 13:04pm    
You are welcome.
You need to hijack anything to just process the keystroke. You did not tag what library you want to use (Forms, WPF?), so it is not possible to give one universal answer; and I don't want to list all thinkable variable. You can easily achieve all you need by just handling KeyDown event an some controls. You can also use menu hot keys — they will work regardless what control is currently focused.

[EDIT]

After the question was tagged:
With System.Windows.Forms, you can also use System.Windows.Forms.Form.KeyPreview, see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.keypreview.aspx[^]. If you assign the valued for this property to true (while default it false), you will be able to process keyboard events before they go to controls; this is what you need.

—SA
 
Share this answer
 
v2
Comments
Ullvantar 25-Apr-11 6:35am    
Sorry I forgot, I've added it in tags now - it's about windows forms!
Ullvantar 25-Apr-11 13:06pm    
Haven't quite gotten this to work and settling for the global hotkey solution however I'm sure if I fiddled with this it would be an equally good solution :)
Sergey Alexandrovich Kryukov 25-Apr-11 13:16pm    
I hope you will get it; not to hard to achieve.
Thanks for accepting this answer.
Good luck,
--SA

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