Key processing in .NET

E

Edward Diener

Is there any good article about processing keys in .net ? I see
things like ProcessDialogKey, IsInputKey, IsInputChar, and mentions of
preprocessing certain keys, and the whole area is confusing to me without a
good general explanation.

In Win32 I can override WM_GETDLGCODE in my derived control to handle
certain dialog keys directly in my control ( arrows, tabs ). The
documentation on processing keys which I am trying to understand does not
make it plain how I can use the .net functionality to do what I want to do
rather. I realize that I can continue to handle WM_GETDLGCODE in my
control's Window procedure but would prefer to use the .net functionality
instead if it is a replacement for the lower level WM_GETDLGCODE
 
M

Morten Wennevik

Hi Edward,

Just to explain a few things, although I'm sure there are better explanations on the net. Keys and Chars are different. .Net Framework distinguishes between keyboard keys and actual characters.
For instance the Decimal key can be either , or . depending on the current location.
The Decimal Character in Norway is , and in USA . but the Decimal Key is the actual key that resides on the numpad representing the decimal point. Keys.A is the keyboard key A and can be both 'A' and 'a'.

When pressing a key on the keyboard three things happen in turn.

KeyDown event is sent when you press the key down. ( = Keys.Decimal )
KeyPress event is the translated character from that key ( = '.' )
KeyUp event is sent when you release the key

As a sample this code checks a textbox and only allows numbers, a decimal point, backspace (delete is a key not a character) or a minus sign, and adds a 0 in case you just hit the decimal point before any numbers are written.
Note that the character is written to the textbox after the KeyPress event and you can cancel this event (e.Handled = true) to prevent characters being written.

comma = Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator[0];

private void tb_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if(!(Char.IsDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == comma || e.KeyChar == '-'))
{
e.Handled = true;
return;
}

TextBox t = (TextBox)sender;

if(e.KeyChar == comma)
{
if(t.Text.IndexOf(comma.ToString()) != -1)
{
e.Handled = true;
return;
}
}
}

private void tb_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
TextBox t = (TextBox)sender;

if(e.KeyCode == Keys.Decimal || e.KeyCode == Keys.Oemcomma)
{
if(t.TextLength == 1)
{
t.Text = "0" + t.Text;
t.SelectionStart = t.TextLength;
}
else if(t.TextLength == 2 && t.Text[0] == '-')
{
t.Text = t.Text.Insert(1, "0");
t.SelectionStart = t.TextLength;
}
}
}

So, you would use ProcessDialogChar to get the actual character like a or A and ProcessDialogKey to get things like Arrow Keys or just the A key.
 
E

Edward Diener

Morten said:
Hi Edward,

Just to explain a few things, although I'm sure there are better
explanations on the net. Keys and Chars are different. .Net
Framework distinguishes between keyboard keys and actual characters.
For instance the Decimal key can be either , or . depending on the
current location. snipped...

Thanks for the information. I do understand the general process and the
basic key events which occur when a key is pressed and it is fielded in a
control. I was really looking for information about preprocessing certain
keys, directing .net to allow my control to handle certain keys, etc. In my
original post I mentioned WM_GETDLGCODE, which is the Win32 way of telling
Windows to allow a control to handle certain key combinations. I think .net
has an equivalent way which I can use so I do not have to handle
WM_GETDLGCODE, but I do not know what it is with all the different
preprocessing and processing functions available besides the keydown,
keypress, and keyup.
So, you would use ProcessDialogChar to get the actual character like
a or A and ProcessDialogKey to get things like Arrow Keys or just the
A key.

How does ProcessDialogChar and ProcessDialogKey relate to keydown, keypress,
and keyup ? Are they called before, after, or during these other calls ?
There is also a PreProcessKey call. How does that relate to these other
calls ?

Do you see what I am getting at ? I understand the basic keydown, keypress,
and keyup, havinbg used them before. But these other functions, which seem
as if they might be used within a control to handle keys a control would not
ordinarily process, confuse me as to their functionality. Needless to say
the MSDN documentation on these other Process... and PreProcess... calls
does not explain how they work.
 
M

Morten Wennevik

Hm ok, I have to admit I haven't used those methods.

They certainly trap messages before the events are sent and would therefore give you more control, but I couldn't tell you cases where you would need to use them.

Maybe this link can shed some light.
 
E

Edward Diener

Morten Wennevik said:
Hm ok, I have to admit I haven't used those methods.

They certainly trap messages before the events are sent and would
therefore give you more control, but I couldn't tell you cases where you
would need to use them.
Maybe this link can shed some light.

Do not see a link.

In the bookstore today, looking for more info on key processing in .NET, I
ran across the information that one can use IsInputKey to specify which keys
should be allowed for a control. Evidently IsInputKey is MS's .Net answer to
WM_GETDLGCODE among other things. Figuring this out from the sparse docs on
IsInputKey is just about impossible. It would really have been nice if MS
had put out a document deetailing the .Net equivalents to Win32 messages. I
know their is a document specifying the .Net equivalent to Win32 functions
( at
http://msdn.microsoft.com/vstudio/default.aspx?pull=/library/en-us/dndotnet/html/win32map.asp )
but an equivalent document dealing with Windows messages would have really
been helpful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top