console app problem

  • Thread starter Thread starter Chris Williams
  • Start date Start date
C

Chris Williams

Hi...

I'm trying to find some code samples that will show me how to capture
non-character keypresses in a console app. I've tried the standard stuff,
such as console.read and console.readline, and I've tried ConsoleEx which
does a great job, but still doesn't handle non-character keys like CTRL and
the arrow keys.

I've read the character mode apps page on MSDN, and couldn't really make
much sense out of it.

So I guess my question is, has ANYONE successfully trapped arrow keys in a
console app, using C#, C++.net, or VB.net? I would really reaaaaally
appreciate any help I can get, thanks.

Finally, if I can't do it, I'll need to write it as a windows app... so the
question is, how do I write to the screen of a windows app, as if it were a
console? (I've seen it done, I just don't know how.)

Thanks,

Chris
 
Hi...

I'm trying to find some code samples that will show me how to capture
non-character keypresses in a console app. I've tried the standard stuff,
such as console.read and console.readline, and I've tried ConsoleEx which
does a great job, but still doesn't handle non-character keys like CTRL and
the arrow keys.

I've read the character mode apps page on MSDN, and couldn't really make
much sense out of it.

So I guess my question is, has ANYONE successfully trapped arrow keys in a
console app, using C#, C++.net, or VB.net? I would really reaaaaally
appreciate any help I can get, thanks.

Maybe if you put the console into character mode instead of line mode
you could capture the keystrokes you're looking for. See this post:

http://tinyurl.com/h6nm
 
Finally, if I can't do it, I'll need to write it as a windows app... so
the
question is, how do I write to the screen of a windows app, as if it were a
console? (I've seen it done, I just don't know how.)

Do I understand this correctly, your trying to make a Windows App
behave/emulate a Console App?

~V
 
character mode does allow me to trap character keys, but I still cant get
the non-character keys like Ctrl and Shift and the arrow keys, etc...

Thanks though,

S.

--
development journal: http://www.mystictriad.com/dev
Heroic Adventure 0.1.1 (HA! for short)
Probably the first Roguelike written in VB.NET
 
Chris Williams said:
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;
}
 
Back
Top