Win32 API WriteConsoleInput and key_event_record

D

Doug Perkes

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;
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.EventType =
InputEventType.KeyEvent;
buffer.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
 

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

Similar Threads


Top