home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / msdos / programm / 8895 < prev    next >
Encoding:
Text File  |  1992-08-30  |  4.2 KB  |  88 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!ncoast!brown
  3. From: brown@NCoast.ORG (Stan Brown)
  4. Subject: Re: Keyboard input processing -- rolling one's own?
  5. Organization: Oak Road Systems, Cleveland Ohio USA
  6. Date: Mon, 31 Aug 1992 02:06:29 GMT
  7. Message-ID: <BttsIw.4z1@NCoast.ORG>
  8. References: <715029154snx@Wellspring.COM>
  9. Lines: 77
  10.  
  11. In article <715029154snx@Wellspring.COM> elliot@Wellspring.COM writes:
  12. >
  13. >In Borland C++ (3.0) and MSC, there is a routine called getch which
  14. >gets a character directly from the console.  I can use this and
  15. >isatty() to write my own getc that does what I want.  The problem is,
  16. >I would like to attempt to make this routine portable to other
  17. >compilers and to UN*X; i.e. using ANSIC or C++ constructs only.
  18. >getch() is _not_ ANSI.
  19. >
  20. >Is this possible?
  21.  
  22. No, it is not possible.  The keyboard is unknown to ANSI C.  (Don't post
  23. disagreements until you've read the standard, please.)  So there is in
  24. principle no ANSI-standard way to do anything that requires the presence
  25. of a keyboard.
  26.  
  27. FWIW, Microsoft C has getch( ); I think most MS-DOS based compilers do.
  28.  
  29. This topic (like so many) is well covered by the FAQ list in
  30. comp.lang.c.  Here's the relevant excerpt:
  31.  
  32. -----------------------------------------------------------------------
  33. 12.1:   How can I read a single character from the keyboard without
  34.         waiting for a newline?
  35.  
  36. A:      Contrary to popular belief and many people's wishes, this is not
  37.         a C-related question.  (Nor are closely-related questions
  38.         concerning the echo of keyboard input.)  The delivery of
  39.         characters from a "keyboard" to a C program is a function of the
  40.         operating system in use, and cannot be standardized by the C
  41.         language.  Some versions of curses have a cbreak() function
  42.         which does what you want.  Under UNIX, use ioctl to play with
  43.         the terminal driver modes (CBREAK or RAW under "classic"
  44.         versions; ICANON, c_cc[VMIN] and c_cc[VTIME] under System V or
  45.         Posix systems).  Under MS-DOS, use getch().  Under VMS, try the
  46.         Screen Management (SMG$) routines.  Under other operating
  47.         systems, you're on your own.  Beware that some operating systems
  48.         make this sort of thing impossible, because character collection
  49.         into input lines is done by peripheral processors not under
  50.         direct control of the CPU running your program.
  51.  
  52.         Operating system specific questions are not appropriate for
  53.         comp.lang.c .  Many common questions are answered in
  54.         frequently-asked questions postings in such groups as
  55.         comp.unix.questions and comp.os.msdos.programmer .  Note that
  56.         the answers are often not unique even across different variants
  57.         of a system; bear in mind when answering system-specific
  58.         questions that the answer that applies to your system may not
  59.         apply to everyone else's.
  60.  
  61.         References: PCS Sec. 10 pp. 128-9, Sec. 10.1 pp. 130-1.
  62.  
  63. 12.2:   How can I find out if there are characters available for reading
  64.         (and if so, how many)?  Alternatively, how can I do a read that
  65.         will not block if there are no characters available?
  66.  
  67. A:      These, too, are entirely operating-system-specific.  Some
  68.         versions of curses have a nodelay() function.  Depending on your
  69.         system, you may also be able to use "nonblocking I/O", or a
  70.         system call named "select", or the FIONREAD ioctl, or kbhit(),
  71.         or rdchk(), or the O_NDELAY option to open() or fcntl().
  72.  
  73.                                         Steve Summit
  74.                                         scs@adam.mit.edu
  75.                                         scs%adam.mit.edu@mit.edu
  76.                                         mit-eddie!adam.mit.edu!scs
  77.  
  78. This article is Copyright 1988, 1990-1992 by Steve Summit.
  79. It may be freely redistributed so long as the author's name, and this
  80. notice, are retained.
  81. -----------------------------------------------------------------------
  82.  
  83. -- 
  84. Stan Brown, Oak Road Systems                      brown@Ncoast.ORG
  85. "Self-esteem, n.  An erroneous appraisement."
  86. "Self-evident, adj.  Evident to one's self and to nobody else."
  87.                                                --Ambrose Bierce
  88.