1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Reading pushed buttons like (page up , Up Arrow etc)

Discussion in 'C' started by netpumber, Apr 19, 2012.

  1. netpumber

    netpumber New Member

    Joined:
    Nov 10, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hello.

    I have this code to read input from console.

    Code:
    main()
    {
    
    HANDLE         stdinInput = 0;
    DWORD          numEvents = 0;
    DWORD          numEventsRead = 0;
    DWORD		   numReceivedRecords = 0;
    DWORD		   fdwSaveOldMode;
    DWORD          fdwMode;
    
    
    char   dataBuffer[100];
    int    bufferLen = 0;
    
    
    stdinInput = GetStdHandle(STD_INPUT_HANDLE);
    
    if (stdinInput == INVALID_HANDLE_VALUE) 
            perror("GetStdHandle");
    
    if (! GetConsoleMode(stdinInput, &fdwSaveOldMode) ) 
            perror("GetConsoleMode");
    
    fdwMode = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
    if (! SetConsoleMode(stdinInput, fdwMode) ) 
            perror("SetConsoleMode"); 
    
    
    for(; ; )
    {
     
       
       GetNumberOfConsoleInputEvents(stdinInput, &numEvents);
      
       if (numEvents != 0) {
        
    	   INPUT_RECORD eventBuffer;
        
          ReadConsoleInputA(stdinInput, &eventBuffer, 1, &numEventsRead);
     
              
    		if (eventBuffer.EventType == KEY_EVENT) {
    
    			if(eventBuffer.Event.KeyEvent.bKeyDown)
    			{
    				if(eventBuffer.Event.KeyEvent.uChar.UnicodeChar != 0)
    				{
    					printf("%c",eventBuffer.Event.KeyEvent.uChar.UnicodeChar);
    					dataBuffer[bufferLen++] = eventBuffer.Event.KeyEvent.uChar.UnicodeChar;
    					dataBuffer[bufferLen]  = '\0';            
                                     
    					if ( dataBuffer[bufferLen] == 99 || eventBuffer.Event.KeyEvent.uChar.UnicodeChar == '\r' ) {
    					printf("User Wrote: %s\n",dataBuffer);
    				
    					memset(dataBuffer,0,sizeof(dataBuffer));
    					bufferLen = 0;
    					}
    				}
    				
    					
    			}
    		}
         
    	}
    }
    
    } 
    As you can understand , it prints out whatever you type after you press enter.

    Now i want to make it print "Up arrow pressed" when i press the up Arrow button but without pressing after the Enter.

    Can somebody help me with this ?

    Thanks in advnace.
     

Share This Page