Key press event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi every one

I'm building an application that need to catch when a key is press

(this is codebar reader the codebar is capture from a text box, but I need
(depending of the message returned from de code bar) to capture if a key is
pressed, maybe de keypad, to change the result of returned message.

Thank you for your help
 
Hi Pedro,

You can detect if the character is a numeric, letter or control character etc in the KeyPress event, but you won't get told what keyboard key triggered the event. For that you need to use the KeyDown/KeyUp events.
 
Hi Morten

Can you help me with an example?

Morten Wennevik said:
Hi Pedro,

You can detect if the character is a numeric, letter or control character etc in the KeyPress event, but you won't get told what keyboard key triggered the event. For that you need to use the KeyDown/KeyUp events.
 
I'm not exactly sure what you are trying to do, but to capture a KeyPress event, you typicall do something like this:

// when loading the Windows Form
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);

....

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
if(!Char.IsDigit(e.KeyChar))
e.Handled = true;
}

The above code will create a TextBox that only allows numeric characters (0-9). To allow for control characters like backspace you would use something like this:

if(!Char.IsDigit(e.KeyChar) && !Char.IsControl)
e.Handled = true;

Characters that don't match the criteria will be marked as handled and will never reach the TextBox.

The KeyDown/KeyUp events work in similar ways but instead of dealing with Char they deal with Keys, like arrow keys (Keys.Left), or A (Keys.A) or function keys (Keys.F1).


Hi Morten

Can you help me with an example?
 
Hi Again Morten
This is a (Idon´t know the term in english) a employee time control

they pass their card in a codebar card reader, if they pass ther entrance
hour they need someome to authorise their entrance. I want thant the
operator press the "R" key (or other) in the keyboard and this riase the form
where he/she can authorise the entrance.

Thank you for your help.



Morten Wennevik said:
I'm not exactly sure what you are trying to do, but to capture a KeyPress event, you typicall do something like this:

// when loading the Windows Form
TextBox tb = new TextBox();
this.Controls.Add(tb);
tb.KeyPress += new KeyPressEventHandler(tb_KeyPress);

....

private void tb_KeyPress(object sender, KeyPressEventArgs e)
{
if(!Char.IsDigit(e.KeyChar))
e.Handled = true;
}

The above code will create a TextBox that only allows numeric characters (0-9). To allow for control characters like backspace you would use something like this:

if(!Char.IsDigit(e.KeyChar) && !Char.IsControl)
e.Handled = true;

Characters that don't match the criteria will be marked as handled and will never reach the TextBox.

The KeyDown/KeyUp events work in similar ways but instead of dealing with Char they deal with Keys, like arrow keys (Keys.Left), or A (Keys.A) or function keys (Keys.F1).
 
Well, in that case you can pretty much use whatever control for the key input, even the main Form itself (you need to set the Form's KeyPreview property to true if other controls may have focus (like various buttons etc). Just listen for Keys.R in the KeyDown event of your input control.

Hi Again Morten
This is a (Idon´t know the term in english) a employee time control

they pass their card in a codebar card reader, if they pass ther entrance
hour they need someome to authorise their entrance. I want thant the
operator press the "R" key (or other) in the keyboard and this riase the form
where he/she can authorise the entrance.

Thank you for your help.
 
Thank you for your help that's what I need

Morten Wennevik said:
Well, in that case you can pretty much use whatever control for the key input, even the main Form itself (you need to set the Form's KeyPreview property to true if other controls may have focus (like various buttons etc). Just listen for Keys.R in the KeyDown event of your input control.
 
Back
Top