console app problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Williams

    #1

    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.readlin e, 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


  • Patrick Steele [MVP]

    #2
    Re: console app problem

    In article <e$t2EPATDHA.26 76@TK2MSFTNGP10 .phx.gbl>,
    spam@spamspamsp amspam.com says...[color=blue]
    > 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.readlin e, 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.[/color]

    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

    Comment

    • Vincent Wiegel

      #3
      Re: console app problem

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

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

      ~V


      "Chris Williams" <spam@spamspams pamspam.com> wrote in message
      news:e$t2EPATDH A.2676@TK2MSFTN GP10.phx.gbl...[color=blue]
      > 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.readlin e, and I've tried ConsoleEx which
      > does a great job, but still doesn't handle non-character keys like CTRL[/color]
      and[color=blue]
      > 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[/color]
      the[color=blue]
      > question is, how do I write to the screen of a windows app, as if it were[/color]
      a[color=blue]
      > 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
      >
      >[/color]


      Comment

      • Chris Williams

        #4
        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]" <patrick@mvps.o rg> wrote in message
        news:MPG.197fe1 f1ebd4cad39897b 1@msnews.micros oft.com...[color=blue]
        > In article <e$t2EPATDHA.26 76@TK2MSFTNGP10 .phx.gbl>,
        > spam@spamspamsp amspam.com says...[color=green]
        > > 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[/color][/color]
        stuff,[color=blue][color=green]
        > > such as console.read and console.readlin e, and I've tried ConsoleEx[/color][/color]
        which[color=blue][color=green]
        > > does a great job, but still doesn't handle non-character keys like CTRL[/color][/color]
        and[color=blue][color=green]
        > > 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[/color][/color]
        a[color=blue][color=green]
        > > console app, using C#, C++.net, or VB.net? I would really reaaaaally
        > > appreciate any help I can get, thanks.[/color]
        >
        > 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[/color]


        Comment

        • Brendan Duffy

          #5
          Re: console app problem

          "Chris Williams" <spam@spamspams pamspam.com> wrote in message
          news:%230MZWTMT DHA.3116@TK2MSF TNGP10.phx.gbl. ..[color=blue]
          > nope, I'm trying to write a console app that captures non-character
          > keypresses as part of the program.
          >[/color]

          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(ST D_INPUT_HANDLE) ;
          AutoResetEvent are = new AutoResetEvent( false);
          are.Handle = stdIn;
          for(;;){
          are.WaitOne();
          System.Diagnost ics.Debug.Write Line("released" );
          int ret = ReadConsoleInpu t(stdIn, ref iRecord, 1, ref nRead);
          for(int n=0; n!=nRead; ++n)
          if(iRecord.Even tType==KEY_EVEN T){
          ushort vk = iRecord.wVirtua lScanCode;
          if(vk==0x2A) System.Diagnost ics.Debug.Write Line("Left Shift");
          else
          System.Diagnost ics.Debug.Write Line(vk.ToStrin g());
          }
          }
          }
          const uint STD_INPUT_HANDL E = 0xFFFFFFFF-9;
          const ushort KEY_EVENT = 0x0001;
          [DllImport("Kern el32")]
          public static extern IntPtr GetStdHandle(ui nt nStdHandle);
          [DllImport("Kern el32")]
          public static extern int ReadConsoleInpu t(IntPtr hConsoleInput, ref
          INPUT_RECORD lpBuffer, uint nLength, ref uint lpNumberOfEvent sRead);
          [StructLayout(La youtKind.Sequen tial)]
          public struct INPUT_RECORD
          {
          public ushort EventType;
          public uint bKeyDown;
          public ushort wRepeatCount;
          public ushort wVirtualKeyCode ;
          public ushort wVirtualScanCod e;
          public char UnicodeChar;
          public uint dwControlKeySta te;
          }


          Comment

          Working...