Send keys to WPF Browser control - c#

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.

Related

c# detect which messagebox (originated in another app) button has been pressed

Another application displays a messagebox (with a unique text inside it), user chooses Yes/No.
How to detect what he pressed in c#? (best in .Net up to 3.5). I could do polling with FindWindowEx (on another thread) but how to detect what button had been pressed? Also I don't think polling is the best way to do the job.
I need to know what the user has chosen in another app, so I can react accordingly in my own app. I don't have access to the other app's source code. Also to make it clear I don't want to click any of the buttons myself. I'm not afraid of a bit of c++, winapi and pinvoke
To monitor UI events in another application you can use UI Automation. To solve your specific problem you need to subscribe to a particular event (see Subscribing to UI Automation Events). To do so call IUIAutomation::AddAutomationEventHandler with a UIA_Invoke_InvokedEventId Event Identifier.
While UI Automation can be used to solve your problem, it is an assistive technology, mainly to enable accessibility needs and automated UI testing.
You could use either Anonymous or Named Pipes or WCF(Windows Communications Foundation).

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 to keep focus on more than one window for input devices

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.

How to Receive click location from another process with C#?

My C# application needs to receive a click position from another process, I then need to show on my app. But I don't know how I would implement it.
Could someone help me figure out how to do this?
Thanks so much
What you need is called a "Hook". Windows allows you to hook both the keyboard and mouse events. Basically windows works by injecting the appropriate movements and clicks of the mouse (and keys typed) into the application that has focus.
However using the hook, you receive all of the events, not just those relevant to your app. Once you have the hook established you can then do what you want with the information.
Note that you are going down to the windows OS and if you do the wrong thing here, you can leak the handles and you can also cause windows to get into a bad state.
There is a great tutorial here from MS Technet that describes how to do this in C#.

C# GUI macro libraries or general approaches?

In a C# WinForms application (on .NET 3.5, probably 4.0 soonish), how would you go about allowing the user to define their own macros? For instance, a user could want the key press CTRL+K to mean clicking a certain item in the menu, and entering some text in the dialog opened by the menu, before pressing "Ok" to close the dialog.
Ideally, our software could provide a macro-recording functionality, that would enable a user to just start recording, perform the desired tasks, and the stop recording to have a macro available. This a feature that they are used to from the old version of the software, that was written in FoxPro, a tool that has such capabilities built-in.
The best option for us would clearly be acquiring and utilizing a tried and tested third party library of some sorts, but ideas for rolling our own version are also appreciated.
If I were working on something like this, I would perhaps integrate a general purpose eventhandler to log events that you want to be recordable ( perhaps either as an individual handler added to available buttons or by creating a RecordedEventHandler class to extend EventHandler and then subclassing all your event handling from that - these are just top-of-the-head ideas so I haven't looked into which would be most viable ) and just use the "record macro" button as a switch for that. There are only likely to be a few different types of event that need to be recorded - button clicks, keyboard input on any text fields, keyboard shortcuts and mouse input on any mouse-input fields so it's not an enormously taxing task although of course working out how to store and bind macros and how to facilitate import and export if you were inclined to do so is time consuming but not technically challenging. I don't know of any libraries for this purpose ( although I expect some exist ) and thinking about it I'm not sure how easy it would be to integrate something like that because most peoples' needs in terms of macros are likely to be different.
If you needed to be able to log events around your application then you may find the ManagedWinApi to be helpful.
I have good experience with "command design pattern" for macro-recording functionaly. Every command fire some application-wide (static for example) event with informations abou parameters etc... The recorder listen to this event and persist the sequence of command calling.

Categories