Let's assume that we've got 2 windows processes ,
Process A is the sender, and Process B is the receiver.
Process B is running a classic Win32 API events loop
How do I generate and send mouse and keyboard events from process A to B ?
Basically via SendMessage or PostMessage. If you want to simulate input events for the whole operating system, then SendInput might be interesting.
You may want to check TestAPI in Codeplex it includes some C# classes that wrap SendMessage and PostMessage APIs (http://testapi.codeplex.com/SourceControl/changeset/view/35517#424245)
TestApi actually wraps up SendInput internally, and exposes a couple of simple classes -- Mouse and Keyboard -- to help you simulate input. SendInput provides the most general way to inject input, but is a notoriously tricky API to use -- the wrappers simplify the usage greatly.
See Link for specific usage examples.
Related
I have created a windows service in c#. How can I call a function whenever a key is pressed, and in this function get the value of the key. I need it for both key up and key down.
My goal is to send an email if a certain series of keys were pressed. For example if you press h-e-l-l-o, no matter where you type it even on the desktop, the program should send the email.
Also, it's ok for me to implement it in another way. I need a program that runs in the background and do something when a key is pressed.
How can I do such thing?
An example would be helpful.
As somebody has already commented, a Windows service does not have a foreground window which can receive keyboard focus or Windows keyboard messages. So your only option to achieve exactly what you're after is to use a low level event hook to receive keyboard events.
Note, however, that these are system-wide events and your service might be flooded with them. They're useful in applications like screen readers and other assistive technology, but I'd recommend trying to think about whether there is some other way to do what you want to do before using this approach. For example, can you just run an application in the system tray and subscribe to WM_HOTKEY messages via calls to RegisterHotKey?
Here's an example of a low-level keyboard hook in C#.
The best solution I found:
Generally author creates .NET wrapper on a low-level methods from user32.dll assembly that make using those methods quite pleasant and enjoyable.
private LowLevelKeyboardListener _listener;
_listener = new LowLevelKeyboardListener();
_listener.OnKeyPressed += _listener_OnKeyPressed;
_listener.HookKeyboard();
void _listener_OnKeyPressed(object sender, KeyPressedArgs e)
{
this.textBox_DisplayKeyboardInput.Text += e.KeyPressed.ToString();
}
here the original link
The idea is to do something like what a keystroke catcher does, but with multiple input devices. I want a window to record input from devices even if the focus is on another window. What libraries or methodologies would allow me to accomplish this?
This is typically handled via a low level Hook. There is no C# library which will handle this directly, though there is a Microsoft KB article showing How to set a Windows hook in Visual C# .NET.
In my program I use class Process to start another application. This application starts fullscreen. My purpose is to handle mouse click from that application in my program. What WinApi functions should I use?
By "WinApi", I assume that you mean "Win32".
In order to handle messages of another process, you need to install a Win32 hook. See this article for more details. You need the WH_MOUSE hook and the SetWindowsHookEx Win32 API.
A hook function needs to be in a DLL, so that it can be injected in any process. You will need to filter the messages you get for the process that you started.
A hook function needs to be a global function so you must code it in C++. You will also need to use some inter-process communication to well, communicate with your main program, if needed.
I want to build a win app using C#...
I want this app to accept letter from the keyboard... and I want that to be done either the text is written via this program or another... its will be much better if I can choose programs I want to spy on...
in another words... I want my program to get every thing presses on the keyboard and everything is being written on firefox,opera,internet explorer witch are running at same time with my program...
You need a global keyboard hook, which will allow your application to listen in on keyboard input events system-wide. You implement this by P/Invoking the SetWindowsHookEx function and specifying the WH_KEYBOARD_LL flag for the idHook parameter. The entire process can get slightly complicated.
But it turns out that you're in luck. Stephen Toub has already written the code for you here on his blog: Low-Level Keyboard Hook in C#. Just drop this into your project and you're in the spy business.
I believe what you are looking for is a keylogger...
if so you can find information on:
http://www.axino.net/tutorial/2009/02/keylogger-in-c-introduction
There's a fairly comprehensive article on this over at Code Project: http://www.codeproject.com/KB/system/KeyLogger.aspx
While that article is based around C++ etc it covers a lot of the technical details you need to know.
There is an example C# project here: http://www.codeproject.com/KB/system/simple_key_log.aspx.
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.