Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I tried to detect key event in mouse up event in text box. Currently I declare public boolean variable that detect whether control key is pressd or not which is further set true or false in key down event and key up event of that text box.At the same time, in mouse up event ,I have to get the control-key-pressing condition via that public variable. It's working but I feel it is not traditional and not totally smart.( I will have to add more variable when I want to detect further keys like (Shift, Alt)). Is there any way to detect key currently pressing during mouse event.
Posted

Unfortunately at the moment there isn't. I ran into the same issue a few weeks ago and ended up doing exactly what you have done. The only solution is to import methods from the User32.dll but I always find imports a little messy however, if you wish to a good example seems to be here:
http://www.java2s.com/Code/CSharp/Event/GetAsyncKeyState.htm[^]

alternatively for a more in depth explaination/example here:
http://www.switchonthecode.com/tutorials/winforms-accessing-mouse-and-keyboard-state[^]
 
Share this answer
 
v2
Comments
NuttingCDEF 30-Apr-11 8:18am    
See possible alternative solution using Control.ModifierKeys.
Ed Nutting 30-Apr-11 8:24am    
Yes that seems to work, though it will only tell you if one of the modifier keys is pressed which causes and issue if whitehacker wants to capture other modifier keys simultaneously.
Sandeep Mewara 30-Apr-11 12:32pm    
My 5!
Nish Nishant 30-Apr-11 12:36pm    
Good answer, my vote of 5!
Not tried it personally, but see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.modifierkeys.aspx[^] for Control.ModifierKeys property that may well give you what you want.
 
Share this answer
 
Comments
Ed Nutting 30-Apr-11 8:25am    
My 5. See my other comment.
Sandeep Mewara 30-Apr-11 12:32pm    
My 5!
Nish Nishant 30-Apr-11 12:36pm    
Good answer, my vote of 5!

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