"on key press"

This is a discussion on "on key press" within the C Programming forums, part of the General Programming Boards category; How to know when a key from the keyboard is pressed ? (PS: i cannot use the return key, just ...

  1. #1
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18

    "on key press"

    How to know when a key from the keyboard is pressed ?

    (PS: i cannot use the return key, just ONE key)

    Thx in advance

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    OS and compiler?

    I know you're new, but please try to give all relevant information when asking a question.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    sorry very much

    Windows XP and Dev-C++(Mingw compiler)

  4. #4
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    look up the kbhit( ) function.. it is a boolean function that returns true whenever there is a keypress. It may be non-standard though but it works great.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  5. #5
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    Thx

    I've tried this


    while(!kbhit('c')) printf( "u didn't press c"); and it works

    Does it work also for a combo like CTRL + Z ? If Yes, how ?
    Last edited by delphi; 03-17-2005 at 03:43 PM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Look at the kbhit() function again. It doesn't expect you to pass anything to it.
    If you understand what you're doing, you're not learning anything.

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    good call.. i was hoping you'd notice
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Part 5 of my Console Programming Tutorial covers keyboard events both blocking and non-blocking, and demonstrates techniques for trapping Alt etc.

    The I/O is C++ style, but if you adjust the headers and replace the cout's with printf(), the example will work. The API routines are language neutral.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Wanna-be :P delphi's Avatar
    Join Date
    Mar 2005
    Location
    Torino - Italy
    Posts
    18
    thank you so much all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 09:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21