tags:

views:

196

answers:

4

My ActiveX control contains various shapes which are drawn. CTRL-A is used in the control to select all the objects. Similarly CTRL-C to copy, CTRL-V to paste etc.

However, when I insert this control within a Windows form in a .Net application, it does not receive these keyboard events. I tried adding a PreviewKey event, and this does allow certain keystrokes to be sent e.g. TAB, but not these modified keys.

Does anybody know how to redirect modified keystrokes to a user control?

Thanks.

A: 

You need to trap the keys and override the ProcessCmdKey method.

class MyDataGrid : System.Windows.Forms.DataGrid

    {
       protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
       {
           ...........

       }
    }

http://support.microsoft.com/kb/320584

pierre
A: 

Hi there,

KeyPreview is just the wrong method. Try using KeyUp or KeyDown, like this:

        private void ControlKeyTestForm_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.A)
                 this.label1.Text = "Ctrl+A pressed";
        }

If you want the containing form to deal with shortcut keys remember to set the KeyPreview property on the form to true then set the KeyDown or KeyUp handlers in the form.

Justin Doyle
A: 

Use Control.ModifierKeys Property to check for Modifier keys.

For example, to check for shift key,

try if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift) { }

Full example here:

http://msdn.microsoft.com/en-us/library/aa984219%28VS.71%29.aspx

+1  A: 

It's possible that the ActiveX control doesn't have focus and is therefore not receiving the key events. You may want to handle the key events at the form level and then call the appropriate methods on your ActiveX control. If you set the KeyPreview property of your form to true your form will receive the key events for all controls on the form. That way, your shortcuts should work no matter what control currently has focus. Here is a quick example you can play with to test this out. Create a new form with several different controls on it and modify the code like so:

public Form1()
{
    InitializeComponent();

    KeyPreview = true;  // indicates that key events for controls on the form
                        // should be registered with the form

    KeyUp += new KeyEventHandler(Form1_KeyUp);
}

void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control)
    {
        switch (e.KeyCode)
        {
            case Keys.A:
                MessageBox.Show("Ctrl + A was pressed!");
                // activeXControl.SelectAll();
                break;
            case Keys.C:
                MessageBox.Show("Ctrl + C was pressed!");
                // activeXControl.Copy();
                break;
            case Keys.V:
                MessageBox.Show("Ctrl + V was pressed!");
                // activeXControl.Paste();
                break;
        }
    }
}

No matter what control has focus when you enter the key combinations, your form's Form1_KeyUp method will be called to handle it.

jwnace