locked
How can I catch the enter key throughout my application? RRS feed

  • Question

  • I have an application with several forms, panels and user controls.  The application uses a barcode scanner.  Whenever a barcode is scanned, my application receives a string followed by the enter key as if someone would have typed the barcode with the keyboard and would have hit 'enter' when done.

    I added an OnKeyDown event to a list view that I have in a panel to grab the barcode.  The problem is that if the focus is not given to the list view, the scan is lost.  In my case, there is not a single spot where I could place the OnKeyDown event handler to fit all cases.

    I need to have a single handler across the entire application, how's that possible?  I would prefer not to use a timer to constantly give the focus to a certain control which would implement my OnKeyDown event, I think this is dirty.

    Any help would be appreciated,

    Dom.

     

    Monday, February 5, 2007 3:18 PM

Answers

  • How can you tell the difference between the user typing on the keyboard and the barcode scanner generating keystrokes?  To catch keystrokes before they reach a control, set the form's KeyPreview property to try and implement its KeyDown event.  You could do it at application scope by writing a low-level keyboard hook.  I wouldn't recommend it though...
    Monday, February 5, 2007 8:53 PM

All replies

  • How can you tell the difference between the user typing on the keyboard and the barcode scanner generating keystrokes?  To catch keystrokes before they reach a control, set the form's KeyPreview property to try and implement its KeyDown event.  You could do it at application scope by writing a low-level keyboard hook.  I wouldn't recommend it though...
    Monday, February 5, 2007 8:53 PM
  • That's excellent.  Good thing that Microsoft thought of the KeyPreview property, works like a charm!

    I know the data is from the barcode scanner because it is the only input device connected to the computer running the application.

    Thanks again for the tip!

    Monday, February 5, 2007 11:07 PM