Opening a Processes in it's own instance - c#

Is there a way to start a process in a separate instance with it's own mouse and keys that don't interfere with the use of other processes.
For Example: Let's say I want to make a program that automates a program and it requires clicking on locations and trying in keys. While this is happening I want to be able to do other things on the computer like nothing is happening.
Attempt At Clarity:
So this separate instance would be given inputs for the program based on what it sees or something like that. No separate output from the user. More like the program is running the process in the background while youre using your computer like normal

You can use .NET for input simulation using WinForms.
For keyboard input you need SendKeys and for the Mouse you can use Cursor. Although it should be noted that the OS - usually - only includes one mouse pointer, and so setting it's position is machine wide.
For a better simulation of mouse input, you will need to resource to p/invoke. The recommended pattern is to use SendInput.
Alternatively, you could look for existing solutions that emply this technique, such as InputSimulator.

Related

C# Hooking specific Keyboard keystroke and stop system propagation

I have an application where I have multiple keyboards connected through USB.
I need to hook a specific keyboard to get the keypress directly into the software, even if this one is not on the foreground. This so far works based on this project. The other keyboards shall work as normal.
Although it seems by using Direct Input, it is not possible to stop propagation (we don't want other apps to get that particular keyboard input).
Concerning Global Hook, it can block the keypress system-wise, but it is impossible, as far as I know, to identify the source of the keystroke (which keyboard it is from) and thus to selectively block them.
There is another project, here, that combines the two, but it is quite messy and heavy.
Is there a better way to achieve this? I am surprised that simple task is so complex.
If you are going to make the app windows only, you should look into Windows raw input api
It isn't that complicated.

Controlling current media player in Windows without sending media key keystrokes?

I have a keyboard with media keys on it, the usual stuff, play/pause, stop, previous, next, as well as volume control.
I would like to mimick this type of behavior, only from my own program. Meaning that if Spotify is running, playing a song, and my program says "Pause" I would like Spotify to pause. All this without me hardcoding support for Spotify.
I know that I can use something like "SendKeys" to fake the user pushing the corresponding key on the keyboard, I've seen AutoHotKeys scripts for this.
But what is the API functions that I would use directly from my program? Do they even exist?
An easy option would be to use the AutoHotkey dll. I've found amazing-andrew's repo AutoHotkey.Interop very useful for this.
Then you can execute ControlSend like so:
//grab a copy of the AutoHotkey singleton instance
var ahk = AutoHotkeyEngine.Instance;
//execute any raw ahk code
ahk.ExecRaw("ControlSend, , {Media_Play_Pause}, ahk_class SpotifyMainWindow"); // send directly to the window

How to send direct input to another program on windows?

If you have used GameRanger, you will better understand this question.
I want to send a direct input, like keystrokes and mouse clicks, to another program (given that I know its executable) on Windows.
So far, I've been using .NET's SendKeys class, according to this MSDN article.
The program that I'm targeting is another .NET application, but I was having a lot of trouble, as it opens a large number of windows, with random handles every time (and names). Thus, I couldn't figure out a way to reliably verify that the particular window that I wanted was open. My solution was relying on the user to have it open, and having focused the correct input control, then my application sending alt+tab and writing its thing with SendWait*.
It does work fine most of the time, but it's the very antithesis of robustness. GameRanger does a similar thing, but much better. How can I achieve the same reliability? Any method/library that can run on windows is ok, if it's in C#, the better.
*Sometimes, a SendWait call might get lost; I haven't forced the new implementation as instructed in the msdn article, I will try it if nothing better exists.

Listening to keyboard C# (WinForm)

I'm writing a program that I want to run when the user is "idle" (I.e. No input from the user - not moving the mouse, or typing anything). I have taken care of detecting if the mouse is moving (through storing the mouse position as a variable and comparing the current position to that variable at a later time), but what I can't figure out is how to detect keyboard input.
I know that if the user is typing in the form itself, I can use event listeners (KeyDown, KeyPressed, etc.), but I want this program to detect keyboard input anywhere on the computer, in any program.
Is there a way to accomplish this? All my research has yielded ways to check inside the form, but not globally.
Note: Not sure if this matters, but I don't care what the user is typing (what keys are being pressed). All I need to know is that there is keyboard activity.
Thanks in advance!
What you're looking for is considered a Global Keyboard Hook.
A sample with explanation and source:
here
Edit: In terms #FᴀʀʜᴀɴAɴᴀᴍ answer, it would be improper to grab using GetLastInputInfo as system calls (SendInput(), keybd_event(), etc) all will trigger it as well, so that doesn't mean the user is technically there.

Are actions on active window through c# possible?

I want to write some code, that can automatically do actions on active windows.
For example, if i want to press the ignore button once the window that pops if I insert a new DOK.
i thought of a code that once a specific (known) window pops, it presses a few TABs, and presses Enter.
Can I implement such a code? preferrable in C#.
I thought of several answers to your question because it isn't clear what you are asking. One of these topics might be an area for research:
If you want to press keys for the active window in an application other than the one you are running, you might read about elevated privileges, interprocess communication, and accessing the keyboard with Windows function calls. You can not normally do anything to other applications other than to cause Windows to send it various messages. Thought: how will you indicate when the press the keys if your application is not the active window?
If the active window is one within your application, then you will be better off in the long run by finding a solution that fits C#'s object-oriented model. If you want certain code to be run at times for certain windows (when one is active), you can share code by putting it into a common superclass and having your window classes inherit it. Looking at it this way will be instructive, but you may have to find a somewhat different solution, since it sounds like your window should be inheriting Window's Window class (you can only have one parent class in C#). You could look up "class inheritance" and perhaps "interfaces."
Another solution that isn't so object-oriented for sharing code is to put it into a static function and call this function as needed in the code of your active windows. But ideally, it is code in the active window class that should be manipulating the window. You can put it into a function and then call the function from anywhere in the application that has the "this" reference for the active window.

Categories

Resources