473,793 Members | 2,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Win32 API WriteConsoleInp ut and key_event_recor d

I am trying to create a method called SendKeys that will
write a string to the console input using the Win32 API.
I have been wrestling with this for days and get more
confused with every level in the APIs.

Here are the questions I have about the KEV_EVENT_RECOR D
structure used inside of the WriteConsoleInp ut WIN32 API
call:
1. Do I need to send both a key down and a key up event?
2. What do I put for the ControlKeyState value if no
control key is pressed
3. How do I get the value of the wVirtualKeyCode ?
4. How do I get the value of the wVirtualScanCod e?

Here's what I have so far:

private static void SendKeys(string text)
{
IntPtr hStdin;
hStdin = GetStdHandle
(StandardHandle .Input);
int length = text.Length;
INPUT_RECORD[] buffer = new INPUT_RECORD
[length];
int eventsWritten = -1;
System.Text.ASC IIEncoding encoder = new
ASCIIEncoding() ;
Byte[] bytes = encoder.GetByte s(text);

for (int i=0;i<length;i+ +)
{
KEY_EVENT_RECOR D rec = new
KEY_EVENT_RECOR D();
EVENT_UNION union = new
EVENT_UNION();
union.KeyEvent = rec;
//Do I need to send both a key
down and a key up event?
rec.bKeyDown = true;
//What do I put for
dwControlKeySta te if no control key is pressed
//rec.dwControlKe yState =
ControlKeyState .????;
rec.uChar.Ascii Char = bytes[i];
rec.wRepeatCoun t = 1;
//how do I get the value of the
wVirtualKeyCode
//rec.wVirtualKey Code = ????;
//how do I get the value of the
wVirtualScanCod e
//rec.wVirtualSca nCode = ????;
buffer[i].EventType =
InputEventType. KeyEvent;
buffer[i].Event = union;
}
WriteConsoleInp ut(
hStdin,
buffer[0],
length,
eventsWritten);

}
public enum StandardHandle
{
Input = -10,
Output = -11,
Error = -12,
}

[StructLayout(La youtKind.Sequen tial)]
public struct INPUT_RECORD
{
public InputEventType EventType;
public EVENT_UNION Event;
}

[StructLayout(La youtKind.Sequen tial)]
public struct KEY_EVENT_RECOR D
{
public bool bKeyDown;
public short wRepeatCount;
public short wVirtualKeyCode ;
public short wVirtualScanCod e;
public CHAR uChar;
public ControlKeyState dwControlKeySta te;
}

[StructLayout(La youtKind.Explic it)]
public struct EVENT_UNION
{
[FieldOffset(0)] public KEY_EVENT_RECOR D
KeyEvent;
[FieldOffset(0)] public
MOUSE_EVENT_REC ORD MouseEvent;
[FieldOffset(0)] public
WINDOW_BUFFER_S IZE_RECORD WindowBufferSiz eEvent;
[FieldOffset(0)] public MENU_EVENT_RECO RD
MenuEvent;
[FieldOffset(0)] public
FOCUS_EVENT_REC ORD FocusEvent;
}

public enum InputEventType : short
{
KeyEvent = 0x0001,
MouseEvent = 0x0002,
WindowBufferSiz eEvent = 0x004,
MenuEvent = 0x0008,
FocusEvent = 0x0010,
}

[Flags()]
public enum ControlKeyState
{
None = 0x0000,
RightAltPressed = 0x0001,
LeftAltPressed = 0x0002,
RightCtrlPresse d = 0x0004,
LeftCtrlPressed = 0x0008,
ShiftPressed = 0x0010,
NumLockOn = 0x0020,
ScrollLockOn = 0x0040,
CapsLockOn = 0x0080,
EnhancedKey = 0x0100,
}
[DllImport("kern el32", CharSet=CharSet .Auto,
SetLastError=tr ue)]
public static extern bool WriteConsoleInp ut (
IntPtr hConsoleInput,
[Out] INPUT_RECORD[] lpBuffer,
int nLength,
out int lpNumberOfEvent sWritten);

[DllImport("kern el32", SetLastError=tr ue)]
public static extern IntPtr GetStdHandle
(StandardHandle nStdHandle);

Thanks for all your help,

Doug
Nov 15 '05 #1
0 3049

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
8467
by: Scaramouche | last post by:
is there an equivalent to unixs' ps command in perl? i was writing some win32 scripts and i needed a listing of specific processes. i could perhaps grep through a list of running processes but how can i attain that through perl? thanks
1
4976
by: RL | last post by:
Hi all, I am new and am lost with Win32::Process stuff. I want on perl script to start a process, then another perl script to terminate the same process. I can start a process using... ------------------------------------------------------- use Win32::Process;
2
4995
by: RL | last post by:
Hello Perl gurus, 1. I have a web page where I can push a button (dospawn.html). 2. This button calls a CGI script (spawnboss.cgi) 3. spawnboss.cgi calls a forking perl script (forkme.pl) 4. forkme.pl calls the process creation script (createme.pl) 5. createme.pl creates my notepad.exe process, but no window shows up on my PC. The result on my web browser is:
5
2218
by: Bryan Parkoff | last post by:
Please provide me the information when it is not in the correct newsgroups. I have been using C and C++ language for a long time when it is time for me to learn Win32 API functions that they can be written in C and C++ language. Please give me an advice where I should post to discuss this topic to the correct newsgroups. Also, where can I obtain the best textbook to learn and understand how Win32 API functions work in C / C++ language. ...
8
4772
by: Tony Johansson | last post by:
Hello! I just wonder what the difference is between a native win32-app and MFC. What I know is that you can use Win32 API in both cases. I think an MFC application is connected with a GUI which a win32 is not. You may correct me if I'm wrong on this point. //Tony
10
2499
by: Doug Perkes | last post by:
Does anyone have an example of declaring and using the WriteConsoleOutput Win32 API function? I'm having a little bit of trouble understanding how to translate all the structures to VB.NET. Specifically, I am trying to send plain text, the enter key, the F3 key, arrow keys, etc to a console window I have attached to. Thanks, Doug
0
2442
by: jbenezech | last post by:
Hi all , I have a perl/java app running under Win32. The application consists of a perl service (Win32::Daemon) and of java classes. The perl service calls every xx hours java classes to perform some action. The java call looks like this: Win32::SetChildShowWindow(0); Win32::Process::Create(
3
4105
by: somuchh8 | last post by:
Hi, I'm having a lot of trouble with the Win32::Spawn module in perl. Here is my situation, I have a Win32::Spawn call which looks like this: my $success = undef; my $cmdline = EBDTools::os_path("${nh_home}/web/aview/modules/svcrsp-ng/saSync.pl"); $success = Win32::Spawn($^X, "${^X} $cmdline ${optfile}", $pid); if (! $success) { my $lasterr = Win32::GetLastError(); return_configerror("Failed to create commit / sync process...
6
10055
by: Perl Beginner | last post by:
Good morning all, I had to reinstall Perl (ActivePerl 5.10.0 Build 1004) on my computer (Windows XP), the install went fine. however, I'm having issues loading Win32-GuiTest using the repository in PPM. the steps i took are: 1. saved Win32-GuiTest-1.50.5.zip from the search.cpan.org website 2. unzipped in the location c:/Perl/Win32-GuiTest 3. opened the PPM window and went to Edit-Preferences and clicked the repositories tab 4. i put...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10430
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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
9033
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...
0
6776
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
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2917
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.