hi
i need to call enter key automatically from my program
i want , when a savedialogbox is opened in windows(all savedialogbox!), then it save automatically without need to click or press by user.
how can i do it?
thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Zoran HorvatPosted Aug 3, 2011, 5:19 AM
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
if (hwnd != IntPtr.Zero)
{
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
}
Without this check you're sending ENTER key to currently active window, which is wrong.
Zoran
Sam HobbsPosted Aug 3, 2011, 3:14 PM
It is not clear to me if this is for the same process or a different process. If it is the same process and you have the code for program, then I don't understand why you are showing a save file dialog when you don't need it.
Bahar JalaliPosted Aug 3, 2011, 6:24 AM
i found that myself and wort like this:
///////////
private void timer1_Tick(object sender, EventArgs e)
{
string windowHandle = "Save As";
// Process[] word = Process.GetProcessesByName(programname);
IntPtr hwnd = (IntPtr)FindWindow("#32770", windowHandle);
IntPtr val = IntPtr.Zero;
if (hwnd != val)
{
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
timer1.Stop();
timer1.Start();
}
}
///////////////////////////////////////
ant it's like your code
Bahar JalaliPosted Aug 3, 2011, 4:48 AM
because the command
and it execute this command on all programs.
i thinks when i put the code in a timer thick event, it can't detect "Save Az" window ! but i want when it detect a save as window then enters it.
///////////////////////////////////////
private void timer1_Tick(object sender, EventArgs e)
{
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
label1.Text = hwnd.ToString();
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
}
//////////////////////////////////////
and it seems i need to a if command for check it
but i don't know how can i check it?!
and what about "static" in DLLs on the top of my code? is it possible my problem is maked for it?
any idea?!
Zoran HorvatPosted Aug 3, 2011, 4:28 AM
Zoran
Bahar JalaliPosted Aug 3, 2011, 4:22 AM
i put the code in form1_shown and form1_changevisible but dosn't work again
i put a timer and it fall in a EXTREME loop
could you test it ?
thanks
Zoran HorvatPosted Aug 3, 2011, 4:03 AM
If you want the code to be executed every time the form is shown, then implement it in the Shown event handler.
Zoran
Bahar JalaliPosted Aug 3, 2011, 3:31 AM
i find it and it works
it's the code
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
i put this code in form_load()
like this:
///////////////////////////////////////////////////////////
// Get a handle to an application window.
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string windowHandle = "Save As";
IntPtr hwnd = (IntPtr)FindWindow(null, windowHandle);
SetForegroundWindow(hwnd);
SendKeys.Send("{ENTER}");
}
}
}
//////////////////////////////////////////////////////////
now it work but it enter the first save dialog box on windows
and when i open a savedialog box again, it dosn't work
i try use if the form is already open but it dosn't work again.
please help me .
i want while my program is running , it enters all save dialog boxes.
thanks