OK, basically I want to test if the shift key is pressed.
The method i've tried (unsuccessfully) is

private bool shiftPressed = false;
        private void CheckKeys(object sender, KeyPressEventArgs e)
        {
            //If enter is pressed and shift isn't
            //This allows for line breaks in a message by pressing shift & enter at the same time
            if (e.KeyChar == (char)13 && !shiftPressed)
            {
                //remove the line break caused by the enter key
                rtbInput.Text = rtbInput.Text.Substring(0, rtbInput.Text.Length - 1);
                Send();
            }

            //Else if shift is pressed
            else if (e.KeyChar == (char)15)
            {
                shiftPressed = true;
            }

            //Else if shift is depressed
            else if (e.KeyChar == (char)14)
            {
                shiftPressed = false;
            }
        }

This is run every time a key is pressed in the rich text box. Unfortunately, shift doesnt appear to trigger the KeyPressEventHandler

Can anybody show me an alternate method that i can use to test for the shift key?

Thanks,
Josh.

Recommended Answers

All 3 Replies

You need to use the KeyUp and KeyDown events. The shift key doesnt fire the KeyPress event.
The KeyUp and KeyDown events have an e.KeyData property which will hold a value you can check against the Keys enumeration:

if(e.KeyData== Keys.ShiftKey)

Thanks Ryshad!

Just a note to anyone else who sees this in the future, the KeyData of KeyDown for shift is equal to:
Keys.ShiftKey | Keys.Shift
whereas for KeyUp it's equal to:
Keys.ShiftKey

^^The above gave me a 10 minute debigging headache - Not really sure why MS has done this, but all the same - works now.

No problem :) Remember to mark the thread as solved

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.