fgetc without wait for enter

Hi!
I am trying to produce a function as follows:
wait for a key. If the key matches a key found in a string, return the value, else discard the key and wait for another.

The idea is that i want to be able to wait and respond to single-key presses on a scheme, and have a function to handle this.

So i wrote the function
int keywait(char keys[])
where currently keys is a string representing the ASCII values of the keys that can be returned.

So printf("%c", keywait("abcde")) should make a, b, c, d, or e print on the screen when pressed, but no other key.

The function works great -- albeit a couple dilemas.
First, the keys are echoed on the screen during the function.
Second, stdin waits until enter is pressed to return the next key.
These problems i know as a fact are caused by the fgetc function -- which happens to echoe characters and won't start returning characters until a whole line has been typed. I need a function to replace fgetc.

Is there some function in the standard C library that simply checks if a key has been pressed and returns the key, not echoing, and not waiting for the next line to start returning keys?


Any and all help is greatly appreciated. Thanks!!
Sincerely,
Jakykong (Jack Mudge)
[email protected]

Comments

  • :
    : Is there some function in the standard C library that simply checks if a key has been pressed and returns the key, not echoing, and not waiting for the next line to start returning keys?
    :

    There is no standard c function that will do that. But there are some non-standard functions in conio.h if your compiler supports them, such as kbhit()which checks to see if there is a key in the keyboard buffer.
  • [b][red]This message was edited by Jakykong at 2006-8-6 17:45:13[/red][/b][hr]
    : :
    : : Is there some function in the standard C library that simply checks if a key has been pressed and returns the key, not echoing, and not waiting for the next line to start returning keys?
    : :
    :
    : There is no standard c function that will do that. But there are some non-standard functions in conio.h if your compiler supports them, such as kbhit()which checks to see if there is a key in the keyboard buffer.
    :

    i think gcc supports conio... has the header file, so it had better :P

    So, mind informing me how to use kbhit? does it just return an ASCII code for the key, like fgetc (just from the keyboard buffer as you might do with a BIOS call in assembly), or does it have some other way (like scancodes or something) to tell you the key? (mostly because i am unfamiliar with conio :D)

    EDIT:
    Spoke too soon. DJGPP (the dos port of gcc -- in dos, i can compile with the same command i do in linux) does, but no linux conio library has apparently yet been written. Any other ideas?

    Again, thanks for the help!
    Sincerely,
    Jakykong (Jack Mudge)
    [email protected]



  • [blue]I've about zero knowledge of programming for Linux, so bear that in mind as I suggest something to try.

    You might try getch() which I think is in curses.h unless I'm really out in left field.

    Try the following:

    [code]


    int keystroke;

    ...

    keystroke = getch();
    printf("%c was pressed which has the character code %d

    ", keystroke, keystroke);
    [/code]

    Take Care,
    Ed

  • : [blue]I've about zero knowledge of programming for Linux, so bear that in mind as I suggest something to try.
    :
    : You might try getch() which I think is in curses.h unless I'm really out in left field.
    :
    : Try the following:
    :
    : [code]
    :
    :
    : int keystroke;
    :
    : ...
    :
    : keystroke = getch();
    : printf("%c was pressed which has the character code %d

    ", keystroke, keystroke);
    : [/code]
    :
    : Take Care,
    : Ed
    :
    :

    Currently i don't have curses.h, However that could be due to a lack of dev packages on curses development. I'm going to install the libcurses5-dev package and see what i come up with then...

    Curses now exists.
    Unfortunately, it does seem to have some major problems (like telling me getch() has some undefined references.)
    SO! i think i'll search the web for some curses manuals...

    Not a problem lacking skill in the area :) You got me off on the right foot anyway -- i know what to look for now, which makes searching possible. Thanks!

    P.S. linux programming is a stinkin' lot easier then windows/DOS programming for the most part (at least as far as i've been programming). GNU's library is extensive, even if it's not totally ANSI C. But then again, on modern systems with modern capabilities and modern operating systems, who wants to totally stick to ANSI C? Given how little it's able to accomplish by today's standards, i'm not picky about what tools i need to use -- whatever gets the job done :). Thanks again!
    Sincerely,
    Jakykong (Jack Mudge)
    [email protected]

  • [blue]I found some more info for you. The curses.h version needs void, to be used in the manner I described in my previous post.

    [code]


    int keystroke;

    ...

    keystroke = getch([red]void[/red]);
    printf("%c was pressed which has the character code %d

    ", keystroke, keystroke);
    [/code]

    Here's another link that may help:

    http://cscene.unitycode.org/CS3/CS3-08.html

    (MW)GETCH is the last item on the page.

    I've been looking at Linux for awhile now trying to get one of my machines running it. But I'm having minor issues that are larger because of my inexperience. My latest trouble was with a Fedora install. Everything seemed to be fine right up until I try to run it, when the mouse gets too choppy to use. The mouse worked fine through the entire install and right up to the login screen where it trashes. I do have a debian install that works fine, but again, I'm familiar with DOS and Windows, so I don't know where to go after I'm up and running.

    I do like the idea of open source and would love to contribute some programming, but I'm quite amateur in my capabilities and only have spurious moments of programming time. So, I'm dabbling for now and hope I'll figure some things out along the way.

    Take Care,
    Ed

  • : [blue]I found some more info for you. The curses.h version needs void, to be used in the manner I described in my previous post.
    :
    : [code]
    :
    :
    : int keystroke;
    :
    : ...
    :
    : keystroke = getch([red]void[/red]);
    : printf("%c was pressed which has the character code %d

    ", keystroke, keystroke);
    : [/code]
    :
    : Here's another link that may help:
    :
    : http://cscene.unitycode.org/CS3/CS3-08.html
    :
    : (MW)GETCH is the last item on the page.
    :
    : I've been looking at Linux for awhile now trying to get one of my machines running it. But I'm having minor issues that are larger because of my inexperience. My latest trouble was with a Fedora install. Everything seemed to be fine right up until I try to run it, when the mouse gets too choppy to use. The mouse worked fine through the entire install and right up to the login screen where it trashes. I do have a debian install that works fine, but again, I'm familiar with DOS and Windows, so I don't know where to go after I'm up and running.
    :
    : I do like the idea of open source and would love to contribute some programming, but I'm quite amateur in my capabilities and only have spurious moments of programming time. So, I'm dabbling for now and hope I'll figure some things out along the way.
    :
    : Take Care,
    : Ed
    :
    :


    [/blue]
    keystroke = getch([red]void[/red]);

    I doubt that, passing void to a function is not legal C/C++.
  • : keystroke = getch([red]void[/red]);
    :
    : I doubt that, passing void to a function is not legal C/C++.
    :
    [blue]Well, I'm not very familiar with Linux programming, but I guess curses.h is meant for C/C++ use. Anyway as I research curses.h, it has lots of prototypes with (void):

    [code]
    Function Prototypes

    The following are declared as functions, and may also be defined as macros:

    int baudrate(void);
    int beep(void);
    int cbreak(void);
    int clear(void);
    int clrtobot(void);
    int clrtoeol(void);
    int color_set(short,void *);
    int def_prog_mode(void);
    int def_shell_mode(void);
    int delch(void);
    char erasechar(void);
    int erase(void);
    void filter(void);
    int flash(void);
    int flushinp(void);
    int getch(void);

    These are just a few...
    [/code]

    Maybe I'm missing something?

    Take Care,
    Ed[/blue]




  • : [blue]I found some more info for you. The curses.h version needs void, to be used in the manner I described in my previous post.
    :
    : [code]
    :
    :
    : int keystroke;
    :
    : ...
    :
    : keystroke = getch([red]void[/red]);
    : printf("%c was pressed which has the character code %d

    ", keystroke, keystroke);
    : [/code]
    :
    : Here's another link that may help:
    :
    : http://cscene.unitycode.org/CS3/CS3-08.html
    :
    : (MW)GETCH is the last item on the page.
    :
    : I've been looking at Linux for awhile now trying to get one of my machines running it. But I'm having minor issues that are larger because of my inexperience. My latest trouble was with a Fedora install. Everything seemed to be fine right up until I try to run it, when the mouse gets too choppy to use. The mouse worked fine through the entire install and right up to the login screen where it trashes. I do have a debian install that works fine, but again, I'm familiar with DOS and Windows, so I don't know where to go after I'm up and running.
    :
    : I do like the idea of open source and would love to contribute some programming, but I'm quite amateur in my capabilities and only have spurious moments of programming time. So, I'm dabbling for now and hope I'll figure some things out along the way.
    :
    : Take Care,
    : Ed
    :
    :
    I also found the same page before reading this post, ironically. It was helpful, thanks.
    Ncurses seems to be the ultimate way to do textmode programming :-).

    A note about linux: many distributions tend (unless you pay for them) to require you to already know a fair amount about linux. Before you try to install ordinary debian (not sure about other distributions because i was never willing to give up apt-get), try some variation on it that is more user-friendly. I personally use kubuntu (http://kubuntu.com). It is very user-friendly as linux goes, is based on debian, but has a windows-like install process (with auto hardware detection, though, like windows, it can't detect quite everything on it's own). Also automatically has a GUI so you don't have to know nitty-gritty configuration settings for the X11 window system right off the bat to get it running beyond the command line.

    Perhaps you should give that a go.

    Or, even this: if you aren't set on your way to leave windows and use linux (Yet. You will be.), go to http://www.knoppix.com and get a copy of knoppix. Knoppix is live-cd, meaning it boots off the cd and never touches your hard drive -- so you can try linux out, get the hang of it, THEN install it when you choose to do so.
    Sincerely,
    Jakykong (Jack Mudge)
    [email protected]

  • : : [blue]I found some more info for you. The curses.h version needs void, to be used in the manner I described in my previous post.
    : :
    : : [code]
    : :
    : :
    : : int keystroke;
    : :
    : : ...
    : :
    : : keystroke = getch([red]void[/red]);
    : : printf("%c was pressed which has the character code %d

    ", keystroke, keystroke);
    : : [/code]
    : :
    : : Here's another link that may help:
    : :
    : : http://cscene.unitycode.org/CS3/CS3-08.html
    : :
    : : (MW)GETCH is the last item on the page.
    : :
    : : I've been looking at Linux for awhile now trying to get one of my machines running it. But I'm having minor issues that are larger because of my inexperience. My latest trouble was with a Fedora install. Everything seemed to be fine right up until I try to run it, when the mouse gets too choppy to use. The mouse worked fine through the entire install and right up to the login screen where it trashes. I do have a debian install that works fine, but again, I'm familiar with DOS and Windows, so I don't know where to go after I'm up and running.
    : :
    : : I do like the idea of open source and would love to contribute some programming, but I'm quite amateur in my capabilities and only have spurious moments of programming time. So, I'm dabbling for now and hope I'll figure some things out along the way.
    : :
    : : Take Care,
    : : Ed
    : :
    : :
    :
    :
    : [/blue]
    : keystroke = getch([red]void[/red]);
    :
    : I doubt that, passing void to a function is not legal C/C++.
    :

    I use C only, not C++ -- mostly because C++ is hard to write assemly code for :-). I happen to program in assemly as well, so C is a good choice for me.
    Void is fine in a function in C, though at least with GCC you don't have to declare it void -- just getch(); works fine.

    And, just using that function on it's own won't do the trick, because to use that function, you have to initialize the curses system, thereby disallowing you standard console access. That's fine -- i read up on ncurses and found that it is quite nice. I think i'll use it over the standard library any day i'm not writing for a teletypewriter :-P.

    Thanks!
    Sincerely,
    Jakykong (Jack Mudge)
    [email protected]

  • : : keystroke = getch([red]void[/red]);
    : :
    : : I doubt that, passing void to a function is not legal C/C++.
    : :
    : [blue]Well, I'm not very familiar with Linux programming, but I guess curses.h is meant for C/C++ use. Anyway as I research curses.h, it has lots of prototypes with (void):
    :
    : [code]
    : Function Prototypes
    :
    : The following are declared as functions, and may also be defined as macros:
    :
    : int baudrate(void);
    : int beep(void);
    : int cbreak(void);
    : int clear(void);
    : int clrtobot(void);
    : int clrtoeol(void);
    : int color_set(short,void *);
    : int def_prog_mode(void);
    : int def_shell_mode(void);
    : int delch(void);
    : char erasechar(void);
    : int erase(void);
    : void filter(void);
    : int flash(void);
    : int flushinp(void);
    : int getch(void);
    :
    : These are just a few...
    : [/code]
    :
    : Maybe I'm missing something?
    :
    : Take Care,
    : Ed[/blue]
    :
    :
    :
    :
    :
    lots of things other then that. At least my ncurses.h header has about 200 functions (estimating, haven't counted) and macros listed... printw = printf (but in ncurses mode), move() lets me move around the screen, mvprintw is like a move then a printw.
    So far, every stdio function i know by heart (that does console I/O anyway) has an ncurses parallel. Should be pretty easy to migrate :-).
    Sincerely,
    Jakykong (Jack Mudge)
    [email protected]

  • : : keystroke = getch([red]void[/red]);
    : :
    : : I doubt that, passing void to a function is not legal C/C++.
    : :
    : [blue]Well, I'm not very familiar with Linux programming, but I guess curses.h is meant for C/C++ use. Anyway as I research curses.h, it has lots of prototypes with (void):
    :
    : [code]
    : Function Prototypes
    :
    : The following are declared as functions, and may also be defined as macros:
    :
    : int baudrate(void);
    : int beep(void);
    : int cbreak(void);
    : int clear(void);
    : int clrtobot(void);
    : int clrtoeol(void);
    : int color_set(short,void *);
    : int def_prog_mode(void);
    : int def_shell_mode(void);
    : int delch(void);
    : char erasechar(void);
    : int erase(void);
    : void filter(void);
    : int flash(void);
    : int flushinp(void);
    : int getch(void);
    :
    : These are just a few...
    : [/code]
    :
    : Maybe I'm missing something?
    :
    : Take Care,
    : Ed[/blue]
    :
    :


    Yeah, but you wrote 'void' in the function [b]call[/b] which isn't legal C/C++.
  • : Yeah, but you wrote 'void' in the function [b]call[/b] which isn't legal C/C++.
    :
    [blue]OK, now I understand. I got confused when Jakykong said getch() had unresolved references. I started thinking it needed void if not used with parameters, but what I was looking at was wgetch, mvgetch, etc.

    Thanks for trying to keep me straight.:-)

    Take Care,
    Ed[/blue]

  • : A note about linux: many distributions tend (unless you pay for them) to require you to already know a fair amount about linux. Before you try to install ordinary debian (not sure about other distributions because i was never willing to give up apt-get), try some variation on it that is more user-friendly. I personally use kubuntu (http://kubuntu.com). It is very user-friendly as linux goes, is based on debian, but has a windows-like install process (with auto hardware detection, though, like windows, it can't detect quite everything on it's own). Also automatically has a GUI so you don't have to know nitty-gritty configuration settings for the X11 window system right off the bat to get it running beyond the command line.
    :
    : Perhaps you should give that a go.
    :
    : Or, even this: if you aren't set on your way to leave windows and use linux (Yet. You will be.), go to http://www.knoppix.com and get a copy of knoppix. Knoppix is live-cd, meaning it boots off the cd and never touches your hard drive -- so you can try linux out, get the hang of it, THEN install it when you choose to do so.
    : Sincerely,
    : Jakykong (Jack Mudge)
    : [email protected]
    :
    [blue]Thanks for the info. I've played with the knoppix both from CD and installing. Then I installed a rather large Debian package that used (I think) Gnome. Lately, I've installed Fedora (also uses Gnome). I'm still a bit too new to the systems though. Little things are annoying me - like why should I have to figure out a work around to have the numlock turned on at bootup? The BIOS knows I want that. I see everywhere there are "fixes" for that issue, but none seem to pertain to MY installation. Linux programmers can't figure out how to read the BIOS entries, but Microsoft can? Sorry, I seem to have gotten sidetracked...

    To get back on topic, I selected to install the developer part of the Fedora package and there's nothing there that I recognize as any programming. All I found was something that looks like a picture/symbol editor. So I'm not sure how to move into that realm. What packages should I look for if I want to try Linux C/C++ programming that would compile programs to run in the Gnome desktop? Is there an equivalent of Forger's Tutorial?

    Thanks again.

    Take Care,
    Ed[/blue]

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories