how to specify a specific KeyPress event?

This is a discussion on how to specify a specific KeyPress event? within the C# Programming forums, part of the General Programming Boards category; Code: private void Form1_KeyPress(object sender, KeyPressEventArgs e) { } During the KeyPress event, it takes any key pressed, but how ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    33

    how to specify a specific KeyPress event?

    Code:
    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
            {
            }
    During the KeyPress event, it takes any key pressed, but how would i make it to ONLY accept the "w" key for example?
    I tried using a case statement...
    Code:
                switch (KeyRestrictionBehavior.AllowOnly.Equals("w"))
                {
                    case "w" :
                }
    but i get an error of "Cannot implicitly convert type 'string' to 'bool'".
    Am i doing this correctly? or is there a different way? And how would i correct the problem?

  2. #2
    Elk
    Elk is offline
    Registered User
    Join Date
    Sep 2006
    Posts
    7
    Are you looking to do this as a global or local hotkey?

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    Quote Originally Posted by Elk View Post
    Are you looking to do this as a global or local hotkey?
    Globally

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    EventArgs has a member which tells you what the character is. Compare it with your desired character and act accordingly if it is equivalent.

    Look up the members of EventArgs in your help file. There is a sample there that demonstrates exactly what you want.

    If you wish to do this globally note that your form only receives the keypress event if it happened while the focus was on the form and not on any of it's controls. If a control had the focus the control will receive the key event. You can set KeyPreview to True in the Designer view of your form to ensure that your form receives the keypress events before its controls do.

    This is almost akin to the MFC way of overriding WndProc to receive messages prior to them being forwarded onto the window's children (aka controls and other objects). The sad part is I've not found a way to pre-intercept mouse messages.
    Last edited by VirtualAce; 11-20-2007 at 06:10 PM.

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    33
    hmm, alright, i got it to work, thx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. Event driven thread programming
    By jaxen in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2006, 07:46 AM
  4. specific windows event
    By GuardianDevil in forum Windows Programming
    Replies: 2
    Last Post: 04-09-2004, 05:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21