I have a problem, I can't emulate pressing the space bar. I need the CS GO game to emulate pressing the space bar. So that the character in the game jumps.
Here's an example mirroring Simulating Key Press C#:
static class Program
{
const UInt32 WM_KEYDOWN = 0x0100;
const int VK_SPACE = 0x20;
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);
[STAThread]
static void Main()
{
while(true)
{
Process [] processes = Process.GetProcessesByName("iexplore");
foreach(Process proc in processes)
PostMessage(proc.MainWindowHandle, WM_KEYDOWN, VK_SPACE, 0);
Thread.Sleep(5000);
}
}
}
Related
When I use Process.Start(Path); Sometimes the program does not appear in the foreground, but it does appear in the taskbar To solve this problem, I must use the "AutoItX" reference to show the program in the foreground using GetForegroundWindow(), But how can I get GetForegroundWindow using Path? ("C:/Users/.../name_program/")
Update
my question is how can I get GetForegroundWindow From Path.
I appreciate any help, thank you
I can't test it, but it should work:
private const int ALT = 0xA4;
private const int EXTENDEDKEY = 0x1;
private const int KEYUP = 0x2;
private const int SHOW_MAXIMIZED = 3;
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
public static void ActivateWindow(IntPtr mainWindowHandle)
{
// Guard: check if window already has focus.
if (mainWindowHandle == GetForegroundWindow()) return;
// Show window maximized.
ShowWindow(mainWindowHandle, SHOW_MAXIMIZED);
// Simulate an "ALT" key press.
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | 0, 0);
// Simulate an "ALT" key release.
keybd_event((byte)ALT, 0x45, EXTENDEDKEY | KEYUP, 0);
// Show window in forground.
SetForegroundWindow(mainWindowHandle);
}
With this you can create the process and then activate it:
var proc = Process.Start(path);
proc.WaitForInputIdle();
ActivateWindow(proc.MainWindowHandle);
Recently I tried to create a bot for an MMO-Game (called Florensia).
It should click on several positions in the game.
My problem is that it only sets the cursour to the position but the click doesn't work out. If I try it at my desktop or some other programs, it clicks correctly.
The game of course is in windowed mode and I already tried to set delays between the Mouseup and Mousedown.
Also to set the game to foreground window before the click didn't work.
Looking forward to any answers! :)
Try to use WinApi functions like in this example.
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
const int MK_LBUTTON = 0x0001;
uint x = 100;
uint y = 100;
IntPtr handle = new IntPtr(0x00090996); //Your window handle
SetForegroundWindow(handle);
Thread.Sleep(300);
SendMessage(handle, WM_LBUTTONDOWN, new IntPtr(MK_LBUTTON), new IntPtr(y << 16 | x));
Thread.Sleep(300);
SendMessage(handle, WM_LBUTTONUP, new IntPtr(0), new IntPtr(y << 16 | x));
}
How can I get the Window Title that the user currently have focus on?
I'm making a program that runs with another Window, and if the user does not have focus on that window I find no reason for my program to keep updating.
So how can I determine what window the user have focus on?
I did try to look into
[DllImport("user32.dll")]
static extern IntPtr GetActiveWindow();
but I seems I can only use that if the Window is part of my application which is it not.
Check this code:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
Use GetForegroundWindow to retrieve the handle of the focused window and GetWindowText to get the window title.
[ DllImport("user32.dll") ]
static extern int GetForegroundWindow();
[ DllImport("user32.dll") ]
static extern int GetWindowText(int hWnd, StringBuilder text, int count);
static void Main() {
StringBuilder builder = new StringBuilder(255) ;
GetWindowText(GetForegroundWindow(), builder, 255) ;
Console.WriteLine(builder) ;
}
I tried it with C++ and it seems like one needs to inject code into another process to disable its main window's close button. But I'm curious, since I actually need it for a .NET program, can I gray out its main window's close button with C#?
PS. Again the process that I'm doing it for is not mine. I can find it as such:
Process[] processes = Process.GetProcessesByName("Notepad");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
//Now how to disable it's close button?
}
I think I got it:
[DllImport("user32.dll")]
static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("user32.dll")]
static extern bool DeleteMenu(IntPtr hMenu, uint uPosition, uint uFlags);
const uint SC_CLOSE = 0xF060;
const uint MF_BYCOMMAND = 0x00000000;
Process[] processes = Process.GetProcessesByName("Notepad");
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
IntPtr nSysMenu = GetSystemMenu(pFoundWindow, false);
if (nSysMenu != IntPtr.Zero)
{
if (DeleteMenu(nSysMenu, SC_CLOSE, MF_BYCOMMAND))
{
//Done!
}
}
}
I am writing a Console application using C#, in which I am firing Print jobs through code. My issue is I do not know how much time I should wait for Print Dialog after sending commands to Print. For the time being I am using Thread sleep of 1000 milliseconds.
//Sending Commands to Print(to press "Ctrl+P" button).
SendKeys.SendWait("^(p)");
Thread.Sleep(1000);
//Sending Commands to Print(to press "Enter" Button).
SendKeys.SendWait("{ENTER}");
Can anybody help me to fix this issue please. Any help will be appreciated.
Thanks in advance
Update:
Here is my whole code:
//Launch the file from the location specified in.
White.Core.Application application =White.Core.Application.Launch(#path);
Console.WriteLine("launch is done");
Thread.Sleep(_delayOfPrint);
//Sending Commands to Print(to press "Ctrl+P" button).
SendKeys.SendWait("^(p)");
Thread.Sleep(1000);
//Sending Commands to Print(to press "Enter" Button).
SendKeys.SendWait("{ENTER}");
//Get the current time as the document fired for print job.
_printedTime = DateTime.Now;
Thread.Sleep(1500);
//Closing the window.
SendKeys.SendWait("%{F4}");
I would have a maximum wait of a few seconds, but during that time I would periodically use the Win32 function FindWindowEx to see if the print dialog actually came up, and if so proceed. You can get a process' main window with code like this:
Process[] processes = Process.GetProcessesByName(appName);
foreach (Process p in processes)
{
IntPtr pFoundWindow = p.MainWindowHandle;
}
You can then pass the found main window handle to FindWindowEx to peruse the child windows to check for the print dialog. FindWindowEx has PInvoke signatures like this:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
Edit 2: Since the OP seems to be demanding I give a perfectly working function, I wrote a general one that does the trick and tested it. Here it is, working general code to wait for any child window. Call as WaitForChildWindow("myApp", "Print", 5000) for this case:
/// <summary>
/// Wait for a child window of an application to appear
/// </summary>
/// <param name="appName">Application name to check (will check all instances)</param>
/// <param name="childWindowName">Name of child window to look for (titlebar)</param>
/// <param name="timeout">Maximum time, in milliseconds, to wait</param>
/// <returns>True if the window was found; false if it wasn't.</returns>
public static bool WaitForChildWindow(string appName, string childWindowName, int timeout)
{
int sleepTime = timeout;
while (sleepTime > 0)
{
Process[] processes = Process.GetProcessesByName(appName);
foreach (Process p in processes)
{
IntPtr pMainWindow = p.MainWindowHandle;
IntPtr pFoundWindow = FindWindowEx(pMainWindow, IntPtr.Zero, null, childWindowName);
if (pFoundWindow != IntPtr.Zero)
return true;
}
Thread.Sleep(100);
sleepTime -= 100;
}
// Timed out!
return false;
}
Edit 3: Here's another way of doing it for merely owned windows that don't have a child relationship:
[DllImport("user32.dll", SetLastError = true)]
static extern uint GetWindowThreadProcessId(IntPtr hwnd, IntPtr processId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumThreadWindows(uint dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32", SetLastError = true, CharSet = CharSet.Auto)]
private extern static int GetWindowText(IntPtr hWnd, StringBuilder text, int maxCount);
public delegate bool EnumThreadDelegate(IntPtr hwnd, IntPtr lParam);
static bool EnumThreadCallback(IntPtr hWnd, IntPtr lParam)
{
StringBuilder text = new StringBuilder(500);
GetWindowText(hWnd, text, 500);
if (text.ToString() == "Print")
return false;
return true;
}
public static bool FindThreadPrintWindow(uint threadId)
{
return !EnumThreadWindows(threadId, EnumThreadCallback, IntPtr.Zero);
}
public static bool WaitForOwnedPrintWindow(string appName, int timeout)
{
int sleepTime = timeout;
while (sleepTime > 0)
{
Process[] processes = Process.GetProcessesByName(appName);
foreach (Process p in processes)
{
IntPtr pMainWindow = p.MainWindowHandle;
uint threadId = GetWindowThreadProcessId(pMainWindow, IntPtr.Zero);
if (FindThreadPrintWindow(threadId))
return true;
}
Thread.Sleep(100);
sleepTime -= 100;
}
// Timed out!
return false;
}