471,086 Members | 1,202 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,086 software developers and data experts.

how to implement System.Windows.Forms.keys in a console app

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

Jan 23 '07 #1
4 7620
Hi Mark,

Pseudo-code:

Check if there is an argument
Check argument not null or empty
Check argument length == 1
Parse the argument into a System.Char using char.Parse

At this point you can just cast the char to int, but if you really want to
use the Keys enumeration at runtime then just cast the char into the
System.Windows.Forms.Keys enumeration, but I don't see how that could be of
any value.

You could even take an entire string of characters like, "ADMFG" and loop
through each character in the string, performing the operation above and
sending each character to the other application as it's read from the
string.

--
Dave Sexton
http://davesexton.com/blog

"Mo" <le******@yahoo.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
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

Jan 23 '07 #2
Mo
Hi,
Thank you for your response. I have to fix the first problem first
which is that my console application does not recognise
System.Windows.Forms and I can not declare it. If I make this into a
Windows Forms application then this could work. How do you implement
System.Windows.Forms.Keys in Windows Console Application?

Mo

Jan 23 '07 #3
jme
You'll need to make a reference to it. Console applications do not
reference System.Windows.Forms by default. Right click the project,
click add reference, in the .NET tab select System.Windows.Forms, thats
if you are using Visual Studio 2005.
Mo wrote:
Hi,
Thank you for your response. I have to fix the first problem first
which is that my console application does not recognise
System.Windows.Forms and I can not declare it. If I make this into a
Windows Forms application then this could work. How do you implement
System.Windows.Forms.Keys in Windows Console Application?

Mo
Jan 23 '07 #4
Hi Mo,

IIRC, it's the same in previous versions too :)

--
Dave Sexton
http://davesexton.com/blog

"jme" <jm*****@hotmail.comwrote in message
news:11**********************@d71g2000cwa.googlegr oups.com...
You'll need to make a reference to it. Console applications do not
reference System.Windows.Forms by default. Right click the project,
click add reference, in the .NET tab select System.Windows.Forms, thats
if you are using Visual Studio 2005.
Mo wrote:
>Hi,
Thank you for your response. I have to fix the first problem first
which is that my console application does not recognise
System.Windows.Forms and I can not declare it. If I make this into a
Windows Forms application then this could work. How do you implement
System.Windows.Forms.Keys in Windows Console Application?

Mo

Jan 23 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jason Kyle Baginski | last post: by
1 post views Thread by Mullin Yu | last post: by
1 post views Thread by Adrian | last post: by
4 posts views Thread by GrandpaB | last post: by
8 posts views Thread by John | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.