• Input without Carriage Return (Enter)

    From Mercyful Fate@DIGDIST 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?
    ---
    ■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com
  • From Mercyful Fate@DIGDIST 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.
    ---
    ■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com
  • From Mercyful Fate@DIGDIST 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.
    ---
    ■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com
  • From Nightfox@DIGDIST 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
    ---
    ■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com
  • From Mercyful Fate@DIGDIST 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;
    }
    ---
    ■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com
  • From Jon Justvig@DIGDIST/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@DIGDIST 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.
    ---
    ■ Synchronet ■ Digital Distortion: digitaldistortionbbs.com