5

I've read quite some articles now about key press events but I can't figure out how to get them. I know that only the current control with keyboard focus gets the press events. But how can i ensure that my user control has it?

Tried this without luck:

public partial class Editor : UserControl

this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
...
//take focus on click
protected override void OnMouseDown(MouseEventArgs e)
{
    this.Focus();
    base.OnMouseDown(e);
}
...
protected override bool IsInputKey(Keys keyData)
{
   return true;
}

And also this:

//register global keyboard event handlers
private void Editor_ParentChanged(object sender, EventArgs e)
{
    if (TopLevelControl is Form)
    {
        (TopLevelControl as Form).KeyPreview = true;
        TopLevelControl.KeyDown += FCanvas_KeyDown;
        TopLevelControl.KeyUp += FCanvas_KeyUp;
        TopLevelControl.KeyPress += FCanvas_KeyPress;
    }
}

The latter gave me the key down and up events, but still no key press. Is there any other method i can use to just get every down/up/press events when my control inherits from UserControl?

Edit: As there was a comment linking another SO question: It's important that I also get the KeyPress event, since it sends the correct character on every keyboard no matter which language. This is important for text writing. If you only get the individual keys you have to process them into the correct character on your own. But maybe there is a convenience method to transform the pressed keys into a localized character?

thalm
  • 2,738
  • 2
  • 35
  • 49
  • Have you turned KeyPreview on for the form? – TaW Apr 01 '14 at 10:15
  • yes, see the second code example, line 6... – thalm Apr 01 '14 at 10:16
  • possible duplicate of [Best way to implement keyboard shortcuts in a Windows Forms application?](http://stackoverflow.com/questions/400113/best-way-to-implement-keyboard-shortcuts-in-a-windows-forms-application) – Sriram Sakthivel Apr 01 '14 at 10:22
  • no, the solution you linked is for key down/up events. what i need is the key press, because the key press event sends the correct character for every keyboard/language and not the distinct keys. this is important if you use it for text writing and such... – thalm Apr 01 '14 at 10:57
  • 3
    A user control was not meant for this, it is a container for other controls, one of those gets the focus. If you don't have any then you should not derive from UserControl, you should derive from Control instead. – Hans Passant Apr 01 '14 at 11:43
  • @thalm did the answer below work for you? I'm trying to do the same thing. – Scott Baker Sep 22 '14 at 17:34
  • @ScottSEA as far as i remember it wasn't working. otherwise i'd mark it as solution. we ended up passing thru what we need by hand. – thalm Dec 10 '14 at 22:38

2 Answers2

4
  • Set your parent form's (contains the usercontrol) KeyPreview property to true
  • Add a new KeyPress event to your parent form

Set the parent form keypress event to forward the event to your usercontrol:

private void parentForm_KeyPress(object sender, KeyPressEventArgs e)
        {
            // Forward the sender and arguments to your usercontrol's method
            this.yourUserControl.yourUserControl_KeyPress(sender, e);
        }

Replace the yourUserControl1_KeyPress method with your own method, which you want to run each time the user presses a button (the button is pressed down and then released).

You can also create a new KeyPress handler to your usercontrol, and forward the sender and KeyPressEventArgs objects there, as in this example.

W0lfw00ds
  • 2,018
  • 14
  • 23
0

Can you attach to form event.

Private Sub MyControl_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim f = Me.FindForm
    If Not f.KeyPreview Then Throw New Exception("Form requires has Keypreview enabled")
    AddHandler f.KeyUp, Sub(sender2 As Object, e2 As KeyEventArgs)

                        End Sub
End Sub