<p>I'm writing a C# POS (point of sale) system that takes input from a keyboard wedge magcard reader. This means that any data it reads off of a mag stripe is entered as if it were typed on the keyboard very quickly. Currently I'm handling this by attaching to the KeyPress event and looking for a series of very fast key presses that contain the card swipe sentinel characters.</p>

<p>Is there a better way to deal with this sort of input? </p>

<p>Edit: The device does simply present the data as keystrokes and doesn't interface through some other driver. Also We use a wide range of these types of devices so ideally a method should work independent of the specific model of wedge being used. However if there is no other option I'll have to make do.</p>

[{'answer_id': 42448, 'author': 'jttraino', 'author_id': 3203, 'author_profile': 'https://Stackoverflow.com/users/3203', 'pm_score': 4, 'selected': True, 'text': "<p>One thing you can do is that you should be able to configure your wedge reader so that it presents one or many escape characters before or after the string.  You would use these escape characters to know that you are about to have (or just had) a magcard input.</p>\n\n<p>This same technique is used by barcode reader devices so you application knows to get focus or handle the data input from the device.</p>\n\n<p>The negative to this approach is that you have to properly configure your external devices.  This can be a deployment issue.</p>\n\n<p>This assumes that your devices simply present the data as keystrokes and don't interface through some other driver.</p>\n"}, {'answer_id': 42463, 'author': 'Pascal', 'author_id': 1311, 'author_profile': 'https://Stackoverflow.com/users/1311', 'pm_score': 0, 'selected': False, 'text': '<p>I second <a href="https://stackoverflow.com/questions/42437/best-way-to-handle-input-from-a-keyboard-wedge#42448">@jttraino\'s idea</a>. </p>\n\n<p>It is the way to go for bar scan/code readers and other such devices that are plug and play (PnP). I have used the same technique to configure a couple of 1D and 2D bar code scanners in my previous assignment.</p>\n'}, {'answer_id': 42469, 'author': 'Re0sless', 'author_id': 2098, 'author_profile': 'https://Stackoverflow.com/users/2098', 'pm_score': 2, 'selected': False, 'text': '<p>I think you are handling it in an acceptable way, just be careful of how fast the card sends the data, we have wireless bar code scanner\'s, and now and again they throw the key strokes at the keyboard to fast for the application to handle.</p>\n\n<p>also if you are distributing your software to other territories, then key stokes may be different, for example in Spain (I think, but may be France) the top line of the keyboard is !"£$%^&amp;<em>() opposed to the USA/UK 1234567890, and if your card reader is set to usa/uk then it will send !"£$%^&amp;</em>() in place of 1234567890, as the wedge just emulates that key been pressed and if windows interprets it different then its your problem.</p>\n'}, {'answer_id': 42850, 'author': 'robsoft', 'author_id': 3897, 'author_profile': 'https://Stackoverflow.com/users/3897', 'pm_score': 1, 'selected': False, 'text': '<p>Another vote for <a href="https://stackoverflow.com/questions/42437/best-way-to-handle-input-from-a-keyboard-wedge#42469">jttraino\'s idea</a>. I do much the same with card-readers and cheque-readers in point-of-sale systems where we need to support keyboard wedge as well as USB and RS232. </p>\n\n<p>Basically, choose a short sequence of characters unlikely to come from the keyboard, and program your message handling loop to see these characters arriving. If you get a completed stream of characters that match your pattern, you can decode the rest of your input until you hit your designated \'end\' sequence, or until you decide the incoming sequence is in error. Select a string that is either difficult, or impossible, to enter from the regular keyboard into your app given things like edit masks and the behaviour of your various screens. </p>\n\n<p>A good starting point is something like tilda-pling (~!) as those characters are not likely to appear in anyone\'s personal details and not likely to ever need to appear together in the text of a note, etc. :-)</p>\n\n<p>The downside, exactly as jttraino said, is that you will probably have to configure/program each reader device itself. Some manufacturers make this fairly easy to do - whose kit are you using? Magtek? Welch Allyn?</p>\n'}, {'answer_id': 589283, 'author': 'Nicholas Piasecki', 'author_id': 32187, 'author_profile': 'https://Stackoverflow.com/users/32187', 'pm_score': 2, 'selected': False, 'text': '<p>You can also use the Raw Input API if you know the Hardware IDs of the devices ahead of time. <a href="http://nicholas.piasecki.name/blog/2009/02/distinguishing-barcode-scanners-from-the-keyboard-in-winforms/" rel="nofollow noreferrer">I blogged about this recently.</a> It may be insane but it satisifed my requirement: the primary goal in my case was to be able to receive input even when the application lost focus because someone accidentally bumped into something while rummaging around to scan items on a pallet. The secondary goal is that I couldn\'t add any sentinel characters because that would have broken existing third-party applications being used with the scan guns.</p>\n\n<p>I\'ve done the sentinel character method before, however, both via a <code>KeyPress</code> attach or a low-level keyboard hook via <code>SetWindowsHookEx()</code> or via <code>KeyPreview</code> on your application\'s main form. If it meets your requirements, it\'s definitely much simpler and easier to use that method and to that end I second the recommendations already given.</p>\n'}]
['https://Stackoverflow.com/questions/42437', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2191/']