Moving the mouse in other application on C# - c#

I'm trying to make a program that basically executes a mouse macro on other windows/apps in a given interval of time.
I've already managed create the timer, mapping and saving the 3 positions on screen that I need (using [DllImport("User32.Dll")] and GetCursorPos/SetCursorPos/mouse_event to do the left click) and even manage to do the mouse move on screen using LinearSmoothMove.
The problem is when I execute the function the mouse moves until it reaches the side of the window of this other app and then it "stops" (actually it looks like it's moving under the window). However, it works with other things like opening Notepad and going between the lines.

Please, see answer to similar question here. But before using ClickOnPointTool.ClickOnPoint() you should write the next code:
To find the certain process like this:
private IntPtr GetProcessMainWindowHandle(string mainWindowTitle)
{
var processes = Process.GetProcesses();
var foundProcess = processes.Single(p => mainWindowTitle.Equals(p.MainWindowTitle, StringComparison.CurrentCulture));
// Also you can use method Process.GetProcessesByName(), it depends on your business logic
return foundProcess.MainWindowHandle;
}
And to activate the found window using P/Invoke call of method SetForegroundWindow() with value of variable mainWindowHandle, recieved from GetProcessMainWindowHandle():
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);

Related

C# Detect Keystrokes and Send to Background Windows

I've looked around for six hours today in search of a method to complete the task I'm looking to accomplish. However with little luck every method I've tried has come out not working.
So the program I'm working on is a multiboxing application for video games. Essentially I want to have my created application running in the background. The user will check on checkbox's to state which keys they want to be captured, so not every key is being captured. Then while they are playing the main game, the application will send the keys that are checked to the games running in the background.
I've tried global hotkeys however never could get more than one key working. I've also tried to hook keys but for some reason, couldn't get that functional. I also dabbled into sendmessage with little luck there either.
Was just curious if anyone else had some ideas for going about doing this. To give an example of another program that does this same thing would be HotKeyNet, KeyClone, and ISboxer. I know there are more out there but that gives you an idea of what I'm trying to do with my application.
Alright, after quite a bit of research into different methods for sending keystrokes and reading keystrokes. I finally was able to splice to different types of coding together to provide the results I was looking for.
I'm posting up the answer, so anyone that is looking for the answer to this question later down the road has it available to them.
My two references for splicing this code together are the following:
http://www.codeproject.com/Articles/19004/A-Simple-C-Global-Low-Level-Keyboard-Hook
Send combination of keystrokes to background window
I used the global low level hook and postmessage for sending keystrokes to the background application.
So first you will need to follow the instructions from the first
link, to get the starting code working.
Download the working source code from link one, and use the
globalKeyboardHook.cs in your application.
Then in references place the following:
using System.Runtime.InteropServices; //Grabs for your DLLs
using Utilities; //Grabs from the file you added to your application.
Now you will want to place the following code in your class:
globalKeyboardHook gkh = new globalKeyboardHook();
[DllImport("user32.dll")] //Used for sending keystrokes to new window.
public static extern IntPtr PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)] //Used to find the window to send keystrokes to.
public static extern IntPtr FindWindow(string className, string windowName);
Now go ahead place your keystrokes you want to be grabbed this I
found is best in Form1_Loaded:
gkh.HookedKeys.Add(Keys.A);//This collects the A Key.
gkh.HookedKeys.Add(Keys.B);//This collects the B Key.
gkh.HookedKeys.Add(Keys.C);//This collects the C Key.
gkh.KeyDown += new KeyEventHandler(gkh_KeyDown); //Event for pressing the key down.
gkh.KeyUp += new KeyEventHandler(gkh_KeyUp); //Event for the release of key.
After that you will want to go ahead and place in the following in
your code as well:
void gkh_KeyUp(object sender, KeyEventArgs e) //What happens on key release.
{
lstLog.Items.Add("Up\t" + e.KeyCode.ToString());
e.Handled = false; //Setting this to true will cause the global hotkeys to block all outgoing keystrokes.
}
void gkh_KeyDown(object sender, KeyEventArgs e) //What happens on key press.
{
lstLog.Items.Add("Down\t" + e.KeyCode.ToString());
e.Handled = false;
}
Once that is in place just put this little bit in the gkh_KeyDown to
get your keystrokes to send to another window of your choosing:
const uint WM_KEYDOWN = 0x100;
IntPtr hWnd = FindWindow(null, "Example1"); //Find window Example1 for application.
switch (e.KeyCode)
{
case Keys.A: //Makes it so it only sends that key when it's pressed and no other keys.
if(chkA.Checked == true)
{
PostMessage(hWnd, WM_KEYDOWN, (IntPtr)(Keys.A), IntPtr.Zero); //Sends to key A to new window assigned hWnd which equals Example1.
}
break;
}
}
The code that I have provided is setup so people can use checkbox's to tell the program which keys they want to send over to the second application.
If you have any questions regarding to this post just let me know, and I will do my best to walk you through the process. Hope this helps someone out later down the road.
My suggestion is to go with "mapped memory" (in operating system concepts: shared memory)
First process creates (may be your check state program) creates a mapped memory and writes values to it.
All other game process reads the value from memory map created by first process.
Here is a very nice example regarding how to do it.
https://msdn.microsoft.com/en-us/library/dd267552(v=vs.110).aspx

