"Chris Williams" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> nope, I'm trying to write a console app that captures non-character
> keypresses as part of the program.
>
You need to use native methods to get that level of control. The following
code demonstrates
getting the console's input handle, waiting on an input events and echoing
the scan-code to the
debug output.
static void Main(string[] args){
uint nRead = 0;
INPUT_RECORD iRecord = new INPUT_RECORD();
IntPtr stdIn = GetStdHandle(STD_INPUT_HANDLE);
AutoResetEvent are = new AutoResetEvent(false);
are.Handle = stdIn;
for(;

{
are.WaitOne();
System.Diagnostics.Debug.WriteLine("released");
int ret = ReadConsoleInput(stdIn, ref iRecord, 1, ref nRead);
for(int n=0; n!=nRead; ++n)
if(iRecord.EventType==KEY_EVENT){
ushort vk = iRecord.wVirtualScanCode;
if(vk==0x2A) System.Diagnostics.Debug.WriteLine("Left Shift");
else
System.Diagnostics.Debug.WriteLine(vk.ToString());
}
}
}
const uint STD_INPUT_HANDLE = 0xFFFFFFFF-9;
const ushort KEY_EVENT = 0x0001;
[DllImport("Kernel32")]
public static extern IntPtr GetStdHandle(uint nStdHandle);
[DllImport("Kernel32")]
public static extern int ReadConsoleInput(IntPtr hConsoleInput, ref
INPUT_RECORD lpBuffer, uint nLength, ref uint lpNumberOfEventsRead);
[StructLayout(LayoutKind.Sequential)]
public struct INPUT_RECORD
{
public ushort EventType;
public uint bKeyDown;
public ushort wRepeatCount;
public ushort wVirtualKeyCode;
public ushort wVirtualScanCode;
public char UnicodeChar;
public uint dwControlKeyState;
}