M
Mo
Hi,
I am writing a console application to send a key sequence to an old
clunky application on a regular interval using the windows scheduler. I
can get it to work if it is a windows form application but not in a
console application. Two questions I have:
1) how can I get this code modified to use System.Windows.Forms.Keys
2) I like to pass a parameter to the console app to indicate which key
needs to be passed to the application. so:
C:/>mykeyboard.exe A
is going to send keyboard "A" to my other application. Any body knows
how to modify Keys.M to be Keys.args[0]?
here is the code I am using
[DllImport("user32.dll", EntryPoint = "keybd_event", CharSet =
CharSet.Auto, ExactSpelling = true)]
internal static extern void keybd_event(byte vk, byte scan, int
flags, IntPtr extrainfo);
[DllImport("user32.dll", SetLastError = true)]
public static extern int FindWindow(String lpClassName, String
lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern int SetForegroundWindow(int hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern int PostMessage(int hwnd, int wMsg, int wParam,
int lParam);
static void Main(string[] args)
{
int x = FindWindow("ClunkyProgram", null);
SetForegroundWindow(x);
Thread.Sleep(200);
Send(System.Windows.Forms.Keys.ControlKey, true);
Send(System.Windows.Forms.Keys.Shift, true);
Send(System.Windows.Forms.Keys.W, true);
Send(System.Windows.Forms.Keys.W, false);
Send(System.Windows.Forms.Keys.Shift, false);
Send(System.Windows.Forms.Keys.ControlKey, false);
}
public static void Send(System.Windows.Forms.Keys key, bool
down)
{
int KEYEVENTF_KEYUP = 0x0002;
keybd_event((byte)key, 0, (down ? 0 : KEYEVENTF_KEYUP),
IntPtr.Zero);
}
Thanks,
Mo
I am writing a console application to send a key sequence to an old
clunky application on a regular interval using the windows scheduler. I
can get it to work if it is a windows form application but not in a
console application. Two questions I have:
1) how can I get this code modified to use System.Windows.Forms.Keys
2) I like to pass a parameter to the console app to indicate which key
needs to be passed to the application. so:
C:/>mykeyboard.exe A
is going to send keyboard "A" to my other application. Any body knows
how to modify Keys.M to be Keys.args[0]?
here is the code I am using
[DllImport("user32.dll", EntryPoint = "keybd_event", CharSet =
CharSet.Auto, ExactSpelling = true)]
internal static extern void keybd_event(byte vk, byte scan, int
flags, IntPtr extrainfo);
[DllImport("user32.dll", SetLastError = true)]
public static extern int FindWindow(String lpClassName, String
lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern int SetForegroundWindow(int hWnd);
[DllImport("user32.dll", SetLastError = true)]
static extern int PostMessage(int hwnd, int wMsg, int wParam,
int lParam);
static void Main(string[] args)
{
int x = FindWindow("ClunkyProgram", null);
SetForegroundWindow(x);
Thread.Sleep(200);
Send(System.Windows.Forms.Keys.ControlKey, true);
Send(System.Windows.Forms.Keys.Shift, true);
Send(System.Windows.Forms.Keys.W, true);
Send(System.Windows.Forms.Keys.W, false);
Send(System.Windows.Forms.Keys.Shift, false);
Send(System.Windows.Forms.Keys.ControlKey, false);
}
public static void Send(System.Windows.Forms.Keys key, bool
down)
{
int KEYEVENTF_KEYUP = 0x0002;
keybd_event((byte)key, 0, (down ? 0 : KEYEVENTF_KEYUP),
IntPtr.Zero);
}
Thanks,
Mo