Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

traping all combinations of keypress in overriding ProcessCmdKey

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peted - 06 Apr 2008 17:40 GMT
Hello,

i am lookinf for the best way to trap any alphanumeric keypress in all
multi key combminations and execute some code

For example , i have a form visible using the form.showdialog method,
then the user can press keys A, D, E, F, T, and C  to peform a
function.
What i want is that if a user presses A, or a, or SHIFT A or CNTRL A
that it executes the same code, BUT NOT IF ALT A is pressed. The same
applies for the other letters.

I am overriding the processcmdkey function using the code bellow, and
it works, but not for ALL the multi key combo's i present above.

Can anyone help me on how to trap all the key combos i want listed
above

thanks for any help

Peted

#region ProcessCmdKey
       protected override bool ProcessCmdKey(ref Message msg, Keys
keyData)
       {
           const int WM_KEYDOWN = 0x100;
           const int WM_SYSKEYDOWN = 0x104;

           if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
           {
               switch (keyData)
               {
                   case Keys.Tab:
                       //Do something
                       return true;
                       break;
                   case Keys.F3:
                       //Do something
                       return true;
                   break;
                   
                   case Keys.A:
                   //Do something
                       return true;

                 
                   case Keys.D:
                       //Do something
                       return true;
                 
                   case Keys.E:
                       //Do something
                       return true;
                 
                   case Keys.F:
                       //Do something
                       return true;
                 
                   case Keys.T:
                       //Do something
                       return true;
                 

                   case Keys.C:
                       //Do something
                       return true;

               }
           }
           return base.ProcessCmdKey(ref msg, keyData);
       }
Peter Duniho - 06 Apr 2008 19:24 GMT
> i am lookinf for the best way to trap any alphanumeric keypress in all
> multi key combminations and execute some code
[quoted text clipped - 8 lines]
> I am overriding the processcmdkey function using the code bellow, and
> it works, but not for ALL the multi key combo's i present above.

Why not?  What happens?  What did you expect to happen instead?

Pete
Peted - 07 Apr 2008 02:39 GMT
>> I am overriding the processcmdkey function using the code bellow, and
>> it works, but not for ALL the multi key combo's i present above.
>
>Why not?  What happens?  What did you expect to happen instead?

Pressing  A, D, E, F, T, and C  keys on thier own works fine

Capslock and  A, D, E, F, T, and C   works fine

Shift and  A, D, E, F, T, and C  does not work

Control and  A, D, E, F, T, and C does not work

Alt and  A, D, E, F, T, and C does not work
(I have now decided i do want the alt key combo to also work)

So any idea how i can get mod the code to make it pick up all those
modifier and key press combo in addition to just pressing the relevant
letter ?

I want all those combos to execute the same code as the single
keypress

Any advice appreciated

thanks

Peted
Steve Gerrard - 07 Apr 2008 03:27 GMT
> Control and  A, D, E, F, T, and C does not work

To capture Ctrl A separately, you would need
               switch (keyData)
               {
                   case (Keys.A | Keys.Control):

Keys combines the keys being pressed.
Peter Duniho - 07 Apr 2008 03:57 GMT
> [...]
> So any idea how i can get mod the code to make it pick up all those
> modifier and key press combo in addition to just pressing the relevant
> letter ?

Instead of "switch (keyData)" use "switch (keyData & Keys.KeyCode)".  That  
will filter out the modifier keys, leaving only the actual key pressed.

If you decide later that you want some modifier keys but not others, there  
are a variety of ways you can check for that, but they all involve similar  
techniques.  That is, using the various mask constants in the Keys  
enumeration to check specifically for modifiers.

Pete
Peted - 07 Apr 2008 04:38 GMT
Great, thanks heaps dude

Peted

>> [...]
>> So any idea how i can get mod the code to make it pick up all those
[quoted text clipped - 10 lines]
>
>Pete

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.