Sandy
Sandy

Reputation: 21

How to get characters read from the keyboard?

I am trying to get the characters read from the keyboard into some variable for further manipulation. I have the following list that I wish to recognize if entered by a user.

List of keyboard entries:

Part of the code:

 void HookManager_KeyUp(object sender, KeyEventArgs e)
 {
       string test = e.KeyCode.ToString();

Here numbers, letters and square brackets work but the ^ which require shift key is read incorrectly. For eg. it reads ^ which is on key 6 as a string value of 6 and not ^ as it should be. Here are the other readings

So D6 is not making senseAny help would be appreciated.

Thanks

AA

Upvotes: 2

Views: 1751

Answers (3)

Ram
Ram

Reputation: 1117

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

Upvotes: 0

t0mm13b
t0mm13b

Reputation: 34592

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;
}

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564413

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

Upvotes: 3

Related Questions