views:

321

answers:

4

As a workaround for a problem, I think I have to handle KeyDown events to get the printable character the user actually typed.

KeyDown supplies me with a KeyEventArgs object with the properities KeyCode, KeyData, KeyValue, Modifiers, Alt, Shift, Control.

My first attempt was just to consider the KeyCode to be the ascii code, but KeyCode on my keyboard is 46, a period ("."), so I end up printing a period when the user types the delete key. So, I know my logic is inadequate.

(For those who are curious, the problem is that I have my own combobox in a DataGridView's control collection and somehow SOME characters I type don't produce the KeyPress and TextChanged ComboBox events. These letters include Q, $, %....

This code will reproduce the problem. Generate a Form App and replace the ctor with this code. Run it, and try typing the letter Q into the two comboxes.

public partial class Form1 : Form
{
    ComboBox cmbInGrid;
    ComboBox cmbNotInGrid;
    DataGridView grid;

    public Form1()
    {
        InitializeComponent();

        grid = new DataGridView();

        cmbInGrid = new ComboBox();
        cmbNotInGrid = new ComboBox();

        cmbInGrid.Items.Add("a");
        cmbInGrid.Items.Add("b");
        cmbNotInGrid.Items.Add("c");
        cmbNotInGrid.Items.Add("d");

        this.Controls.Add(cmbNotInGrid);
        this.Controls.Add(grid);
        grid.Location = new Point(0, 100);
        this.grid.Controls.Add(cmbInGrid);
    }
A: 

Have a look at System.Text.Encoding.ASCII and System.Text.Encoding.Default

gnud
A: 

Try:

KeysConverter converter = new KeysConverter();
string key = converter.ConvertTo(e.KeyCode, typeof(string));

But is very strange the behavior you are describing. You should get the KeyPress in those cases... Try to do a simple example (just a form with KeyPreview = true and KeyPress event handled) and see what you get. Also check in the language bar when the form is displayed, maybe there is an input method different than what you expect.

Aleris
A simple combobox in a simple app gives me the KeyPress events. I suspect that the DataGridView is intercepting the messages. It's not aware of these alien children I put in its Control collection.
Corey Trager
Sigh, KeyConvert requires VS 2008, 3.0/3.5 framework...
Corey Trager
Similar behavior: http://bytes.com/forum/thread548399.html
Corey Trager
A: 

Just as an idea to throw out there, if it looks like your DataGridView is intercepting keyboard events before they can reach your child control, can you provide your own handlers for the keyboard events you are interested in directly on the DataGridView, and in the handler method(s), (1) suppress the DataGridView's normal handling of the event, and/or (2) manually pass the event along to your child control?

Jon Schneider
I'm in still debugging this. The secret little child edit control of the combobox is ketting WM_KEYDOWN, WM_CHAR, and WM_KEYUP messages. I don't understand....
Corey Trager
See my post above. I don't think you need to go this route at all. Besides, overriding those messages can get you more trouble than you ask for since the underlying control might expect certain input and respond to that, i.e. so that your dropdown doesn't automatically close when it's supposed to.
Pedery
+1  A: 

Many controls override the default key input events. For instance, a Panel won't respond to them by default at all. As for the case of simple controls, you could try:

protected override bool IsInputKey(Keys keyData) {
    // This snippet informs .Net that arrow keys should be processed in the panel (which is strangely not standard).

    switch (keyData & Keys.KeyCode) {
     case Keys.Left:
      return true;
     case Keys.Right:
      return true;
     case Keys.Up:
      return true;
     case Keys.Down:
      return true;
    }
    return base.IsInputKey(keyData);

}

The IsInputKey function tells your program what keys to receive events from. There is a chance you'll get weird behaviour if you override keys that clearly have special functions, but experiment a little and see for yourself what works and what doesn't.

Now, for more advanced controls like a DataGridView or ComboBox, keyhandling can be even more complicated. The following resource should give you a few pointers about how to go about your problem:

http://www.dotnet247.com/247reference/msgs/29/148332.aspx

Or this resource might perhaps solve your problem:

http://dotnetperls.com/previewkeydown

Pedery
The logic did not work for me.
Corey Trager
Pedery - see the code I just posted in the question itself.
Corey Trager
Your *edit* code just adds stuff to a form. From your rep score I assume you're fairly aquainted with Windows Forms. What you're trying to do *can* be a bit tricky. It all depends on the control and what you want to achieve. Take a look at the two links I posted. They describe some of the functions you want to explore. Remember to override i.e. the ComboBox class in a separate file and put the *override* code there. Sorry if I'm describing something that is obvious, but it's a bit hard to guess your exact background.
Pedery