473,794 Members | 2,804 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.e xe 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("user 32.dll", EntryPoint = "keybd_even t", CharSet =
CharSet.Auto, ExactSpelling = true)]
internal static extern void keybd_event(byt e vk, byte scan, int
flags, IntPtr extrainfo);
[DllImport("user 32.dll", SetLastError = true)]
public static extern int FindWindow(Stri ng lpClassName, String
lpWindowName);
[DllImport("user 32.dll", SetLastError = true)]
static extern int SetForegroundWi ndow(int hWnd);
[DllImport("user 32.dll", SetLastError = true)]
static extern int PostMessage(int hwnd, int wMsg, int wParam,
int lParam);
static void Main(string[] args)
{
int x = FindWindow("Clu nkyProgram", null);
SetForegroundWi ndow(x);
Thread.Sleep(20 0);
Send(System.Win dows.Forms.Keys .ControlKey, true);
Send(System.Win dows.Forms.Keys .Shift, true);
Send(System.Win dows.Forms.Keys .W, true);
Send(System.Win dows.Forms.Keys .W, false);
Send(System.Win dows.Forms.Keys .Shift, false);
Send(System.Win dows.Forms.Keys .ControlKey, false);

}
public static void Send(System.Win dows.Forms.Keys key, bool
down)
{
int KEYEVENTF_KEYUP = 0x0002;
keybd_event((by te)key, 0, (down ? 0 : KEYEVENTF_KEYUP ),
IntPtr.Zero);
}

Thanks,
Mo

Jan 23 '07 #1
4 8010
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.goog legroups.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.e xe 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("user 32.dll", EntryPoint = "keybd_even t", CharSet =
CharSet.Auto, ExactSpelling = true)]
internal static extern void keybd_event(byt e vk, byte scan, int
flags, IntPtr extrainfo);
[DllImport("user 32.dll", SetLastError = true)]
public static extern int FindWindow(Stri ng lpClassName, String
lpWindowName);
[DllImport("user 32.dll", SetLastError = true)]
static extern int SetForegroundWi ndow(int hWnd);
[DllImport("user 32.dll", SetLastError = true)]
static extern int PostMessage(int hwnd, int wMsg, int wParam,
int lParam);
static void Main(string[] args)
{
int x = FindWindow("Clu nkyProgram", null);
SetForegroundWi ndow(x);
Thread.Sleep(20 0);
Send(System.Win dows.Forms.Keys .ControlKey, true);
Send(System.Win dows.Forms.Keys .Shift, true);
Send(System.Win dows.Forms.Keys .W, true);
Send(System.Win dows.Forms.Keys .W, false);
Send(System.Win dows.Forms.Keys .Shift, false);
Send(System.Win dows.Forms.Keys .ControlKey, false);

}
public static void Send(System.Win dows.Forms.Keys key, bool
down)
{
int KEYEVENTF_KEYUP = 0x0002;
keybd_event((by te)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*****@hotmai l.comwrote in message
news:11******** **************@ d71g2000cwa.goo glegroups.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
5142
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 way I'd anticipate), but the FlatStyle.System one does not. If you click on any of the buttons, use the enter key, or the spacebar, they will bring up a messagebox that says "Activate". Except the FlatStyle.System one, which will bring up _two_...
1
5942
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 I right that I need to create a new thread when clicking a "Start Button" for starting a console application. And, a separating thread of "Stop Button" is used to stop a specific console application?? I tried to do the following, button the...
1
5260
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 responding to a request from the remote end if so how do I either tell it to ignore the request and just send the url or set what its looking for?
4
16790
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 resources I am still stumped! I did find a promising code snippet on MSDN, but have yet to successfully implement it. Here is the code snippet: Public Class MessageFilter
0
1671
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 the users application (for instance IE). I' m not getting passed the stage where I write to the console catching clicks on my applications form.. I really doubt if this one's possible at all (especially for an API noob like me)..
3
4035
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 selects? thanks...
9
13342
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 the start of the program : - using System; // I've added this...
8
3136
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 WScript.Echo vbCrLf & " Name: " & objMember.Name Next
1
4166
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 case now is when focus on the other form(regular form, not MDI), ProcessCmdKey() doesn't capture keyboard anymore. Which class should I put ProcessCmdKey() in and anything to make it work? Thanks! namespace myNamespace { public class...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6779
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.