472,958 Members | 2,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 7841
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jason Kyle Baginski | last post by:
Here's a little test app to demonstrate a problem I'm having. It creates four buttons, each one with the different FlatStyle types available. Three of them behave exactly the same way(and the...
1
by: Mullin Yu | last post by:
I want to create a C# GUI application that will start a new console application, e.g. console.exe when clicking a button. Then, if click a button, it will kill a specific console application. Am...
1
by: Adrian | last post by:
hi I'm attempting to build an app then sends a request to a URL and reads the response it works fine on my test site but when I connect to a real system I get the text below, I guess its...
4
by: GrandpaB | last post by:
Hi, I recently had a post about how to block Mousewheel events. The answer was to implement an IMessageFilter. Sadly, I must report that after 24 hours of researching my library and online...
0
by: Nickneem | last post by:
I' m trying to disable all right mouse clicks by using the vbAccelerator Windows Hooks Library The small (systray / console) app. must catch all (right) mouseclicks before they are received by...
3
by: forest demon | last post by:
for example, let's say I do something like, System.Diagnostics.Process.Start("notepad.exe","sample.txt"); if the user does a SaveAs (in notepad), how can i capture the path that the user...
9
by: garyusenet | last post by:
I am using a C# solution that somebody kindly sent me. It was a console application. I am trying to get use of the MessageBox. I have added the following using directive to the namespaces at...
8
by: John | last post by:
Hi, gurus, How can I implement the following feature in C#: Set objGroup = GetObject("WinNT://" & strComputer & "/" & strGroup & ", group") For Each objMember In objGroup.Members...
1
by: scudsong | last post by:
I override ProcessCmdKey() in my MDI parent form class and have some keyboard shortcut calling method in same class. But I wish to make these hotkeys working in parent/child form and other form. The...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.