471,594 Members | 1,955 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Capturing Carriage Return

Hello all,

To help alleviate human error, I'm developing an application that uses a
barcode reader to fill in a value in a TextBox control. The barcode
reader is connected to the PC through the PS2 keyboard jack, so to
Windows, it's essentially just a keyboard. After the reader has
finished reading the barcode, it sends out Ctrl + M (which I *think* is
a carriage return).

To capture the Ctrl + M, I configured an event handler for the
PreviewKeyDown event. In the event handler, I have tried the following:

private void MyTextBox_PreviewKeyDown(object sender,
PreviewKeyDownEventArgs e)
{
// First way I tried
if (e.Control && e.KeyCode == Keys.M)
{
ShowNewDialog();
}

// Second way I tried
if ((int) e.KeyData == (int) (Keys.M | Keys.Control))
{
ShowNewDialog();
}
}

Essentially, both of them do the same thing, but the first one is less
confusing to somebody that reads my code later.

At any rate, the code works --kind of. The problem is that after
ShowNewDialog() returns, the letter "m" shows up in the MyTextBox
control. If I comment out the call to ShowNewDialog(), the letter "m"
never shows up.

Short of an ugly "fix", such as removing the letter "m" after
ShowNewDialog() returns, I'm not sure what else to do. I was hoping
there was a better way of capturing the carriage return that I haven't
thought of.

Any suggestions?

Thank you in advance,

--
Sean
Nov 3 '06 #1
2 4116
Hi,

Any reason for using PreviewKeyDown instead of KeyDown?

If you are using KeyDown you can use the KeyEventArgs.Handled = true to
suppress keys

On Fri, 03 Nov 2006 15:45:52 +0100, senfo
<en**********@yahoo.comI-WANT-NO-SPAMwrote:
Hello all,

To help alleviate human error, I'm developing an application that usesa
barcode reader to fill in a value in a TextBox control. The barcode
reader is connected to the PC through the PS2 keyboard jack, so to
Windows, it's essentially just a keyboard. After the reader has
finished reading the barcode, it sends out Ctrl + M (which I *think* is
a carriage return).

To capture the Ctrl + M, I configured an event handler for the
PreviewKeyDown event. In the event handler, I have tried the following:

private void MyTextBox_PreviewKeyDown(object sender,
PreviewKeyDownEventArgs e)
{
// First way I tried
if (e.Control && e.KeyCode == Keys.M)
{
ShowNewDialog();
}

// Second way I tried
if ((int) e.KeyData == (int) (Keys.M | Keys.Control))
{
ShowNewDialog();
}
}

Essentially, both of them do the same thing, but the first one is less
confusing to somebody that reads my code later.

At any rate, the code works --kind of. The problem is that after
ShowNewDialog() returns, the letter "m" shows up in the MyTextBox
control. If I comment out the call to ShowNewDialog(), the letter "m"
never shows up.

Short of an ugly "fix", such as removing the letter "m" after
ShowNewDialog() returns, I'm not sure what else to do. I was hoping
there was a better way of capturing the carriage return that I haven't
thought of.

Any suggestions?

Thank you in advance,


--
Happy Coding!
Morten Wennevik [C# MVP]
Nov 3 '06 #2
Morten Wennevik wrote:
Any reason for using PreviewKeyDown instead of KeyDown?

If you are using KeyDown you can use the KeyEventArgs.Handled = true to
suppress keys
Hi Morten,

That's a good question and I don't have an answer for you. Using the
KeyDown event fixed everything.

Thank you very much!

--
Sean
Nov 3 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Canes_Rock | last post: by
4 posts views Thread by Josh | last post: by
2 posts views Thread by Andrew Chanter | last post: by
2 posts views Thread by Matt Mercer | last post: by
3 posts views Thread by Dinsdale | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.