Thread: get key?

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    47

    get key?

    ok is the a way do do something like:
    Code:
    if(someone presses the up arrow)
    {than do this}
    i know i use and if and i need to do something like the ketkey were if the user presses a key than the program will do some stuff

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    I think you might want to look into Windows programming to do that, using the Windows procedure to peek for a message from the keyboard.

    In just standard C++ for console apps, you can't do this...unless you're waiting for the user to press the ENTER key.

  3. #3

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    47
    that helps alot

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    If your working with a Windows console this might be of some use. In combination with Virtual-Key Codes I think you could figure it out.

    Regards,
    Brian
    Code:
    #include <windows.h>
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    WORD scanKeyboard(void)  
    {
    	DWORD dwCharsRead;
    	INPUT_RECORD inRec;
    	HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
    	 
    	while(ReadConsoleInput(hInput, &inRec, 1, &dwCharsRead))  
    		if(inRec.EventType == KEY_EVENT && inRec.Event.KeyEvent.bKeyDown)
    			return(inRec.Event.KeyEvent.wVirtualKeyCode);
    			
    	return('ì'); // dummy return to suppress warning msg.
    }
    
    
    int main( int argc, char * argv[] )
    {	
    	WORD c;
    	
    	do
    	{       
              cout << "\nEnter something, N to exit: ";
              c = scanKeyboard( );
              cout << c;
    	  if(c == VK_UP)
    		cout << " : Up Arrow key pressed";
    	}while(c !='N');
    
    	return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10: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