Is it possible to block keyboard input selectively in Windows? How? - c#

My app (in C#) need to interface with a USB bar-code scanner, which is basically working like a keyboard. It inputs the bar-code with an enter key at the end.
The app need to be work even when it's at background, so I am using low level keyboard hook to get and filter the bar-code out in the global key events. This part is already working.
Here is my problem: I don't want other apps to get the keyboard(scanner) inputs if it is a bar-code. And the normal key events should not be interfered. In one word, block the key events selectively. Is this possible?
My app is in C#, but I have no problem with C++ or more native solutions as long as it's easy to integrate in C#.
Thanks.
Additional Information:
The whole idea is working at background, even when it's not active. It watches the global key events stream and spot the bar-code sequence (already implemented with Hook). And most importantly, it do NOT interfere with normal keyboard events nor other applications' operation. That's why I cannot block all the key events or make it top-most.
I already can get the bar-code. I need to prevent other applications from getting the bar-code.

At the end of your keyboard hook you would call CallNextHookEx to execute next hook in the chain.
I would suggest that put some unique signature as a preamble for your barcode so that your keyboard hook procedure can detect it as a valid barcode input from your scanner. Now, when you get this data, just skip the call to 'CallNextHookEx' so that the chain will be discontinued and other programs won't get your barcode. Otherwise - call 'CallNextHookEx' so the chain can continue.
Note: This is my theory, I have never tried the exact same thing myself. I have however, written hooks in C++ and C#.

Check this project out
http://globalmousekeyhook.codeplex.com/
It is in C# as well so will make your coding easier. Sounds like all you need is to hook up the global key press event and suppress it by setting the Handled value or something similar.

Related

C# Hooking specific Keyboard keystroke and stop system propagation

I have an application where I have multiple keyboards connected through USB.
I need to hook a specific keyboard to get the keypress directly into the software, even if this one is not on the foreground. This so far works based on this project. The other keyboards shall work as normal.
Although it seems by using Direct Input, it is not possible to stop propagation (we don't want other apps to get that particular keyboard input).
Concerning Global Hook, it can block the keypress system-wise, but it is impossible, as far as I know, to identify the source of the keystroke (which keyboard it is from) and thus to selectively block them.
There is another project, here, that combines the two, but it is quite messy and heavy.
Is there a better way to achieve this? I am surprised that simple task is so complex.
If you are going to make the app windows only, you should look into Windows raw input api
It isn't that complicated.

.NET disable keyboard shortcuts - from a SPECIFIC input device

I would like to disable, with run-time configurability, various keyboard combinations (Alt+Tab, Ctrl+F, etc.) from the primary keyboard on Windows.
I'd still like to be able to emulate these key combinations through software-based means, such as the approach shown at https://inputsimulator.codeplex.com/.
Every example I've found disables the key combinations completely, but I only want to disable the key combinations if they come from the main keyboard.
Any thoughts on the feasibility of this? My current prototype is a .NET service that is written in C#.
What you're asking for is not possible. Windows discards the device ID early in keyboard processing; by the time it reaches user mode (a low-level keyboard hook, say) the device ID is gone. Raw Input lets you track the device-specific input, but doesn't let you block it.

How to check keystroke in WindowService [duplicate]

I have to write an application in C# that listens to any keys being pressed. In actuality I have a bar code scanner sending the "key pressed" event, and I need to listen it... what it does from there is beyond the scope of my question.
My security requirements are that there is not allowed to be any sign-on to the machine in any way shape or form AND this must run as a windows service. The user will start the machine and walk away (i.e., no desktop session).
I'm assuming I'm going to have to go unmanaged for this.
Given the security requirements is this even possible? If so, any pointers on where to start would be great.
Thanks in advance,
Jay
Try keyboard and mouse hook
http://www.codeproject.com/KB/cs/globalhook.aspx
You will have to learn pInvoke with combination of learning how to look for the right events produced at the lower level of OS. By calling into user32.dll using pInvoke your managed code can filter those events through the hooks.
http://pinvoke.net/default.aspx/user32.SetWindowsHookEx
If you need to see keyboard presses for all apps, system-wide Hooks would be the way to go normally, but the problem is that the security changes in Vista and above make hooks rather less useful. A process cannot hook another process at a higher integrity level, so there's no guarantee you will see all events.
However if you only need to see events going to a particular app - the one reading the bar codes - then provided you can identify that process, a thread-specific hook will suffice and the integrity question will not arise.

How to retrieve a string in a C# Key(*)Event that was inserted by scan? (Windows CE 5)

I am working on a Win CE 5 application that captures data scanned via barcode scanner. The application should support some "system-barcodes", predefinded barcodes that trigger functions of the application.
Those barcodes have to work in the whole main-frame. So I set the property "KeyPreview" of the main-frame to true and registered an KeyEventHandler on the KeyDown event of the main-frame.
My problem is, I couldn't figure out how to get the full scanned string. The string is 12 characters long and ends with a newline.
Is there a possibility to get the whole string in the EventHandler?
Thank you in advance for any help.
Most, if not all, barcode scanners based on Windows CE inject the scan data as keyboard data to the device driver. The easiest way to intercept that data in your app, where you don't have to hook up handlers to every form and worry about controls getting the keys before your processing logic is to use a keyboard hook. It works just like on the desktop, so any code you find that applies to keyboard hooks on the desktop would be valid (with the exception that the DLL containing the APIs in WinCE is coredll.dll).
There is an example of keyboard hooking for Windows Mobile on CodeProject which would probably give you the bases for everything you need. From there, it's simply string parsing in the hook handler.
Using any of the Key events by setting the Form.KeyPreview property should allow you to see the data from the scanner. But you will only see it one "key" or character at a time. Those events only handle a single character or key on each call. As far as you app could tell, it looks no different than a user smashing keys on the keyboard.
If you have the option, and can put your scanner into a non-keyboard emulation mode and hook into scan events directly, then you would probably be much more satisfied with the results in terms of how it can work independently of the GUI when used that way. Typically with devices that can do that, you will receive the entire scan as a single event.
I'd suggest including the details about the device(s) you are using so someone might be able to give you more specific advice that might be relevant for the hardware in question.

Capture key events from a Windows Service in C#

I have to write an application in C# that listens to any keys being pressed. In actuality I have a bar code scanner sending the "key pressed" event, and I need to listen it... what it does from there is beyond the scope of my question.
My security requirements are that there is not allowed to be any sign-on to the machine in any way shape or form AND this must run as a windows service. The user will start the machine and walk away (i.e., no desktop session).
I'm assuming I'm going to have to go unmanaged for this.
Given the security requirements is this even possible? If so, any pointers on where to start would be great.
Thanks in advance,
Jay
Try keyboard and mouse hook
http://www.codeproject.com/KB/cs/globalhook.aspx
You will have to learn pInvoke with combination of learning how to look for the right events produced at the lower level of OS. By calling into user32.dll using pInvoke your managed code can filter those events through the hooks.
http://pinvoke.net/default.aspx/user32.SetWindowsHookEx
If you need to see keyboard presses for all apps, system-wide Hooks would be the way to go normally, but the problem is that the security changes in Vista and above make hooks rather less useful. A process cannot hook another process at a higher integrity level, so there's no guarantee you will see all events.
However if you only need to see events going to a particular app - the one reading the bar codes - then provided you can identify that process, a thread-specific hook will suffice and the integrity question will not arise.

Categories

Resources