Issue keyboard input to a particular window without interrupting background in Windows - c#

I need to be able to issue keyboard input to a particular window in the background on a periodic basis without interrupting the user. How could this be done?
Use case:
I have two windows open: one window is waiting for the input (window A), and I'm actively working in another window (window B). While working in window B, periodic commands are issued to window A through the keyboard without interrupting the interactivity going on in window B.
I'm hoping to accomplish this using Python or C#...whichever gets the job done cleanest and fastest (or a nice hybrid thereof).

Check winGuiAuto.py. If you can find the hwnd for the control whose input you want to change, you can send it to it even if it's not the active window.
Even it doesn't solve your problem directly, the source code is a good lesson on using the win32api with Python and should help you anyway.

Related

C# forward mouse events to another window without losing focus

My app screencaptures another window that runs on a second monitor. Now I'd also like to forward mouse clicks made in my app to that window. I tried using SendMessage in user32.dll for this, but this also makes window focus switch, which causes some issues, like the two windows rapidly fighting for focus. Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
Is there are way to place those mouse events without making the hidden window active and losing focus on the main app?
No, there is not even a way to forward mouse input to another receiver. Messages are only part of the input processing. The system also does internal bookkeeping and you cannot replicate that.
The only reliable way to inject input is by calling SendInput. Doing so doesn't allow you to specify a receiver. Input goes to whichever thread is determined to be the receiver by the system.
Although, more often than not, this question is asked when the problem that needs to be solved is a different one altogether: How do you automate a UI? The answer to that question is UI Automation.

In Windows how to get highlighted text in a window that belongs to another process using C++ or C#?

It's in a C++ translation program. When user selects some text in some other program without pressing CTRL-C, I want to get the highlighted text so that I could translate it. C# solutions are welcome because this(text acquisition) is a relatively standalone component.
One possible solution is to add hook to mouse event. I detect the following messages: mouse drag with left button hold, then release left button. Then I send CTRL-C to the program and get the text from clipboard.
I save and restore clipboard so most programs would work well with the method. But I have observed some programs would process events like WM_KEYUP in their message loops. For these programs I send fake messages! Is there another safer way to get the highlighted text?
I was trying to find some API call to get text between two mouse cursor positions, but I cannot find one.
The right way to write a Windows program that interacts with another programs GUI interface is to use UI Automation. Specifically, you'll write a UI Automation Client.
It requires a bit of work, but a UI Automation solution will work with almost every other application. A hacky solution built on hooks and messages will also be a lot of work, but it will be very fragile. It will only work with certain kinds of applications in certain contexts. Lots of things can go wrong.

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.

Handling WM_ events in a windowless C# process

I've been looking around for quite a while, and can't seem to find a good way to do this.
Basically I have a C# process using WPF (which has no visible window), that I need to handle WM_ events in (such as WM_CLOSE or WM_DESTROY for example; so that I can elegantly shutdown when a user chooses to log off or restart their machine).
There are a number of solutions I've seen out there that suggest using System.Windows.InteropServices to call AddHook and provide it a pointer to a function that then becomes the WndProc. The problem with this is, as far as I can tell, it depends on the window actually being visible (and in this case there is no window).
Another way that's suggested but doesn't work is to override the WndProc method of a WinForm, but this process has no visible forms or windows.
I've also found things referring to a Message-only Window. Some kind of invisible window that still receives WM_ events. From what I've seen, this is only available in a Microsoft.WindowsCE.Forms assembly. I added a reference to this assembly in my project and subclassed MessageWindow as indicated at: http://msdn.microsoft.com/en-us/library/microsoft.windowsce.forms.messagewindow.aspx but it still seems to not work. The breakpoints inside the WndProc are not being hit.
Any clue?
Think about what you are asking- if you don't have a window, how could your application receive a window message (considering that messages are sent to a window's handle).
That's like saying "how can I receive email without having an email address?"
Michael Entin covers windows' behavior during shutdown here.
I am 99% sure that all processes running in a user's session are automatically closed when the user logs off anyway, so this shouldn't be an issue. If you really must handle this window message, you can create a hidden window as per Any way to create a hidden main window in C#?

Send keystroke to non-foreground window

According to this article on msdn, it is possible to set a specific window as the foreground window, and then send keystrokes to it... but I need to send keystrokes to a window in the background, so that whatever the user is doing can continue uninterrupted.
Maybe you should just catch the keystrokes in your foreground window and pass them to the background window (delegate). Or if they are somewhat complex you could use global hotkeys. Anyways passing the keys from your foreground form to your background form should be the better solution.
I found a nice example it's not about hotkeys but the concept should apply in your case as well.
http://www.codeproject.com/KB/cs/pass_data_between_forms.aspx
So basically a keystroke listener? Java has a robot class that listens for keystrokes and can act upon them (ex. me typing "Hello World" and the class will automatically write that to a file or other window).
I've created something similar in the past. You can create a java program to run in the background in a constant while loop and just listen for keystrokes. Set the program to write each keystroke to a file or specific place. Because this is a short while loop and only reacts when a key is pressed, it takes up almost no processing power and does not affect the performance of the program in the foreground.
I do this with shortcut keys. I have an application that has multiple child windows along the side. The main window enables the Form.KeyPreview property of the child windows and then registers for the appropriate event (KeyPress, KeyDown or KeyUp) on that child window. With this setup the main window can process all the keys from the child windows.
The technique I've used on Windows Mobile/CE is to hook the keyboard and then simply use PostMessage() to send them to the target window to be handled as normal. This technique should also work on the desktop. There are several examples on Code Project of hooking the keyboard - http://www.codeproject.com/KB/system/globalsystemhook.aspx. Your question was not clear if the foreground window was part of your application or not. If it is, wouldn't you simply subscribe to one of foreground form key events from the background form?

Categories