On Keypress

This is a discussion on On Keypress within the C++ Programming forums, part of the General Programming Boards category; I have searched for this everywhere. The FAQ answer, as far as I can tell, just replicates cin.get(). All I ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    On Keypress

    I have searched for this everywhere. The FAQ answer, as far as I can tell, just replicates cin.get().
    All I want is this: On a key press, ANY key press [NOT just enter], I want a specific function to be executed. Although it is fine if it only detects numbers, letters, and symbols, or even only letters.

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    maybe
    if cin.get()=='a'?
    You ended that sentence with a preposition...Bastard!

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Ok, that sort of works - I have to press a then enter, and it executes the function twice. All I need now is to remove the need for an enter press.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    2,360
    there is no standards-compliant way to do that. different hardware has different functionality, and there's no way to guarantee that such functionality exists on all hardware, so the standard library works with lines of text, terminated by '\n' or "\r\n" characters, instead of raw key presses. there are ways to do it on every platform. on unix/linux, you can try the ncurses library, and on windows there are built-in api functions for it.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Ok, I use windows, so I'm interested in these api functions.

    I've foung the System.Windows.Form class and its method OnKeyPress(). Just a question before I try it out - How do I use things from the Windows API? What header file do I use? do I use something other than namespace std?
    Last edited by mortalc; 03-15-2011 at 07:58 AM.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    2,360
    the System.Windows.Form class will do you very little good in C++, unless you're using the .Net variant C++/CLI.

    try this link

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (MFC, Visual C++) Keypress Detection in a dialog...
    By guitarist809 in forum Windows Programming
    Replies: 4
    Last Post: 08-31-2008, 01:13 PM
  2. Simulating keypress
    By Scarvenger in forum Windows Programming
    Replies: 21
    Last Post: 02-07-2008, 03:38 PM
  3. how to specify a specific KeyPress event?
    By Diablo02 in forum C# Programming
    Replies: 4
    Last Post: 11-20-2007, 07:50 PM
  4. Detecting keypress
    By dandago in forum C Programming
    Replies: 4
    Last Post: 06-10-2007, 09:34 AM
  5. Getting KeyPress
    By loko in forum C Programming
    Replies: 5
    Last Post: 07-13-2005, 12:43 PM

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