+3  A: 

You need to check to see if e.Shift is true, in addition to just checking the KeyCode property.

Reed Copsey
okay. Here's what I have. It reads [ --> OemOpenBracket, ] --> Oem6, ~ --> OemTilde, ^ --> D6. So D6 is not making sense.
Sandy
Characters and numbers get converted to upper character so i shows as I which should not be the case
Sandy
BTW e.Shift is never true - even when I use shift key
Sandy
+1  A: 

It would be easier to do it this way:

private readonly string VALID_KEYS = "[]~^ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

private void txtBox_KeyPress(object sender, KeyPressEventArgs e) {
    if (VALID_KEYS.IndexOf(char.ToUpper(e.KeyChar)) != -1 || e.KeyChar == (char)8)
         e.Handled = false;
    else
         e.Handled = true;
}

tommieb75
Actually I want the combination of letters, numbers and special characters (as listed above) entered by the user. I am checking and I need to append these to my variable. So I need to evaluate what was wntered by the use r which will be used for further evaluations
Sandy
A: 

Hi,

This might be of some help...

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.Shift | Keys.D6:
                    //Your code
                    break;
            }
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }

Thanks, Ram

Ram