Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1115
  • Last Modified:

Capturing the correct keys in a User Control KeyDown event

Hi,

I have a User Control containing a text box, and I'd like to capture a Ctrl-X key press on that text box. However, whatever I try with regards to capturing the keys doesn't seem to work.

My code currently looks like this:

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
      if ((e.KeyData == Keys.X) && (e.Modifiers == Keys.Control))
            Console.Write("Ctrl-X pressed");
}

I know the event is being fired correctly, but it's not successfully matching both the X key and the Ctrl key when pressed together.

I've tried other combinations but nothing seems to work - can anyone help please

Thanks
Gerry
0
gamesmeister
Asked:
gamesmeister
2 Solutions
 
Thunder_screamCommented:
First set "key preview" to true for the form you are working on.

then try this

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (((e.KeyData & Keys.X) == Keys.X) && (e.Control))
            {
                Console.Write("Ctrl-X pressed");
            }
        }  
0
 
Thunder_screamCommented:
actually the "key preiview" seems not to be needed.

hope this solves your problem.
0
 
gamesmeisterAuthor Commented:
No, unfortunately that didn't work either.

However, I've just noticed something really bizarre. When I press Ctrl-Z, the above conditional is regarded as true: i.e., "Ctrl-X pressed" is written to the console. The same thing happens if I press Ctrl-Y

Haven't a clue what's going on here - my keyboard is mapped normally

Btw, your code works fine if I put it in a KeyDown event in a textbox directly on a Form. It's only on my user control that it's not working. Any ideas why that might be?

Thanks for your help
Gerry
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Thunder_screamCommented:
gamesmeister,

could you perhaps post the rest of the code?
0
 
Bob LearnedCommented:
Is Ctrl-X being trapped by something else?  A menu on a form, mayhaps?

Bob
0
 
patspamCommented:
Most like they key you are trying to capture is being captured by something else. Confirm this easily by trying to trap another key that isn't already being captured, such as Ctrl-L.

To get around the problem you'll probably have to turn Key Preview off (maybe on more than one control).
0
 
gamesmeisterAuthor Commented:
Doh...schoolboy error - I thought I'd cleared all the Ctrl-X events from the menu but missed one

Thanks for the help everyone
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now