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

Code snippit: Basic button handler

Discussion in 'C, C++, C#' started by WinBoot, Sep 7, 2010.

  1. WinBoot

    WinBoot Registered Member

    Joined:
    Aug 25, 2010
    Messages:
    61
    Likes Received:
    14
    Don't ask me why i needed this, i was just amazingly bored, but it might be usefull for some people

    Code:
    /* 
    	Button Handler
    	By WinBoot
    */
    
    int mouse_x; /* You still have to define this */
    int mouse_y; /* You still have to define this */
    
    char *storedKeyNames[256] = {
    /* n° */	/* Name */
    /* 01 */	"LButton",
    /* 02 */	"RButton",
    /* 03 */	"Cancel",
    /* 04 */	"MiddleButton",
    /* 05 */	"Backspace",
    /* 06 */	"Tab",
    /* 07 */	"Shift",
    /* 08 */	"Control",
    /* 09 */	"Alt",
    /* 10 */	"Caps",	
    /* 11 */	"Space",
    /* 12 */	"Pageup",
    /* 13 */	"Pagedown",
    /* 14 */	"End",
    /* 15 */	"Home",
    /* 16 */	"Leftarrow",
    /* 17 */	"Uparrow",
    /* 18 */	"Rightarrow",
    /* 19 */	"Downarrow"
    };
    
    int storedKeyValues[256] = {
    /* n° */	/* ID */
    /* 01 */	(int) 0x01,
    /* 02 */	(int) 0x02,
    /* 03 */	(int) 0x03,
    /* 04 */	(int) 0x04,
    /* 05 */	(int) 0x08,
    /* 06 */	(int) 0x09,
    /* 07 */	(int) 0x10,
    /* 08 */	(int) 0x11,
    /* 09 */	(int) 0x12,
    /* 10 */	(int) 0x14,	
    /* 11 */	(int) 0x20,
    /* 12 */	(int) 0x21,
    /* 13 */	(int) 0x22,
    /* 14 */	(int) 0x23,
    /* 15 */	(int) 0x24,
    /* 16 */	(int) 0x25,
    /* 17 */	(int) 0x26,
    /* 18 */	(int) 0x27,
    /* 19 */	(int) 0x28
    };
    
    int getKeyIDByName( char *keyname ) {
    	int output = -1;
    	for ( int i = 0; i < 256; i++ ) {
    		if ( strcmp( keyname, storedKeyNames[ i ] ) ) {
    			output = storedKeyValues[ i ];
    			break;
    		}
    	}
    	return output;
    }
    
    bool buttonPress( char *keyname ) {
    	int tID = getKeyIDByName( keyname );
    
    	if ( ( tID != NULL ) && ( tID != -1 ) ) {
    		if ( GetAsyncKeyState( tID ) && 1 == 1 )
    			return true;
    		else
    			return false;
    	}
    	else
    		return false;
    }
    
    bool buttonPressRange( char *keyname, int x, int y, int w, int h ) {
    	if ( buttonPress( keyname ) ) {
    		if ( ( mouse_x > x ) &&
    		     ( mouse_x < x+w ) &&
    		     ( mouse_y > y ) &&
    		     ( mouse_y < y+h ) ) {
    			return true;
    		}
    		else
    			return false;
    	}
    	else
    		return false;
    }