Thread: to ask if a key is pressed

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    25

    to ask if a key is pressed

    hi guys

    I need to run some operation if a key from keyboard is pressed

    so I go with

    Code:
    c=getchar();
    to get it read. yet the user could press a key anytime; so I'd need some if-loop. no plans on how it'd look like though...I suppose something like this below wouldn't work right?

    Code:
    if(getchar()==1){
    
    ..
    
    }




    any hints? thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Checking to see whether there is any key requires some OS/Compiler specific coding.
    FAQ > How can I get input without having the user hit [Enter]? - Cprogramming.com

    You need to tell us your OS/Compiler if you want specific details.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    Quote Originally Posted by Salem View Post
    You need to tell us your OS/Compiler if you want specific details.

    It's Eclipse based on GNU-C/C++- compiler

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    OS means operating system.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    25
    Quote Originally Posted by stahta01 View Post
    OS means operating system.
    ikr? yet not sure what OS has to do with it....I use Windows 7

  6. #6
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Aitra View Post
    ikr? yet not sure what OS has to do with it
    Standard C doesn't provide that information. You need to use OS specific functions.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. While pressed
    By yann in forum C Programming
    Replies: 3
    Last Post: 09-15-2009, 10:42 AM
  2. C - until pressed
    By yann in forum C Programming
    Replies: 7
    Last Post: 09-08-2009, 11:51 AM
  3. key pressed
    By hareldo in forum Windows Programming
    Replies: 7
    Last Post: 10-08-2008, 04:17 PM
  4. Replies: 3
    Last Post: 02-10-2008, 10:13 AM
  5. do while any key not pressed
    By ipe in forum C Programming
    Replies: 2
    Last Post: 08-03-2003, 02:21 PM