Thread: Many years programming but C is new to me - keyboard question:

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    Many years programming but C is new to me - keyboard question:

    Hi,

    I am trying to figure out a way I can achieve the following in C:

    Set a variable to read the keyboard but NOT display it on the screen as _gtchr() seems to do. I want to be able to use printf to write the variable to the screen as a word-processor routine would do.

    do
    set a variable to the key pressed
    loop until a key is pressed

    convert the ascii value of the key to a string
    locate a position on the screen
    print the string character to the screen in the intended column and row.

    I can work with conditional statements to handle Esc (27), Enter (13) or (10), crtl key presses, etc., later. For now, I would really like to find a way to just place the ascii code of a key press into a variable without having it AUTOMATICALLY printed to the screen.

    BTW – I am using Dev-C++ to compile C and C++ programs.

    Thanks,

    Pete

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    do
    set a variable to the key pressed
    loop until a key is pressed
    FAQ > How do I... (Level 2) > How can I get input without having the user hit [Enter]?

    Set a variable to read the keyboard but NOT display it on the screen as _gtchr() seems to do.
    getch() (in conio.h) as mentioned in the FAQ link above.
    convert the ascii value of the key to a string
    Maybe this? http://faq.cprogramming.com/cgi-bin/...&id=1043284385 ... Option 1.
    For now, I would really like to find a way to just place the ascii code of a key press into a variable without having it AUTOMATICALLY printed to the screen.
    Usually users like to see what they've typed . . . use getch().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by Pete_2000
    Hi,

    I am trying to figure out a way I can achieve the following in C:

    Set a variable to read the keyboard but NOT display it on the screen as _gtchr() seems to do. I want to be able to use printf to write the variable to the screen as a word-processor routine would do.

    do
    set a variable to the key pressed
    loop until a key is pressed
    As dwks said, you can use getch to read a single character from the console without enter. Getch automatically waits untill a key is pressed. You can use kbhit() to see if a character is pressed or not. You'd have to look up what it returns, 'cause I don't know. kbhit() is not standard, however.

    convert the ascii value of the key to a string
    itoa() is the function you need. pass tha character as if it was an int.

    locate a position on the screen
    print the string character to the screen in the intended column and row.
    C does not have these capibilities built in.

    I can work with conditional statements to handle Esc (27), Enter (13) or (10), crtl key presses, etc., later. For now, I would really like to find a way to just place the ascii code of a key press into a variable without having it AUTOMATICALLY printed to the screen.
    As I said getch() will do it. Getch() deals with control keys for you, such as arrows and number pad values. Look it up. Control+Key operations however, is managed by the OS. Each such character should return an ascii value.
    Last edited by King Mir; 05-03-2006 at 12:30 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    kbhit() is not standard, however.
    Neither is getch(). Both, however, are available in Dev-C++'s <conio.h>. That's why I like it when people post their compiler.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by dwks
    Neither is getch(). Both, however, are available in Dev-C++'s <conio.h>. That's why I like it when people post their compiler.
    Really? Than Why is it in my iRMX® C Library Reference?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Standard, around here, means it's part of the ANSI standard (usually C89 unless other wise specified), or maybe POSIX in the Linux forum. Your iRMX reference certainly doesn't define the standard.

    [edit] To answer your question, because your iRMX manual isn't ANSI standard. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    _gtchr()
    You probably mean _getch(), which does the same as getch() under Microsoft compilers, but not under Dev-C++. Another pitfall of using non-standard functions.

    I would suggest using getchar() or fgets() or scanf() (ANSI standard input functions).

    [edit] Unless there's a function called _gtchr() that I'm un-aware of. Another non-standard function. [/edit]

    [edit=2]
    A function of that name seems highly doubtful, because google turns up nothing: http://www.google.ca/search?hl=en&q=_gtchr&meta=
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    9
    Thanks DWK,

    Canada, cool - no pun intended, well, maybe. Actually, I have a friend in Canada who is a graphics artist and who programs as a hobby. Anyway, thanks for pointing me to that FAQ. I had to tweak it a bit and I was wondering what the difference is between using _getch(), which I beliveve can also be written as int_getch() and getch()? I had to change the code in the FAQ to _getch() to stop it from automatically writing to the screen on my Win 98 (and yes, I did remove the printf statement, first.) Anyway, when I used _getch() instead of getch(), the program worked. I was able to press keys without seeing any of them show up on the screen, until I put back in the printf statement.

    Eventually, when I learn a lot more about c, I will design my own keyboard input routine. I made a word-proseccor 15 years ago in basic and you need to be able to control the screen display for word-wrap. That is why I was asking for this particular keyboard function.

    Thanks for the speedy and very helpful reply.

    Pete

    This is the code I ended up with. The only changes were getch() to _getch and I used 27, the escape key to end it instead of the 'q' key. I left the printf in for now, I will manipulate the screen output at another time.-----------------------

    Code:
    #include <stdio.h>
    #include <conio.h> 
    
    int main()
    {
      int ch;
    
      puts ("Press any key, q to quit");  
      
      while ((ch = _getch()) != EOF && ch != 27)
      printf ("%c", ch);
    
      return 0;
    }

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Under Unix, "echo off" means input is not displayed, is there an mdsos equivilent?

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It's the same in MSDOS.
    Sent from my iPad®

  11. #11
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    That's what I thought, however I just tried it and it didn't work.
    No error message, but the characters were still echoed.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    echo off in MSDOS merely means that the console won't display the directory you are in like it normally would. You can also talk to yourself:
    Code:
    C:\>_
    C:\>echo Hello World!
    Hello World!
    C:\>echo off
    _

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    When used in batch files, echo off turns off echoing of commands (like a leading @):
    Code:
    C:\>type h.bat
    echo hello
    C:\>h
    
    C:\>echo hello
    hello
    
    C:\>type i.bat
    @echo off
    echo hello
    C:\>i
    hello
    C:\>
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I had to tweak it a bit and I was wondering what the difference is between using _getch(), which I beliveve can also be written as int_getch() and getch()? I had to change the code in the FAQ to _getch() to stop it from automatically writing to the screen on my Win 98 (and yes, I did remove the printf statement, first.) Anyway, when I used _getch() instead of getch(), the program worked.
    Who knows. They're both non-standard, so what works for me might not work for you.

    As far as I know, _getch() is the Microsoft implementation of the Borland getch().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  3. A newb question about keyboard input
    By PorkyChop in forum Game Programming
    Replies: 4
    Last Post: 12-06-2002, 01:12 PM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM