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

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
 
D

Dave Sexton

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.
 
M

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
 
J

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top