So is there possible to intersect all keys pressed everywhere, where everywhere is at any application? More or so like a keylogger. I was wondering if that is possible in C++ or C#.
Regards
What you're looking for is a Keyboard Hook. This is possible using some P/Invoke. See sample here:
http://www.codeproject.com/KB/cs/CSLLKeyboardHook.aspx
If you want to use winapi thaen function you are looking for is SetWindowsHookEx with flag WH_KEYBOARD. If you want to get really all kes you might use low-lewel flag instead WH_KEYBOARD_LL, but this will not translate keystrokes, so it's more difficult to work with.
I never used this flag, but i know that some flags need registered hook function to be in separate module (eg. dll) as they will be loaded and executed executed in context of application that actually recieves keyboard input. If it is so you must also think of a mechanism of returning colected data back to your application, cause global variables will not work.
Related
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.
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.
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.
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.
I'm creating a program for my personal use that must react to some hotkeys. For example, when I press ctrl+win+z it must perform a certain action. At present, I can use GetKeyState() from user32.dll to detect the keypresses, however I am unable to stop them being passed to the active application. In the case of ctrl+win+z, the action I want to happen occurs, but if I am using explorer, for example, explorer will also read that as an 'undo'.
The obvious solution would be to use "SetWindowsHookEx()" (also from user32.dll), however each time I try a solution involving it (anything similar to this site's example code) I get a massive slow down the first time I use the hotkey. This is not acceptable.
I hope someone can help. I can provide more information if required.
Have you tried the RegisterHotKey function? It's definitely easier to use than a windows hook.