getch() command on mac

Discussion in 'Mac Programming' started by anthonyme, Nov 2, 2016.

  1. anthonyme macrumors newbie

    Joined:
    Nov 2, 2016
    #1
    Hello,
    I was programming on xcode and trying to enter a character without pressing de 'Enter' key, but the the command _getch() doesn't work. Is there another way of doing it?
    thank you
     
  2. hostins macrumors member

    Joined:
    Aug 8, 2009
    #2
    i´m not sure, but you need to implement something with kbhit() instead of using getch()
     
  3. anthonyme thread starter macrumors newbie

    Joined:
    Nov 2, 2016
    #3
    I've tried to use it but xcode tells me it is undeclared. Do you know the library it comes with?
     
  4. Toutou macrumors 6502a

    Toutou

    Joined:
    Jan 6, 2015
    Location:
    Prague, Czech Republic
  5. anthonyme thread starter macrumors newbie

    Joined:
    Nov 2, 2016
    #5
    Oh I'm sorry I totally forgot to tell you I'm working in c++
     
  6. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #6
    This is how I implemented getch for a ported MS/DOS program (!) that needed it. As a bonus, this also works for Linux.

    #include <stdlib.h>

    #include <sys/ioctl.h>

    #include <termios.h>

    int getch(void) {

    char chbuf[1];

    struct termios oldstate, newstate;

    tcgetattr(0, &oldstate);

    newstate = oldstate;

    newstate.c_lflag &= ~ICANON;

    newstate.c_lflag &= ~ECHO;

    tcsetattr(0, TCSANOW, &newstate);

    read(0, &chbuf, 1);

    tcsetattr(0, TCSANOW, &oldstate);

    }
     
  7. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #7
    Is there some reason not to use the standard C function: int getchar(void)?
     
  8. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #8
    It uses line buffering -- you only get keystrokes after an entire line has been typed in. getch returns a single character typed (and without echoing it).
     
  9. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #9
    There is the fact that it is NOT part of any standard!

    If I remember correctly (and I may not) it was a Borland provide routine on DOS/Windows.
     
  10. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #10
    You have it backwards. getch() was by Borland. getchar() is in the Standard C Library and equivalent to getc(stdin).
     
  11. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    As I said - I may not!
     
  12. anthonyme thread starter macrumors newbie

    Joined:
    Nov 2, 2016
    #12
    Thanks a lot! The only problem is that it tells me that the identifier "read" is undeclared so the program does not work... Would it be because of a library or something?
     
  13. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #13
    It's in the standard C library. libc. Do "man 2 read" on the command line to get the documentation.
     
  14. anthonyme thread starter macrumors newbie

    Joined:
    Nov 2, 2016
    #14
    I am working on c++ does it change something?
     
  15. talmy macrumors 601

    talmy

    Joined:
    Oct 26, 2009
    Location:
    Oregon
    #15
    It shouldn't as long as the correct header file is specified. The man page says you need:
    #include <sys/types.h>

    #include <sys/uio.h>

    #include <unistd.h>

    so add those in and see if it fixes it.
     
  16. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #16
    Since this is a Mac programming forum, the right way to do this is to create an XCode project with a single window. Add an NSTextField to the window. Create a custom class to be the window.xib's file owner. Create a delegate method
    Code:
    - (void) controlTextDidChange:(NSNotification *) aNotification;
    NSTextField delegate method and link to it. Every time a user types a character this method will be invoked. In the code for this method you can get the string from the text field using a call to some method like
    Code:
    NSString * myString = [TextFieldName stringValue];
    This is very oversimplified but it's really the way to get user input for the Mac.

    Yes, you can create a console app as well.
     
  17. _circledancer macrumors newbie

    _circledancer

    Joined:
    Mar 2, 2018
    #17



    HI HERE IS MY RESPONSe...
    so this is my first macrumor post I think. I'm coding. Trying to figure out these header files and libraries. Can you elaborate on how to 'add an NSTextField to the window and create a custom class to be the window.xib's file owner" ... it sounds like you know what you're doing and all my peers and instructor use WINDOWS and no one can help me .
     
  18. Senor Cuete macrumors regular

    Joined:
    Nov 9, 2011
    #18
    Create a single window application with XCode. Drag an NSTextField from the options in the right pane into the window.

    You need to look at examples of how to do this in a programming book like Cocoa Programming for OS X: The Big Nerd Ranch Guide (5th Edition). Search for others at Amazon.com.

    Or you can create a C command line tool.
     

Share This Page