Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# keypress
Hey guy, what is the best way to detect if Control key, or Shift key is pressed, without my form is on focus
Posted 10-Apr-13 13:35pm
Comments
Sergey Alexandrovich Kryukov at 10-Apr-13 18:43pm
   
System.Windows.Forms? Tag: "WinForms". Something else? Tag it properly.
—SA
Sergey Alexandrovich Kryukov at 10-Apr-13 19:07pm
   
Why doing so? You should better explain your ultimate goals, that way you could get an advice even if the technical approach you might be preoccupied with is wrong...
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

This is impossible if you use .NET FCL, because this is not really needed. For your information, "focus" means "keyboard focus" and nothing else. The difference you see on screen is no more than a visual clue to show the focus. As a form has no keyboard focus, it does not accept keyboard events at all, by definition.
 
You still can capture the keyboard and many other events dispatched to the processes other then your application, but it is impossible to do with pure C#. It looks like the only way to capture such event it the Windows Hook: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^].
 
So, why cannot you simply install such hook with C# and P/Invoke? Well, because, it has to be the Global Hook, as you need to capture keyboard events system-wide. And, according to Microsoft Hook documentation, the hook function should reside in a separate native (unmanaged) DLL. You can create such DLL, but not using C#. Then you can develop some mechanism to load DLL and communicate between this DLL and your .NET application. I've done this with unmanaged application, but it's not a problem to do it with .NET.
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

if ((Control.ModifierKeys & Keys.Shift) != 0)
 
This will be true if shift key is down..
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 295
2 Sergey Alexandrovich Kryukov 197
3 Peter Leow 155
4 Afzaal Ahmad Zeeshan 154
0 OriginalGriff 7,510
1 DamithSL 5,494
2 Sergey Alexandrovich Kryukov 4,954
3 Maciej Los 4,936
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100