Control OS Mouse Event using Windows Form - C# - c#

The program I am trying to do is to simulate the mouse event of a operating system using keyboard with Windows Form. Right now I am able to change the cursor and do different actions like mouse clicking inside the Form (when the Form is on the Top).
The problem is I would like to extend it to the whole Operating System, which means even if my Windows Form is not at the top, I am still able to control my cursor and do all sorts of mouse event on other applications while the Form is running. How should I do to implement this ?

You might want to look at this library Global System Hooks in .NET which uses global system hooks to detect all mouse and keyboard events include those outside of your application.

You can synthesize OS-wide keystrokes, mouse motions, and button clicks using the Win32 SendInput() API. You can call it from C# using P/Invoke. Sample code can be found here: SendInput on PInvoke.net.
I remember back in the day I used the SendInput (and a screenshot API) to create a Minesweeper bot in C# (2.0 I think). It could solve an Expert puzzle in about one second. I wish I still had the source code to sample here, but I don't.
EDIT: It appears someone has already created a nice .NET warpper for the SendInput(): Windows Input Simulator on CodePlex.

Related

Determining (programmatically) who controls the mouse on a PC using C# or C++

Is there a way to determine who is controlling the mouse (and which mouse) on a PC programmatically? I recently installed LogMeIn (logmein.com) and wanted to know if it's possible to (1) tell within a program if the mouse is being clicked/moved by the direct user or by a remote user, (2) write a stand-alone program that simply shows mouse events (on any application) and whether or not the mouse event was generated by a local or remote user. I am somewhat familiar with Win32 hooks, but don't think that they can give this sort of information. Regarding (1) it would seem like a common request. I.e. "Only allow user to complete button presses related to password change if he is local..." or something like that.
Of secondary importance (just academic interest actually) is the question of telling whether the local user is using the mouse or the trackpad.
you can hook the device API's of windows that you want to get the information from, then if the mouse moves the api is probably not called when the user movers the mouse, but if logmein does then it probably calls some SetMousePointerPos-like C WinAPI.
Since posting, I came across this post which basically answers the question.
C# Get Mouse handle (GetRawInputDeviceInfo)
I'd add that for those of you who want to use WPF instead of WinForms (as the example above uses), check out ComponentDispatcher.ThreadFilterMessage (for WPF) or IMessageFilter (Windows Forms).
I wrote a couple of programs based on the above posting (one is basically the posting above with some minor additions and the other is a WPF (as opposed to WinForms)). If I can figure out GitHub I'll post all the code and add a comment here. But the posting above definitely gives you all you need.

How can I listen to mouse events of other processes

Is it possible in C# to listen to mouse related windows messages sent to other windows in other processes?
There is a open source project in codeplex for that
Application and Global Mouse and Keyboard Hooks .Net Libary in C#
If you want to do this yourself do some extra search on Windows Hooks
This article shows how
You can take a look at the Codeplex Application and Global Mouse and Keyboard Hooks library
This library attaches to windows global hooks, tracks keyboard and mouse clicks and movement and raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need:
Mouse coordinates
Mouse buttons clicked
Mouse wheel scrolls
Key presses and releases
Special key states
From above links Documentation:
Before beginning it is important to know that hooking terms can and will be used interchangeably. Global hooks are also known as System, or System-Wide hooks, and will encompass the entire operating system. Application hooks are often called Local, because as the name implies, will only correspond to the single application which placed the hook (or a specific thread).
There is no managed way to do this.
However, with Windows Api this is possible:
This should help you.
C# - Capturing Windows Messages from a specific application
It would be a good start to capture WM_MOUSEMOVE
http://msdn.microsoft.com/en-us/library/windows/desktop/ms645616(v=vs.85).aspx
But there are other mouse related window messages, too.

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.

Send keys to WPF Browser control