C#: Put an exe file on the foreground of the form [duplicate]

I am searching for alternatives to the old User32.dll version of switching to a different application with FindWindow() and SetForegroundWindow().
I did find an alternative to the first with the usage of Process.GetProcessesByName() but I do not see the corresponding method to switch (set active/foreground) to that application.
Is there a way of doing that without using the old way with the User32.dll?
Thank you for your help.
EDIT
I accepted the answer of #Sorceri although it is not the answer I was looking for.
Answer: No.
But, to help the next wonderer looking to find a window and activate it from C# here's what you have to do:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
void ActivateApp(string processName)
{
Process[] p = Process.GetProcessesByName(processName);
// Activate the first application we find with this name
if (p.Count() > 0)
SetForegroundWindow(p[0].MainWindowHandle);
}
To bring notepad to the front, for example, you would call:
ActivateApp("notepad");
As a side note - for those of you who are trying to bring a window within your application to the foreground just call the Activate() method.
You could use SetActiveWindow as an alternative to SetForeGroundWindow. I'd say you should go through all the Windows Manipulation Api Functions and see if there's something you're missing out.
Also, note that you can obtain the handle of the System.Diagnostics.Process object via the Process.Handle property.
An alternative to SetForeGroundWindow is VisualBasic's AppActivate
Call it like this
Microsoft.VisualBasic.Interaction.AppActivate("WindowTitle")
Just because it is in the VisualBasic namespace doesn't mean you can't use it in C#.
Full Documentation here
You can use System.Diagnostics.Process Object for a FindWindow equivalent. There currently is no equivalent for SetForegroundWindow. You will want use Pinvoke with SetForgroundWindow.
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);

