Go Back   Velocity Reviews > Newsgroups > DotNet
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

DotNet - console app problem

 
Thread Tools Search this Thread
Old 07-17-2003, 02:36 AM   #1
Default console app problem


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

--
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
  Reply With Quote
Old 07-17-2003, 04:28 AM   #2
Patrick Steele [MVP]
 
Posts: n/a
Default Re: console app problem

In article <e$>,
says...
> 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

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
  Reply With Quote
Old 07-17-2003, 05:01 AM   #3
Vincent Wiegel
 
Posts: n/a
Default Re: console app problem

> 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


"Chris Williams" <> wrote in message
news:e$...
> 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
>
> --
> development journal: http://www.mystictriad.com/dev
> Heroic Adventure 0.1.1 (HA! for short)
> Probably the first Roguelike written in VB.NET
>
>



  Reply With Quote
Old 07-18-2003, 01:42 AM   #4
Chris Williams
 
Posts: n/a
Default Re: console app problem

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


"Patrick Steele [MVP]" <> wrote in message
news: om...
> In article <e$>,
> says...
> > 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
>
> --
> Patrick Steele
> Microsoft .NET MVP
> http://weblogs.asp.net/psteele



  Reply With Quote
Old 07-24-2003, 08:58 AM   #5
Brendan Duffy
 
Posts: n/a
Default Re: console app problem

"Chris Williams" <> wrote in message
news:%...
> 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;
}


  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump