المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Send keyboard keys in c# [modified]



C# Programming
12-04-2009, 08:12 AM
Hello,
I want to send some text or keys to other applications say "notepad,calc,etc.". I have tried but i failed. New window is opened but key is not displayed on the ********
Here is my code:

[DllImport("User32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hwnd);
[DllImport("User32.DLL")]
public static extern int SetActiveWindow(IntPtr hwnd);

newprocess.StartInfo.FileName = "calc.exe";
newprocess.StartInfo.UseShellExecute = false;
newprocess.StartInfo.RedirectStandardOutput = true;
newprocess.StartInfo.RedirectStandardInput = true;
newprocess.Start();
SetActiveWindow(newprocess.MainWindowHandle);
//SetForegroundWindow(newprocess.MainWindowHandle);

//write to calc
// Press the 1 key
keybd_event(0x61, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
//Release the 1 key
keybd_event(0x61, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

//// Press the + key
keybd_event(0x6B, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
////Release the + key
keybd_event(0x6B, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

//// Press the 1 key
keybd_event(0x61, 0x45, KEYEVENTF_EXTENDEDKEY, 0);
////Release the 1 key
keybd_event(0x61, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);

I have tried with many functions like SetForegroundWindow(), but there is no change. I have tried with notepad and used Sendkeys.Sendwait(), and its working fine. But help me for keybd_event().
Please help me its urgent.
Thank you in advance.

modified on Thursday, December 3, 2009 4:34 AM