I have an app that runs in the background (minimized/task tray). I need to be able to detect mouse activity (clicks, move) as well as keyboard activity.
What is the best way to do this given the constraint that my window is not "focused" ?
Take a look at this library globalmousekeyhook.
It is 100% managed c# code to install global mouse and keyboard hooks.
It wraps low level hooks into common windows forms keyboard and mouse events.
The magic words are windows hooks. These are created with a p/invoke call to SetWindowsHookEx. You can set up a hook to monitor, among others, keyboard and mouse events. Normally, such hooks are local to the application, but you can also create global hooks. The Microsoft KB shows how.
However, be aware that not all types of global hooks can be used from .NET. In particular, there are only two that you can use: the low-level keyboard and mouse hooks, known as WH_KEYBOARD_LL and WH_MOUSE_LL. Luckily, these are just what you need.
Related
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 have 3, and sometimes more duplicates of an executable, and what I would like to do is to reduce workforce by sharing mouse click and key press events through all applications, with C#.
I am able to get handle's of executables, so ideas might be based on acquired handles.
Also I have done some work to ease my work, all applications are arranged at same coordinates, with the same window size. So basically transparent mouse and key clicks which will interact with all windows (the top window, as well as background windows) would do the trick.
You need to sned Windows Messages from one application to the other, but that involves security configuration.
Here you have how to do it:
Communication between applications via Windows Messages
Here you have comments about security issues, and ideas for alternative solutions
Sending, receiving and processing a windows message between windows applications
Stacking the windows isn't necessary. Once you have the handle to all the windows that you want to interact with, P/Invoke the SendMessage API to send your click to each window at it's target coordinates.
If I had the need for such a thing, I would put a picture box on a form and capture the image of one of the windows (so I can see what I'm clicking), then process the PictureBox.Click event to calculate the coordinates to use in the P/Invoke call(s).
There are several other tricks you could use to make your life easier and click-sharing utility better, but this will get you started.
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've developed a WPF application that registers Hotkeys using com interop and processes them using WndProc.
The hotkeys work perfectly even when the application doesn't have focus. However, they don't work when certain fullscreen appications have focus (games). Is there a way for me to fix this?
Thanks!
The disabling of hotkeys is by design and as far as I know not much you can do about it.
Full screen DirectX applications (typically games) will call IDirectDraw::SetCooperativeLevel with the DDSCL_FULLSCREEN and DDSCL_EXCLUSIVE flags which results in, amongst other things, the registered hotkeys being disabled.
Even though registered hotkeys are disabled, you might be able to process your hotkeys via a low level keyboard hook. They can be kind of tricky to implement right, but you can look at the AutoHotkey source to see how it handles hotkeys in a low level keyboard hook.
This is a good tutorial on setting up a low level keyboard hook in C#.
how can i record and playback mouse and keybaord events.
i need this to capture the user interactions with my application so that later on i can play to see what user did.
There are literally hundreds of keyboard / mouse automation apps out there:
http://www.nonags.com/nonags/auto.html
I recommend Do It Again - its free, easy to use and works well, although if I remember correctly it has a quirk where it doesn't work particularly well over a remote desktop connection.
UPDATE: Just re-read the question, I dont think what you want is the ability to record keyboard / mouse actions, as its not guarenteed that the application will "keep up" with the mouse clicks (windows could open in slightly different places, or there could be a slight delay etc...)
What you want is some screen capture software.
You should hook keyboard and mouse events, see Processing Global Mouse and Keyboard Hooks in C# , Low-Level Mouse Hook in C# or Processing Global Windows Mouse and Keyboard Hooks in C#