I would like to know how can i detect left mouse up in the active window.
I googled but i was not able to find anything.
I don't see any mechanism to subscribe to that event in the active window.
Maybe using Windows API functions but how?
There is no trivial way for getting the job done in Outlook. The only possible way is to use Windows API functions like SetWindowsHookEx which installs an application-defined hook procedure into a hook chain. You would install a hook procedure to monitor the system for certain types of events. For example, you may find the MouseProc callback function helpful.
Related
I am trying to get when the left mouse button up is hapenning on a specific window. So I googled and find an interesting code snippet here.
That code snippet detects in which window the mouse click was done so i could modify a little to make it work. But the problem is that the hook using SetWindowsHookEx is not being set on a specific window, instead it listens for all mouse clicks in all windows (independently of the window) so i am interested in only set the hook in a specific window.
I have the window handle of the window in which i am interested in but now i need to know how to set the hook only in this window. How can i do this? I don't want to listen mouse events globally on all windows.
Thank you very much and sorry, i am completely new in using hooks.
SetWindowsHookEx() hooks are installed either globally or per-thread. It has no concept of windows.
The code you linked to is using a WH_MOUSE_LL hook, which can be used only globally not per-thread, and as such its callback does not give you any information about which window receives each mouse event. It does, however, give you the screen coordinates of each mouse event. You can determine the current HWND located at those coordinates by using WindowFromPoint() and ChildWindowFromPoint/Ex().
On the other hand, an WH_MOUSE hook can be installed per-thread. You can get the thread ID of a given HWND by using GetWindowThreadProcessId(). This way, the hook callback will give you mouse events only for windows owned by that thread, and will also give you the HWND that receives each event, so you can ignore events for windows you are not interested in.
Note: if the HWND you are interested in monitoring belongs to another process than the one that is installing the hook, you will have to implement your hook callback in a DLL. Doing that is not required when using a WH_MOUSE_LL hook, or when hooking a specific thread that is owned by the same process that is installing the hook.
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 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 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.
I am going to be checking if the user is moving any window around (my application does not have an interface) and respond accordingly. What do you think is the best way to do this? Can I determine if the user is clicking on a titlebar? Can I determine if a window is being moved? I then need to grab the hWnd of the window after I know it's being moved.
To get notifications for all windows, not just Windows Forms ones, you'll need to use a hook set by the SetWindowsHookEx() API function. You'll need a WH_CALLWNDPROC hook so you can see the WM_MOVE message that Windows sends to the window.
Unfortunately, that's a global hook. The code that implements the hook callback needs to be packaged into a DLL so that it can be injected into all target processes. That shoots a hole into your plans to use C# for this, you can't inject the CLR. The DLL must be written in unmanaged code.
This code project offers an approach, including the unmanaged injectable DLL you'll need.
here is a technique to spy on window handles. You can inspect all the handles which are open and wait for the move messages.
EDIT
.NET spy code.