Capture keyboard input after a hotkey is pressed, without app focus - c#

I have a bar code scanner that presents itself as a USB-HID keyboard.
How do I capture scanned bar codes in my c# WPF application, if the app currently does not have focus?
When a barcode is scanned, the scanner sends one or more configurable prefix keypress events, then the barcode itself, then some postfix keypresses.
If the application has focus, I can use the prefix hotkeys to focus a specific textbox; then the barcode is automatically entered into this textbox.
If the application does not have focus, how can I receive the barcode?
I could register a global hotkey via pInvoking RegisterHotkey from User32.dll, as outlined e.g. here:
How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?
Global hotkeys in WPF working from every window
But how do I capture the following keyboard events containing the bar code?
I am a little dubious about this approach as well, is there a simpler way to capture a string prefixed by a specific hotkey, without having focus?

Related

How can I capture a key press, outside of the form?

I have been trying to capture the keys pressed outside of my winform, but obviously a KeyPress event won't work.
I haven't been able to get any closer than the KeyPress event, which only works on the form level, as specified
I suspect that I will have to do the
[DllImportAttribute("user32.dll")]
, but I have little to no experience with that.
Being able to capture key presses anywhere requires using Hooks.
There is a library on CodePlex which simplifies implementing Application and Global Mouse and Keyboard Hooks for C# users.

Converting WPF KeyDown events to a string

I have an application that can receive key strokes from a barcode scanner (which to Windows just looks like a USB keyboard).
My requirement is for the user to be able to use the barcode scanner anywhere in the application (any window, any tab) and have it react accordingly. I already have a version of this working by monitoring for PreviewTextInput events in my App.xaml.cs and firing off my own custom BarcodeArrived event. The problem with this is that if the user has put focus on a control that does not accept Text Input, then PreviewTextInput never fires.
PreviewKeyDown does always fire, but the data it presents is ugly and I can't seem to find anyway of translating KeyDown events to a normalized string. I found a Stack Overflow article at "Convert received keys in PreviewKeyDown to a string" seemed promising, but it does not appear that I can wire this in my App.xaml.cs (no dependency object).
Any thoughts or suggestions would be great. Or, as an alternative, at least a way of detecting which keyboard the input is coming from, at which point using PreviewKeyDown might be a viable option because I can assume a "dumbed down" input stream.

Send keystroke to non-foreground window

According to this article on msdn, it is possible to set a specific window as the foreground window, and then send keystrokes to it... but I need to send keystrokes to a window in the background, so that whatever the user is doing can continue uninterrupted.
Maybe you should just catch the keystrokes in your foreground window and pass them to the background window (delegate). Or if they are somewhat complex you could use global hotkeys. Anyways passing the keys from your foreground form to your background form should be the better solution.
I found a nice example it's not about hotkeys but the concept should apply in your case as well.
http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx
So basically a keystroke listener? Java has a robot class that listens for keystrokes and can act upon them (ex. me typing "Hello World" and the class will automatically write that to a file or other window).
I've created something similar in the past. You can create a java program to run in the background in a constant while loop and just listen for keystrokes. Set the program to write each keystroke to a file or specific place. Because this is a short while loop and only reacts when a key is pressed, it takes up almost no processing power and does not affect the performance of the program in the foreground.
I do this with shortcut keys. I have an application that has multiple child windows along the side. The main window enables the Form.KeyPreview property of the child windows and then registers for the appropriate event (KeyPress, KeyDown or KeyUp) on that child window. With this setup the main window can process all the keys from the child windows.
The technique I've used on Windows Mobile/CE is to hook the keyboard and then simply use PostMessage() to send them to the target window to be handled as normal. This technique should also work on the desktop. There are several examples on Code Project of hooking the keyboard - http://www.codeproject.com/KB/system/globalsystemhook.aspx. Your question was not clear if the foreground window was part of your application or not. If it is, wouldn't you simply subscribe to one of foreground form key events from the background form?

how to use console/windows app without focus on it?

im going to create a KEylog application that enables me to write all data typed(keys pressed) on a text file/database how can i do this without focus on the windows app/console app?
for a reason , for all you to know, this is for my PC and im not trying to hack an account.
just for me to know what they are doing on my computer.
Find an example written in .NET here:
Processing Global Mouse and Keyboard Hooks in C#
This class allows you to tap keyboard and mouse and/or to detect their
activity even when an application runs in the background or does not
have any user interface at all.

Writing in notepad with keyboard application

Keyboard simulator ، Like Like On-Screen Keyboard
how to make Like "On-Screen Keyboard " ?
In old win 32 API there was a sendKeys() method like API. when user clicks your form, then your form is active and has focus while he has activated the desired control for writing just before giving focus to yours. One solution is monitoring focus changes on controls and when user presses keyboard on your form, you can sendKeys() to appropriate window handle. I don't know what's the real name of that method and it's equivalent ic .NET but it should not be hard to find out. Stick to design.
[edited]
here it is :
http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

Categories