Send Windows key keypress into application [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect the currently pressed key?
(EDIT: For what it's worth, this is not a duplicate... not sure why it was voted as such)
I have a small timer application that we use in the office to track the hours spent on a project. There is a start and stop button, and a project and task fields. When we go on break and to lunch and other things, we stop the timer for that project, then restart it. This is a repetitive task that generally involves digging the window out from behind several other Windows, then the same after the break.
What I want to do is assign WindowKey+W to the work timer application and have it not only bring the timer application to the front and focus it, but also have it toggle the Start/Stop.
I have tried a number of searches, but I can't seem to narrow down the examples to what I want. I do know that you can open the properties of a Windows shortcut and assign a shortcut key to launch a program, and (I guess?) if you have that app already open and it is set to allow only one instance of the program that it will bring that program to the front??? maybe..
Anyway.. but that method will not accept the WindowsKey as a valid key combo. And I don't know if it can somehow pass that key combo in to the program.
I appreciate any help or direction here!!
EDIT - Answer Update
Thank you #huadianz for your answer! I converted your code to VB:
Public Const MOD_WIN As Integer = &H8
Public Const KEY_W As Integer = &H57
<DllImport("user32.dll")> _
Public Shared Function RegisterHotKey(hWnd As IntPtr, id As Integer, fsModifiers As Integer, vlc As Integer) As Boolean
End Function
<DllImport("user32.dll")> _
Public Shared Function UnregisterHotKey(hWnd As IntPtr, id As Integer) As Boolean
End Function
Public Sub RegisterKeys()
RegisterHotKey(Me.Handle, 1, MOD_WIN, KEY_W)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
If (m.Msg = &H312) Then
Me.TopMost = True
Me.PlayPauseTimer()
Me.TopMost = False
End If
End Sub
On an interesting note, Me.BringToFront() would not actually bring the application to the front in this scenario in Win7, nor did Me.Focus(). However, Me.TopMost = True worked, but it has the secondary effect of making the window always on top. Setting it to True, toggling the timer, then setting it back to False works great!
If you want full operating system intergration, you can hook into the kernel input functions by using PInvoke.
What you are looking for is the explorer.exe Windows API function, described in detail here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309%28v=vs.85%29.aspx
Using PInvoke, you can invoke this C++ function
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public const int MOD_WIN = 0x00000008;
public const int KEY_W = 0x00000057
public static void RegisterKeys()
{
RegisterHotKey((IntPtr)this.Handle, 1, MOD_WIN, KEY_W);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
this.Visible = !this.Visible;
}
I would also suggest looking at Auto Hotkey: http://www.autohotkey.com/. If all you're after is sending a window to front on key-press then you may find it faster to learn AHK and write a script to do so (be only 1 or 2 lines) than writing something in C# or VB.
*edit*As regards pressing a button automatically, if it's set as the default button then you can just send the enter keystroke to it. Otherwise you may need to send a mouse click even to the window. Neither is particularly difficult once you have the first part done.
#W::MsgBox "This is a message box. You just need to use something like send to send keystrokes and other commands to the window you want to control, instead of the this message box."
You can use Windows SendKeys. If you look at that link it shows the codes and syntax for applying a ctrl, alt or shift key. I imagine you can send these keys to your application to simulate a shortcut press.

Can't simulate action properly. SendMessage in c# (spy++)

I've got a problem with simulating in game's process this action:
- mouse_down
- mouse_move
- mouse_up
When I do it manually, that's the spy++ output:
mouse_down:
mouse_move:
mouse_up:
So I've tried to simulate that with below code start x,y: (618,392) final x,y: (618,432) but It's not working.
uint Iparm = makeDWord((ushort)x, (ushort)y);
IntPtr xd = new IntPtr(Iparm);
uint Iparm2 = makeDWord((ushort)x2, (ushort)y2);
IntPtr xd2 = new IntPtr(Iparm2);
SendMessage(UltraBot.p, (uint)0x201, (IntPtr)0x1, xd); // down button (start x,y)
SendMessage(UltraBot.p, (uint)0x200, (IntPtr)0x1, xd2); // move (final x,y)
SendMessage(UltraBot.p, (uint)0x202, (IntPtr)0x0, xd2); // up button (final x,y)
Here's the spy++ output after using the code:
I don't really know why it's not working. I've been using this way to simulate keys for some time, actions like: ctrl+q, mouse clicks and so werent any problem. What's more, IT WORKED FOR ME ONCE, but just once. I've stuck in here. Thanks for any help :)
You cannot simulate mouse input by sending mouse messages (such as WM_LBUTTONDOWN and WM_MOUSEMOVE) using the SendMessage function.
Yes, that's what it looks like is happening when you monitor the messages with Spy++, but there's a lot more going on behind the scenes.
To simulate mouse or keyboard input properly, you need to use the SendInput function. The P/Invoke declaration to call it from C# looks like this:
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

Windows Console Application Getting Stuck (Needs Key Press) [duplicate]

This question already has answers here:
How and why does QuickEdit mode in Command Prompt freeze applications?
(2 answers)
Closed 6 years ago.
I have a console program that has different components that run like this:
void start() {
while(true){
DoSomething();
Thread.Sleep(1000*5);
}
}
My main entry point looks like [pseudo-ish code]
Thread.Start(Componenet1.Start);
Thread.Start(Componenet2.Start);
while(true){
Console.Writeline("running");
Thread.Sleep(1000*5);
}
There are no Console.Reads anywhere. My problem is SOMETIMES the application will be running great but then stop and if I press any key on the window it will start working again. This happens fairly infrequently but I have this program deployed on 100+ VM's running 24/7 in an automated environment.
Also on the computer I have some AHK scripts and other stuff that manipulate the mouse but not sure if that has anything to do with it.
Also note that sometimes the CPU can really be running at 100% on the machines so maybe thread priority is an issue?
SOLUTION: You need to disable quick edit mode. Here is working C# code to do this:
// http://msdn.microsoft.com/en-us/library/ms686033(VS.85).aspx
[DllImport("kernel32.dll")]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
private const uint ENABLE_EXTENDED_FLAGS = 0x0080;
static void Main(string[] args)
{
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
SetConsoleMode(handle, ENABLE_EXTENDED_FLAGS);
If the user accidentally clicks into the black console window, the cursor changes to a filled white rectangle, and the app hangs at the next Console.Write statement, until another clic is made.
It is a generic feature of the Console window when its "QuickEdit Mode" is enabled.
In order to disable that feature, you should uncheck the "QuickEdit Mode" option of your app's console window at run-time.

Categories