"It works fine with windows notepad" means that you are using so called "
keyboard wedge":
http://en.wikipedia.org/wiki/Barcode_scanner#Keyboard_wedges[
^].
As bowlturner correctly pointed out, you need to keep focus on some control of your application. However, this is not so reliable way, because any other application can always grab focus if the user activates its main window. Probably, the purpose of keyboard wedge design was meant to be simplicity for development, but in fact, is just creates problems like that. An, if you really want to make a reliable solution, this is not simple at all: you will need to set a
global Windows hook on keyboard events and capture the scanner events. Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[
^].
As this is native Windows API, you will need to export some functions and types to CLI. You can do it using P/Invoke:
http://en.wikipedia.org/wiki/P/Invoke[
^],
http://msdn.microsoft.com/library/en-us/vcmxspec/html/vcmg_PlatformInvocationServices.asp[
^].
However, this might be not the best way. Here is why: first of all, you will need a
global, not a local hook. According to Microsoft, you cannot set a global hook in a CLI (managed) application; instead, you will need to create a native DLL for this purpose. Your application will need to load that DLL and organize some communication with the hook. So, C# won't be enough anyway. I don't know what native-platform language would you prefer, but I would advise using C++/CLI, because you can reach all goals at once: you can create a native DLL to set the hook, you can use the same project for managed (CLI) code if you create a
mixed-mode (managed+unmanaged) project. Such project will make you a DLL which can contain unmanaged code, but, from the point of view of .NET, serve as main executable module of a regular .NET assembly which you can reference in any .NET assembly, for example, the one written in C#.
C++/CLI is standardized under ECMA-372. Please see:
http://en.wikipedia.org/wiki/C%2B%2B/CLI[
^],
http://www.ecma-international.org/publications/standards/Ecma-372.htm[
^],
http://www.gotw.ca/publications/C++CLIRationale.pdf[
^],
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[
^].
See also:
http://msdn.microsoft.com/en-us/magazine/cc188966.aspx[
^].
And see also my past answers on related topics:
How the Keyboard Hook works and how it is being implemented[
^],
How to set a window hook in other application?[
^],
Creating global shortcuts.[
^].
—SA