Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: Detect input from barcode scanner


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Feb 8, 2018 10:17 AM Last Post By: Remy Lebeau (Te... Threads: [ Previous | Next ]
Mikael Lenfors

Posts: 99
Registered: 3/6/01
Detect input from barcode scanner  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2018 9:36 AM
Hello!

We have an application with houndreds of forms. We now want to attach a barcode scanner for input.
The scanner has to be in a mode where it emulates a keyboard for it to work with other applications,
The input from the scanner is not supposed to go to any control currently in focus!
As we have houndreds of controls we can't have input control on all of them.

So I want to be able to detect if the keys streaming into the application originates from the real keyboard or from the scanner, and prevent characters from the scanner to flow directly into the controls. I want this input to be buffered and handled separately in the application..

I Googled and found that getrawinputdata could be a solution.

Is this the way to go or is there a simpler way? Does anyone have sample code for a solution?

Best regards, Mikael
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Detect input from barcode scanner  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Feb 8, 2018 10:17 AM   in response to: Mikael Lenfors in response to: Mikael Lenfors
Mikael Lenfors wrote:

We have an application with houndreds of forms. We now want to attach
a barcode scanner for input. The scanner has to be in a mode where
it emulates a keyboard for it to work with other applications

And how exactly does it do that? What does its documentation say
exactly?

So I want to be able to detect if the keys streaming into the
application originates from the real keyboard or from the scanner

The only way to differentiate whice device is sending keyboard input is
to use the Raw Input API:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536.aspx

The WM_INPUT message that is delivered on every keystroke will give you
a handle to the device that is generating the message. You have to
register interest in the device via RegisterRawInpuDevices() before you
can receive messages from it.

https://msdn.microsoft.com/en-us/library/windows/desktop/ms645600.aspx

and prevent characters from the scanner to flow directly into the
controls.

Unfortunately, handling WM_INPUT messages alone does not let you block
keyboard input from reaching its destination. For that, there are two
different approaches you can take:

1. augment your app's message loop (such as by using the
TApplication(Events)::OnMessage event) to look for standard keyboard
messages (WM_KEYDOWN, WM_KEYUP). When your app receive a WM_INPUT
message from the scanner, ignore any subsequent keyboard messages for
the same keystroke, do not dispatch the messages to your window for
processing.

2. use a low level keyboard hook from SetWindowsHookEx() and have it
look for the standard keyboard messages. When your app receives a
WM_INPUT message from the scanner, tell your hook to discard any
subsequent keyboard messages for the same keystroke, do not pass the
messages along the hook chain for further processing.

On the other hand, some barcode scanners can be configured to send a
unique keystroke at the beginning of scanning, and another one at the
end of scanning. If that is the case with your scanner (check the
documentation), then you could avoid using the RAw Input API or a
keyboard hook, and just handle standard keyboard messages normally.
Have your OnMessage handler look for the starting keystroke, and when
detected then set a flag. For every key message received, if the flag
is set, capture the key and don't dispatch the message, otherwise
dispatch the message normally. When the ending keystroke is detected
and the flag is set, clear the flag and process whatever you have
captured.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02