Thread: Capturing key stokes

  1. #16
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    But shouldn't the function be returning a bool, e.g true or false?

    not necessarily, the if statement will result to false if the expression results to zero.

    Code:
    int num = 4;
    if(num)
    {
         cout << "hello";
    }
    That would print "hello" to the screen.

  2. #17
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Right, so this:
    Code:
     if(num)
    is basically a shortcut for this:
    Code:
    if(num == 4)
    ? I usually test conditions explictily.
    Be a leader and not a follower.

  3. #18
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    tests if the expression is zero or non-zero

  4. #19
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    this should clarify:

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int i=4;
    	int x=-4;
                    int y=0;
    	
    	if(i)
    	    cout << "hello";
        
    	if(x)
                        cout << "world";
    
    	if(y)
                        cout << "nothing";
    	
     	return 0;
    }

  5. #20
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    GetAsyncKeyState() on MSDN

    However, if you wanted to check for an ALT+something keypress in a console, try this:
    Code:
    #include <windows.h>
    #include <iostream>
    
    bool ALT(char key)
    {
    	HANDLE hInConsole = GetStdHandle(STD_INPUT_HANDLE);
    	INPUT_RECORD	Event, SecondEvent;
    	DWORD	NumberOfEvents, EventsRead, EventCounter, NumberOfSecondEvents, SecondEventsRead;
    	GetNumberOfConsoleInputEvents(hInConsole, &NumberOfEvents);
    
    	if (NumberOfEvents == 0)
    		return false;
    	
    	for (EventCounter = 0; EventCounter < NumberOfEvents; EventCounter++)
    	{
    		PeekConsoleInput(hInConsole, &Event, 1, &EventsRead);
    		if (Event.EventType == KEY_EVENT)
    		{
    			ReadConsoleInput(hInConsole, &Event, 1, &EventsRead);
    			if (Event.Event.KeyEvent.wVirtualKeyCode == VK_MENU)
    				while (Event.Event.KeyEvent.bKeyDown)
    				{
    					FlushConsoleInputBuffer(hInConsole);
    					GetNumberOfConsoleInputEvents(hInConsole, &NumberOfSecondEvents);
    					PeekConsoleInput(hInConsole, &SecondEvent, 1, &SecondEventsRead);
    					if (SecondEvent.EventType == KEY_EVENT && SecondEvent.Event.KeyEvent.bKeyDown)
    						if (SecondEvent.Event.KeyEvent.uChar.AsciiChar == key)
    							return true;
    				}
    		}
    		else
    			ReadConsoleInput(hInConsole, &Event, 1, &EventsRead);
    	}
    	return false;
    }
    
    int main()
    {
    	std::cout << "Press ALT+F to quit\n";
    	while(!ALT('f')) {}
    	std::cout << "Wait! Now press ALT+Q!\n";
    	while(!ALT('q')) {}
    	std::cout << "Hehehe, now press ALT+X and you may go. . .\n";
    	while(!ALT('x')) {}
    
    	return 0;
    }
    Works for me

  6. #21
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    367
    Right i'm with you, thanks for your help.
    Be a leader and not a follower.

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