• Input without Carriage Return (Enter)

    From Jon Justvig@STEPPING to All on Thu Aug 6 15:22:01 2009
    Hi all, I've been dealing with a problem for quite some time and was
    wondering if someone could help me with this. I have written a program that is currently under development and has not been released because it's far
    from a public beta release. Anyway, I use std::cin to ask for input from a menu of different option. My question is this. Dealing with portability to compile both in Linux and DOS, I need to be able to just enter one character from the keyboard to issue a command without hitting the enter key or a carriage return. How would I code the input portion after a menu of
    commands has been displayed on the console?

    -- Jon
    telnet://stepping.synchro.net


    ---
    ■ Synchronet ■ Stepping Stone BBS -- telnet://stepping.synchro.net
  • From Nightfox to Jon Justvig on Thu Aug 6 18:28:56 2009
    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to All on Thu Aug 06 2009 15:22:01

    compile both in Linux and DOS, I need to be able to just enter one charac from the keyboard to issue a command without hitting the enter key or a carriage return. How would I code the input portion after a menu of commands has been displayed on the console?

    I don't think such a function exists in standard C++ or C.

    One thing you could do is to write your own function for inputting a
    character from the user and implement it differently, depending on which platform you're compiling on. You'd have the function decleared in a header file, and you can have the implementation in separate .cpp files (one for
    each platform), or a single .cpp file with pre-processor diretives around the implementations so you can give the pre-processor the appropriate flag, and
    it would compile the appropriate code in the .cpp file.

    In Win32, there may be functions provided that do this. This page on Microsoft's MSDN site describes ReadConsole() - I haven't used this, but it may do what you want (although it looks a bit more complicated to use than cin):
    http://msdn.microsoft.com/en-us/library/ms684958%28VS.85%29.aspx

    The Linux implementation could use the nCurses library. nCurses has
    functions for getting a single character, such as getch(), wgetch(), etc.: http://www.mkssoftware.com/docs/man3/curs_getch.3.asp

    I hope this helps.

    Eric

  • From Jon Justvig@STEPPING to Nightfox on Thu Aug 6 20:47:29 2009
    Re: Input without Carriage Return (Enter)
    By: Nightfox to Jon Justvig on Thu Aug 06 2009 06:28 pm

    http://msdn.microsoft.com/en-us/library/ms684958%28VS.85%29.aspx http://www.mkssoftware.com/docs/man3/curs_getch.3.asp

    Thank you for the links. I will look into these when I get more time. I
    will most certainly bookmark them now.

    -- Jon
    telnet://stepping.synchro.net


    ---
    ■ Synchronet ■ Stepping Stone BBS -- telnet://stepping.synchro.net
  • From Nightfox to Jon Justvig on Fri Aug 7 01:16:35 2009
    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to Nightfox on Thu Aug 06 2009 20:47:29

    Re: Input without Carriage Return (Enter)
    By: Nightfox to Jon Justvig on Thu Aug 06 2009 06:28 pm

    http://msdn.microsoft.com/en-us/library/ms684958%28VS.85%29.aspx http://www.mkssoftware.com/docs/man3/curs_getch.3.asp

    Thank you for the links. I will look into these when I get more time. I will most certainly bookmark them now.

    No problem. Come to think of it, though, nCurses might not be the best
    answer for Linux. I've worked with nCurses before, and I know it has functions that input a single character from the user without the user having to press Enter, but nCurses is a full-blown character interface library dealing with "screen" structures; you need to initialize nCurses before you use any of its functions, and then de-initialize it when you're done, and the process of doing that will clear the screen.

    I imagine there must be some C or C++ function in Linux that does what you want to do.

    Nightfox

  • From Jon Justvig@STEPPING to Nightfox on Fri Aug 7 06:43:49 2009
    Re: Input without Carriage Return (Enter)
    By: Nightfox to Jon Justvig on Fri Aug 07 2009 01:16 am

    No problem. Come to think of it, though, nCurses might not be the best answer for Linux. I've worked with nCurses before, and I know it has functions that input a single character from the user without the user havin to press Enter, but nCurses is a full-blown character interface library dealing with "screen" structures; you need to initialize nCurses before you use any of its functions, and then de-initialize it when you're done, and th process of doing that will clear the screen.

    Hmmm...you don't suppose nCurses also includes normal color ANSi as well? Right now my code does not have any color in it. If so, I could tolerate the clear screens as long as it doesn't interfere with game play.

    I imagine there must be some C or C++ function in Linux that does what you want to do.

    I'm hoping there is. I used the header, windows.h, I believe, but, it would not work with linux obviously and I wanted the code to be portable to both
    dos and linux. So, I've been looking around. I'll be glad when I start working on my BS degree in a few years and really dig into programming.

    -- Jon
    telnet://stepping.synchro.net


    ---
    ■ Synchronet ■ Stepping Stone BBS -- telnet://stepping.synchro.net
  • From Nightfox to Jon Justvig on Fri Aug 7 09:14:08 2009
    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to Nightfox on Fri Aug 07 2009 06:43:49

    Hmmm...you don't suppose nCurses also includes normal color ANSi as well? Right now my code does not have any color in it. If so, I could tolerate clear screens as long as it doesn't interfere with game play.

    nCurses can do color, but as far as I know, it doesn't output ANSI codes, unfortunately.

    I imagine there must be some C or C++ function in Linux that does what want to do.

    I'm hoping there is. I used the header, windows.h, I believe, but, it wo not work with linux obviously and I wanted the code to be portable to bot dos and linux. So, I've been looking around. I'll be glad when I start

    C and C++ are kinda tough to work with sometimes because they were meant to
    be portable languages, yet there are things not in their standard libraries that would be nice to do - like being able to input just 1 character without the user having to press Enter. Also, part of what I've read about C and C++ is that since they were meant to be portable, the designers left out features that aren't common on all platforms; thus, it doesn't include a GUI library, for example, because not all systems have a GUI OS. Thus, there are several different GUI libraries to choose from if you want to develop a GUI app, and some GUI libraries limit you to just one platform, while others are multi-platform, and of course, it isn't all universal. :)

    Nightfox

  • From Nick@LIGHTNIN to Jon Justvig on Sat Aug 8 14:59:02 2009
    Re: Input without Carriage Re
    By: Jon Justvig to Nightfox on Fri Aug 07 2009 06:43 am

    Hmmm...you don't suppose nCurses also includes normal color ANSi as well?

    This may not have anything to do with it, but I know of a colorful nCurses screensaver that I actually use to run at login time on my own BBS.

    'cmatrix' package (actually "port") for FreeBSD
    I'm sure all the unixes and linuxes have cmatrix come to think of it.

    The only reason I think it's ANSI is because it of course displays in
    all the high/low/pretty green colors over telnet, and through a BBS.

    So even if it weren't ANSI, SynchroNet BBS software "seems" to convert
    it to ANSI colored equivalent perfectly.

    ---
    ■ Synchronet ■ -_- Lightning BBS! -_- telnet://lightningbbs.dyndns.org
  • From Mercyful Fate to Jon Justvig on Wed Sep 16 12:36:02 2009
    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to All on Thu Aug 06 2009 15:22:01

    Hi all, I've been dealing with a problem for quite some time and was wondering if someone could help me with this. I have written a program that is currently under development and has not been released because it's far from a public beta release. Anyway, I use std::cin to ask for input from a menu of different option. My question is this. Dealing with portability to compile both in Linux and DOS, I need to be able to just enter one character from the keyboard to issue a command without hitting the enter key or a carriage return. How would I code the input portion after a menu of commands has been displayed on the console?
  • From Mercyful Fate to Jon Justvig on Wed Sep 16 12:39:00 2009
    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to All on Thu Aug 06 2009 15:22:01

    Hi all, I've been dealing with a problem for quite some time and was wondering if someone could help me with this. I have written a program that is currently under development and has not been released because it's far from a public beta release. Anyway, I use std::cin to ask for input from a menu of different option. My question is this. Dealing with portability to compile both in Linux and DOS, I need to be able to just enter one character from the keyboard to issue a command without hitting the enter key or a carriage return. How would I code the input portion after a menu of commands has been displayed on the console?

    That depends on what language you are using, if your Using C, or C++ Even, in Windows you can usualy include conio.h which has a getch(), myself i've always used the DevC++, and sorta riped out their conio.h and included it in my own project for windows development. In linux you can use Read() Write() with stdio, it works great.. test with Select(), then if there is data, use Read() to get it, sorta like sockets, but works perfect for console i/o.
  • From Mercyful Fate to Nightfox on Wed Sep 16 12:49:20 2009
    Re: Input without Carriage Return (Enter)
    By: Nightfox to Jon Justvig on Thu Aug 06 2009 18:28:56

    I don't think such a function exists in standard C++ or C.

    One thing you could do is to write your own function for inputting a character from the user and implement it differently, depending on which platform you're compiling on. You'd have the function decleared in a header file, and you can have the implementation in separate .cpp files (one for each platform), or a single .cpp file with pre-processor diretives around th implementations so you can give the pre-processor the appropriate flag, and it would compile the appropriate code in the .cpp file.

    In Win32, there may be functions provided that do this. This page on Microsoft's MSDN site describes ReadConsole() - I haven't used this, but it may do what you want (although it looks a bit more complicated to use than cin):
    http://msdn.microsoft.com/en-us/library/ms684958%28VS.85%29.aspx

    The Linux implementation could use the nCurses library. nCurses has functions for getting a single character, such as getch(), wgetch(), etc.: http://www.mkssoftware.com/docs/man3/curs_getch.3.asp

    I hope this helps.

    I think in Visual Studio, i'm a little foggy, but conio useta be present in VS as of 6.0.. so you can #include <conio.h> i think in more recent versions, i haven't played with any .NET though.. but i remember being able to just include <windows.h> then just adding the following to my header to get access to teh console input functions.

    #ifdef __cplusplus
    extern "C" {
    #endif

    char*_cgets (char*);
    int_cprintf (const char*, ...);
    int_cputs (const char*);
    int_cscanf (char*, ...);

    int_getch (void);
    int_getche (void);
    int_kbhit (void);
    int_putch (int);
    int_ungetch (int);


    intgetch (void);
    intgetche (void);
    intkbhit (void);
    intputch (int);
    intungetch (int);


    #ifdef __cplusplus
    }
    #endif

    then just calling getch() would get a hotkey.
  • From Nightfox to Mercyful Fate on Wed Sep 16 16:08:05 2009
    Re: Input without Carriage Return (Enter)
    By: Nightfox to Mercyful Fate on Wed Sep 16 2009 12:49:20

    I think in Visual Studio, i'm a little foggy, but conio useta be present as of 6.0.. so you can #include <conio.h> i think in more recent version
    ...
    then just calling getch() would get a hotkey.

    That may work in Windows, but I believe Jon Justvig is working in Linux and wants his door to at least work in Linux. For a multi-platform solution, there are ways to use the appropriate function depending on which platform you're using.

    Nightfox
  • From Mercyful Fate to Nightfox on Thu Sep 17 14:04:48 2009
    Re: Input without Carriage Return (Enter)
    By: Nightfox to Mercyful Fate on Wed Sep 16 2009 16:08:05

    That may work in Windows, but I believe Jon Justvig is working in Linux and wants his door to at least work in Linux. For a multi-platform solution, th are ways to use the appropriate function depending on which platform you're using.

    Indeed, although i thought it was being asked an example of both Windows and linux. In linux i would use read() to get stright from stdio.

    This is just a quick and dirty example... You still need to add buffering for escape sequences etc...

    # include <cstdlib>
    # include <cstdarg>
    # include <cstring>
    # include <time.h>


    char getkey() {

    fd_set fds;
    timeval tv;
    char buffer[256]={0};
    int len = 0;
    char ch;

    while(1) {
    if (feof(stdin) || ferror(stdin))
    clearerr(stdin);

    FD_ZERO(&fds);
    FD_SET(STDIN_FILENO, &fds);
    tv.tv_sec = 0;
    tv.tv_usec = 400000; // .4 second delay / Save CPU Usage

    if (select(STDIN_FILENO+1, &fds, 0, 0, &tv)) {
    if (FD_ISSET(STDIN_FILENO, &fds)) {
    len = read(STDIN_FILENO, buffer, sizeof(buffer)-1);

    ch = buffer[0];
    break;
    }
    }
    }
    return ch;
    }
  • From Jon Justvig@STEPPING to Mercyful Fate on Sat Nov 14 12:08:07 2009
    Re: Input without Carriage Return (Enter)
    By: Mercyful Fate to Jon Justvig on Wed Sep 16 2009 11:39 am

    used the DevC++, and sorta riped out their conio.h and included it in my own

    I love Bloodshed Dev++, works great for me.

    -- Jon
    ---
    ■ Synchronet ■ Stepping Stone BBS - stepping.synchro.net
  • From Mercyful Fate to Jon Justvig on Wed Dec 2 10:36:22 2009
    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to Mercyful Fate on Sat Nov 14 2009 12:08:07

    used the DevC++, and sorta riped out their conio.h and included it in my

    I love Bloodshed Dev++, works great for me.

    i agree, in windows it's one of the best free compilers there is, although i haven't touch in a long time since 2002 when i made to switch to full linux on my home systems. The last i saw though the compiler wasn't being updated as ofthen as i would have liked, not sure how current it is anymore, but defently very nice.

    If you want a great IDE though, you should check out eclipse, i found that recently and it's pretty dam sweet.
  • From Jon Justvig@STEPPING to Mercyful Fate on Thu Dec 3 15:06:00 2009
    Mercyful Fate wrote to Jon Justvig <=-

    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to Mercyful Fate on Sat Nov 14 2009 12:08:07

    used the DevC++, and sorta riped out their conio.h and included it in my

    I love Bloodshed Dev++, works great for me.

    i agree, in windows it's one of the best free compilers there is,
    although i haven't touch in a long time since 2002 when i made to
    switch to full linux on my home systems. The last i saw though the compiler wasn't being updated as ofthen as i would have liked, not sure how current it is anymore, but defently very nice.

    Yeah, mine is bit out of date from 2005. I couldn't seem to find the exact release date.

    If you want a great IDE though, you should check out eclipse, i found that recently and it's pretty dam sweet.

    Nice compiler. I may like it better than Bloodshed even, if I can figure out to get the include libraries set in the path. So far, doing everything by the command prompt gives me the most control and works best so far.

    -- Jon

    ... Gone crazy, be back later, please leave message.
    --- MultiMail/Win32 v0.49
    ■ Synchronet ■ Stepping Stone BBS - stepping.synchro.net
  • From Nightfox to Mercyful Fate on Fri Dec 4 00:06:35 2009
    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to Mercyful Fate on Wed Dec 02 2009 10:36:22

    If you want a great IDE though, you should check out eclipse, i found th recently and it's pretty dam sweet.

    I thought Eclipse was mainly for Java, but it has been several years since I tried it. I'd be curious to try it for C++.

    Nightfox
  • From art@FATCATS to Nightfox on Fri Dec 4 08:14:48 2009
    Re: Eclipse
    By: Nightfox to Mercyful Fate on Fri Dec 04 2009 00:06:35

    I thought Eclipse was mainly for Java, but it has been several years since I tried it. I'd be curious to try it for C++.

    Now that we're talking bloaty IDEs (VS, Eclipse)... let me butt in here to say that I've always enjoyed using NetBeans for java, ruby, and of course it has C/C++ support. It's come a long way, and seems a bit more mature than Eclipse, but to each his own of course.

    The "new" free/open source model also helps!

    Kind regards,
    +------------------------------+
    | Art % fatcats.poorcoding.com |
    +--------------- ----- -- - `

    ---
    ■ Synchronet ■ fatcats bbs - http://fatcats.poorcoding.com
  • From Jon Justvig@STEPPING to Nightfox on Fri Dec 4 07:41:00 2009
    Nightfox wrote to Mercyful Fate <=-

    Re: Input without Carriage Return (Enter)
    By: Jon Justvig to Mercyful Fate on Wed Dec 02 2009 10:36:22

    If you want a great IDE though, you should check out eclipse, i found th recently and it's pretty dam sweet.

    I thought Eclipse was mainly for Java, but it has been several years
    since I tried it. I'd be curious to try it for C++.

    There is a C++ version, it does look nice.

    -- Jon

    ... He does the work of 3 Men...Moe, Larry & Curly
    --- MultiMail/Win32 v0.49
    ■ Synchronet ■ Stepping Stone BBS - stepping.synchro.net
  • From Mercyful Fate to Nightfox on Thu Dec 10 10:23:08 2009
    Re: Eclipse
    By: Nightfox to Mercyful Fate on Fri Dec 04 2009 00:06:35

    If you want a great IDE though, you should check out eclipse, i found recently and it's pretty dam sweet.

    I thought Eclipse was mainly for Java, but it has been several years since I tried it. I'd be curious to try it for C++.

    It has java, c++, php and a few others, it's very nice, if i didn't already start my project in Kdevelop (linux) i probably would be using it now.