Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My application is in background and i want to know if user press combination of control and z button from keyboard?

I can only detect if Control is pressed but how do i can detect combination of both control and z key?

What I have tried:

C#
if (Control.ModifierKeys == Keys.Control)
                MessageBox.Show("Control pressed");
Posted
Updated 4-May-16 20:59pm
v2
Comments
Philippe Mori 4-May-16 12:32pm    
You should never handle such common hotkey when your app'ication is in the background as it might have unintended side effect on other applications. If you want global shortcut, either use one with 3 or 4 keys or let the user select the combination or both...

If your app doesn't have the input focus it'll never see the keystrokes in those events.

You either need to implement a Global Hotkey or a Global Keyboard Hook. The two are NOT the same thing even though there are examples out there that registry for a Global Hotkey and pack the code inside a "KeyboardHook" class.

Google for "C# Global Hotkey" for examples.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-May-16 11:01am    
5ed. I wrote more detailed answer in Solution 4.
—SA
Dave Kreskowiak 4-May-16 11:32am    
Thanks. Someone with equal weight voted me a one within two minutes of my post. WTF?
Sergey Alexandrovich Kryukov 4-May-16 12:41pm    
As to those down-votes... someone (just one, it looks like, the one with the same weight) come regularly and down-vote nearly all my answers, doing it withing one minute. This is a pattern of a hatred voting (something like that), because it's not possible to really evaluate 6 or 8 answers (sometimes). Are you facing anything similar?
—SA
Dave Kreskowiak 4-May-16 14:11pm    
No, it's just this one time.
vast25 wrote:

in this code e is EventArgs so it will also not work if my app in background.
This is a different story. But, first of all, you should not "know" if a key is hit; you need to handle events. Solution 1 and 2 did not explained it to you and are just wrong, written out of context.

Now, as your application may be in the background (in the sense you mean), that is, with no activated window (it may have no windows at all or none of them are activated), you can register a hot key globally for the whole system. This is how it is done:
RegisterHotKey function (Windows)[^],
UnregisterHotKey function (Windows)[^].

To do it for .NET, you need to use P/Invoke. Everything is already done for you here:
http://www.pinvoke.net/default.aspx/user32.registerhotkey[^],
http://www.pinvoke.net/default.aspx/user32.unregisterhotkey[^].

You can get more flexibility if you set up a global Windows Hook, but this is much harder. For a global hook, you would need to set up the hook in a native DLL, not your .NET assembly. Hook code is quite hard to debug, there is a lot of danger to destabilize your system for a while, and so on. Anyway, read about it:
Hooks (Windows)[^],
Hooks Overview (Windows)[^],
SetWindowsHookEx function (Windows)[^].

—SA
 
Share this answer
 
Comments
Philippe Mori 4-May-16 12:30pm    
But I would not recommand using a well know shortcut as a global hot key.
Sergey Alexandrovich Kryukov 4-May-16 12:45pm    
Agree. Good point.
—SA
You have to check for both keys in the OnKeyDown event like:

C#
protected override void OnKeyDown(KeyEventArgs e)
 {
      if (e.KeyCode == Keys.Z && e.Modifiers == Keys.Control)
      {
            MessageBox.Show("Hello world");
      }
      base.OnKeyDown(e);
  }


als you can look at this SO post
 
Share this answer
 
v2
Comments
vishal2592 4-May-16 10:31am    
This code is only work on form but i want to determine if my application is in background.
CHill60 4-May-16 10:32am    
Minor typing error - that should be e.KeyCode == Keys.Z
Ehsan Sajjad 4-May-16 10:41am    
it happens when copy pasted :D
 
Share this answer
 
v3
Comments
vishal2592 4-May-16 10:34am    
in this code e is EventArgs so it will also not work if my app in background.
Karthik_Mahalingam 4-May-16 10:40am    
in which event you are checking?
Karthik_Mahalingam 4-May-16 10:51am    
check the link in updated solution.

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