473,395 Members | 2,151 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Win32 API WriteConsoleInput and key_event_record

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_RECORD
structure used inside of the WriteConsoleInput 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 wVirtualScanCode?

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.ASCIIEncoding encoder = new
ASCIIEncoding();
Byte[] bytes = encoder.GetBytes(text);

for (int i=0;i<length;i++)
{
KEY_EVENT_RECORD rec = new
KEY_EVENT_RECORD();
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
dwControlKeyState if no control key is pressed
//rec.dwControlKeyState =
ControlKeyState.????;
rec.uChar.AsciiChar = bytes[i];
rec.wRepeatCount = 1;
//how do I get the value of the
wVirtualKeyCode
//rec.wVirtualKeyCode = ????;
//how do I get the value of the
wVirtualScanCode
//rec.wVirtualScanCode = ????;
buffer[i].EventType =
InputEventType.KeyEvent;
buffer[i].Event = union;
}
WriteConsoleInput(
hStdin,
buffer[0],
length,
eventsWritten);

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

[StructLayout(LayoutKind.Sequential)]
public struct INPUT_RECORD
{
public InputEventType EventType;
public EVENT_UNION Event;
}

[StructLayout(LayoutKind.Sequential)]
public struct KEY_EVENT_RECORD
{
public bool bKeyDown;
public short wRepeatCount;
public short wVirtualKeyCode;
public short wVirtualScanCode;
public CHAR uChar;
public ControlKeyState dwControlKeyState;
}

[StructLayout(LayoutKind.Explicit)]
public struct EVENT_UNION
{
[FieldOffset(0)] public KEY_EVENT_RECORD
KeyEvent;
[FieldOffset(0)] public
MOUSE_EVENT_RECORD MouseEvent;
[FieldOffset(0)] public
WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
[FieldOffset(0)] public MENU_EVENT_RECORD
MenuEvent;
[FieldOffset(0)] public
FOCUS_EVENT_RECORD FocusEvent;
}

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

[Flags()]
public enum ControlKeyState
{
None = 0x0000,
RightAltPressed = 0x0001,
LeftAltPressed = 0x0002,
RightCtrlPressed = 0x0004,
LeftCtrlPressed = 0x0008,
ShiftPressed = 0x0010,
NumLockOn = 0x0020,
ScrollLockOn = 0x0040,
CapsLockOn = 0x0080,
EnhancedKey = 0x0100,
}
[DllImport("kernel32", CharSet=CharSet.Auto,
SetLastError=true)]
public static extern bool WriteConsoleInput (
IntPtr hConsoleInput,
[Out] INPUT_RECORD[] lpBuffer,
int nLength,
out int lpNumberOfEventsWritten);

[DllImport("kernel32", SetLastError=true)]
public static extern IntPtr GetStdHandle
(StandardHandle nStdHandle);

Thanks for all your help,

Doug
Nov 15 '05 #1
0 2992

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

Similar topics

4
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...
1
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......
2
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....
5
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...
8
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...
10
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....
0
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...
3
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 =...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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,...

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.