Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than kernel32, prefix the name with the module name and a period.
WriteConsoleInput (kernel32)
.
C# Signature:
/* Writes data directly to the console input buffer. */
[DllImport("kernel32.dll", EntryPoint = "WriteConsoleInputW", CharSet = CharSet.Unicode, SetLastError = true)]
internal static extern bool WriteConsoleInput(
IntPtr hConsoleInput,
INPUT_RECORD[] lpBuffer,
uint nLength,
out uint lpNumberOfEventsWritten);
// Code analysis will bring errors, but things here work
// This is because i have the flag definitions inside the structs but ignore them through field offsets
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct INPUT_RECORD
{
public const ushort KEY_EVENT = 0x0001,
MOUSE_EVENT = 0x0002,
WINDOW_BUFFER_SIZE_EVENT = 0x0004;
[FieldOffset(0)]
public ushort EventType;
Notes:
The only practical usage for this i've found is for debugging code reliant on ReadConsoleInput.
Please add if you've found something else that's useful
// These are a union
[FieldOffset(4)]
public KEY_EVENT_RECORD KeyEvent;
Tips & Tricks:
Please add some!
[FieldOffset(4)]
public MOUSE_EVENT_RECORD MouseEvent;
Sample Code:
[DllImport("kernel32")]
public static extern IntPtr GetStdHandle(StdHandle index);
[FieldOffset(4)]
public WINDOW_BUFFER_SIZE_RECORD WindowBufferSizeEvent;
public enum StdHandle
{
OutputHandle = -11,
InputHandle = -10,
ErrorHandle = -12
}
/*
MENU_EVENT_RECORD MenuEvent;
FOCUS_EVENT_RECORD FocusEvent; */
// MSDN claims that these are used internally and shouldn't be used
// https://docs.microsoft.com/en-us/windows/console/input-record-str
static void Main(string[] args)
{
INPUT_RECORD[] record = new INPUT_RECORD[1];
}
record[0].EventType = INPUT_RECORD.KEY_EVENT;
[StructLayout(LayoutKind.Sequential)]
public struct COORD
{
public short X;
public short Y;
record[0].KeyEvent = new KEY_EVENT_RECORD();
record[0].KeyEvent.UnicodeChar = 'a';
record[0].KeyEvent.AsciiChar = (byte)'a';
record[0].KeyEvent.bKeyDown = true;
public COORD(short x, short y)
{
X = x;
Y = y;
}
}
uint recordsWritten = 0;
bool boi = WriteConsoleInput(GetStdHandle(StdHandle.InputHandle), record, 1, out recordsWritten);
}
[StructLayout(LayoutKind.Explicit)]
public struct MOUSE_EVENT_RECORD
{
[FieldOffset(0)]
public COORD dwMousePosition;
Alternative Managed API:
Do you know one? Please contribute it!
public const int DOUBLE_CLICK = 0x0002,
MOUSE_HWHEELED = 0x0008,
MOUSE_MOVED = 0x0001,
MOUSE_WHEELED = 0x0004;
[FieldOffset(4)]
public uint dwButtonState;
public const int FROM_LEFT_1ST_BUTTON_PRESSED = 0x0001,
FROM_LEFT_2ND_BUTTON_PRESSED = 0x0004,
FROM_LEFT_3RD_BUTTON_PRESSED = 0x0008,
FROM_LEFT_4TH_BUTTON_PRESSED = 0x0010,
RIGHTMOST_BUTTON_PRESSED = 0x0002;
[FieldOffset(12)]
public uint dwEventFlags;
}
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct KEY_EVENT_RECORD
{
[FieldOffset(0)]
public bool bKeyDown;
[FieldOffset(4)]
public ushort wRepeatCount;
[FieldOffset(6)]
public ushort wVirtualKeyCode;
[FieldOffset(8)]
public ushort wVirtualScanCode;
[FieldOffset(10)]
public char UnicodeChar;
[FieldOffset(10)]
public byte AsciiChar;
Used to report input events in the console input buffer
8/18/2019 11:44:04 AM - -177.51.184.79
The ReadConsoleInput API
6/27/2021 12:38:02 PM - -79.206.152.209
The ReadConsoleInput API
6/27/2021 12:38:02 PM - -79.206.152.209
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).