Can I programatically send [UserID]{TAB}[Password]{CARRIAGE RETURN} to a webbrowser control which has a userID, password and Sign-in button there. I wanted to use my own virtual keyboard in my application. Any tips here?
Sorry for the late answer but I've just finished a similar project and as part of the work am in the process of open sourcing two projects to Codeplex.
The first is the Windows Input Simulator which is a simple .NET wrapper around the Win32 SendInput written in C#.
The second is a very customisable on screen keyboard or touch screen keyboard control and toolkit called WpfKB and will be available as an initial release tomorrow. Hope these are of help to you or anyone else who comes across the projects.
I recently had to implement automatic authentication through a WPF browser control, and I looked into simulating keystrokes. I didn't need a full virtual keyboard so interacting with the DOM of the login page through IHTMLDocument2 ended up being the best approach, but I looked into keystroke automation before making that decision and found a few options.
You can raise the appropriate routed events on the control as described in Simulating basic keyboard events and Simulating text input. I don't know of any specific problems with this approach but I opted against it simply because I wasn't comfortable simulating input without looking at how the CLR handles the actual input, and without at least raising the complete lifetime (PreviewKeyDown, KeyDown, PreviewKeyUp, KeyUp) I was wary of unintended consequences.
Take a look at WOSK on CodePlex. It's a good example of how to invoke Win32 keybd_event and SendInput functions to generate the low-level input messages via Managed Windows API to simulate input. There's some unnecessary fluff (eg transparency) and some odd WPF usage, such as using a CommandParameter with a Click event instead of a Command on the buttons, but the general approach is sane and it's reasonably complete.
You can also invoke the windows on-screen keyboard as alluded to by Jeroen. I didn't try this because I didn't need a virtual keyboard, but if you're going to call into Win32 anyway, you might as well follow the WOSK model and build the UI the way you want it.

Detect and Prevent Overlapping Windows in C#

Anyone know of an efficient way of detecting movement of any windows currently open on a windows system? I need to detect a window's movement, determine if it collides with my applications Form, and bump it out from underneath if necessary.
I know I can scan through an enumerated list and check each window -- but that is way to intensive to perform constantly.
Background:
I have a taskbar-esque application that docks on the side of a user's screen. When the "Always on Top" feature is on, maximized windows will take up the remaining available space without covering the toolbar, as expected.
However, if you drag a non-maximized window over the toolbar, the application goes behind the toolbar (also expected), but you can no longer grab onto the title bar to move it back -- the window is stuck unless you disable "Always on Top" and then move it. So, I want to bump the window out from underneath.
Although not a direct answer, one possible solution to this is to create your application as an application desktop toolbar rather than a regular window. From the docs:
An application desktop toolbar(also called an appbar) is a window that is similar to the Microsoft Windows taskbar. It is anchored to an edge of the screen... The system prevents other applications from using the desktop area occupied by an appbar. (emphasis added)
This may not be a great fit for your scenario because it is oriented towards COM and unmanaged code rather than managed apps: however see this CodeProject article for info about using this feature from C#.
Failing that, you could try installing a hook (see SetWindowsHookEx) and listening for move messages but this is pretty low-level...
Try checking your PaintEventArgs ClipRectangle ..
(edit: and/or WindowFromPoint shooting match)
You can get notification of window movements using a CBT Hook: http://msdn.microsoft.com/en-us/library/ms644977(VS.85).aspx
http://www.codeproject.com/KB/dialog/FindWindow.aspx?msg=3262771
"FindWindow By Jörg Bausch"
Will get you the external (not your app's) window ID (IntPtr) the mouse went up over from within your C# application. For the desktop, and everything else on the desktop, it will return the same pointer (you can't distinguish, using this code, between as mouse-up on a folder, the desktop, the Recycle Bin).
http://www.codeproject.com/KB/cs/globalhook.aspx
"Processing Global Mouse and Keyboard Hooks in C# By George Mamaladze"
Will allow you to create GlobalHook for keyboard and mouse-events in C#. I've used it recently in VS 2010 beta 2 : it is NOT USABLE compiled against FrameWork 4.0, but does compile and work okay against FrameWork 3.5 and lower. If you download only George's demo app, be aware the download doesn't include the required dll, and will fail when you launch the .exe file (which I have brought to George's attention).
I've never worked with a "desktop application toolbar;" I hope this is relevant.
best,

Categories