1

How to detect Enter key press on Delphi form without interfering with any user interface controls on the form.

I am trying to read the output of a barcode scanner in a Delphi form. The scanner generates keypress events for each digit of the barcode as if a numeric key on the keyboard had been pressed and then an Enter keypress to indicate the barcode has been read.

The problem is this - I must differentiate between scanner and keyboard without affecting user interface.

I build up a buffer containing the barcode every time a numeric digit keypress is detected and when the enter key press detected I test to see if buffer is the length of the barcode- if so I assume a barcode was scanned and eat the enter key press. If the enter key was pressed and the barcode buffer is not the length of the expected barcode I assume enter was pressed on another user control. I cannot read the barcode into a textbox - a common method for reading such scanners - as the barcode is not relevant to the user and this requires the textbox having focus. The requirements are that a scan can occur anytime/anywhere while the form is open.

I have solved this by using the solution proposed by @Tlama at following link.

https://stackoverflow.com/a/10470110/960757

Community
  • 1
  • 1
user2425056
  • 327
  • 2
  • 4
  • 14
  • Add a memo control and give it the focus. Or get better barcode scanner software that has a sane interface. – David Heffernan Aug 01 '14 at 11:59
  • 1
    For instance [`this way`](http://stackoverflow.com/a/10470110/960757) if you replace `VK_TAB` with `VK_RETURN` and `YouWantToInterceptTab` will be the flag indicating that you are reading from that scanner. – TLama Aug 01 '14 at 12:09
  • @Lama :thanks this works as I it gives the behaviour I was trying to get. Somehow I cannot accept this as the solution - no option to accept is being shown. – user2425056 Aug 01 '14 at 12:23
  • @TLama : What do we do? Close as a duplicate? – whosrdaddy Aug 01 '14 at 12:32
  • @whosrdaddy, the linked question is so specific that I wouldn't call it a duplicate. If I were OP, I would wait for a possible alternative way to handle the overall problem. Or just delete the question if that linked answer suffice. – TLama Aug 01 '14 at 13:46
  • The easiest solution to achieve what OP wanted is to set TabStop of that button to False. This way the button won't be focused on form creation and pressing enter won't do anything unles you set Default property to true which will focus that button by default. Note if you have more buttons you have to set this property for each of them. – SilverWarior Aug 01 '14 at 14:51
  • @Silver It might be the default button – David Heffernan Aug 01 '14 at 17:31
  • If that buton is set as default buton then using approach described in the question that you marked this to be duplicate will break its functionality as default buton, becouse that approach will prevent all VK_RETURN key events to reach it. SO why write a whole bucnh of code if you can achieve same thing just by disabling TabStop property of buttons and making sure that none of them is set to be default. – SilverWarior Aug 01 '14 at 21:38
  • @silver Well I'd use a library that does do comms by faking input. Any solution based on input faking will have a high degree of lameness. Your approach means that I am compelled to use the mouse rather than the keyboard. – David Heffernan Aug 02 '14 at 08:54
  • Wait what? Let me get this straight. First you would go and filter any VK_RETURN key press messages to prevent VK_RETURN message to fire the buttons OnClick event which would also break using of enter key on keyboard. Then you would fake imput, so that pressing the enter key on keyboard would fire the buttons OnClick event probably by simulating mouse click on it. By my opinion that is a lot of unnecessary code which could cause tons of other bugs. Wouldn't it be easier to make keyboard hook and inject it into barcode software to simply filter out VK_RETURN message. – SilverWarior Aug 02 '14 at 14:27

0 Answers0