By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,827 Members | 2,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,827 IT Pros & Developers. It's quick & easy.

Capturing Carriage Return

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.