Thread: Moving keys ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

    Moving keys ...

    (I accidently put this in the C++ board even though I'm using C to write it ... whooops)

    Hi, I'm making a program which takes the keypad inputs and moves around an 8x8 array. I want it so that if the user presses, I dunno, "f" it'll store that array location, and if the press "t" or something it'll store that location. The characters used are arbitary ... (want the code?)

    I had something like this tho:


    Code:
    Code:
    			if ((ch = getch()) == 'f'			{
    				printf ("\nSaved Data: %d%d\n", up, accross);
    				system ("PAUSE");
    			}

    but that didn't work. Obviously this doesn't save the data, I just wanted to see if the algorithm would work... (EDIT well it did work sometimes, but not reliably)

    (I'm using the second last code in this:

    http://faq.cprogramming.com/cgi-bin...5&id=1043284392)

  2. #2
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    I don't understand what you wanna do.

    Am I right in thinking you want to store the array in a certain position in the memory, determined by which key the user presses? In that case you'd have to change the value of the pointer which points to the 1st [0] position of your array...

    What are up and across?

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you could at least post something which compiles, and doesn't work reliably.

    > printf ("\nSaved Data: %d%d\n", up, accross);
    If you're printing this to a file, and hoping to read it back again, the lack of spaces between %d%d is a killer

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    This is the code:

    Code:
    #include <conio.h>
    #include <stdio.h> 
    #include <limits.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    #include <time.h> 
    
    
    int main ( void )
    {
    	system("color f0");
    	system ("title MoveAry");
    	system("mode con:cols=17 lines=15");
    	srand(0);
    	char array[8][8];
    	char yarra='a';
    	int up=2;
    	int accross=2;
    	int row, col;
    	short esc = 0;
    	
    	HANDLE hConsole;
    	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	for (row=0; row<8; row++)
    	{
    		for (col=0; col<8; col++)
    		{
    			array[row][col]=yarra;
    			yarra++;
    		}
    	}
    
    	for (row=0; row<8; row++)
    	{
    		for (col=0; col<8; col++)
    		{
    			if (col==up && row==accross)
    			{
    				SetConsoleTextAttribute(hConsole, 252);
    				printf (" %c", (char)0x0f);
    				SetConsoleTextAttribute(hConsole, 240);
    			}
    			else 
    			{
    				SetConsoleTextAttribute(hConsole, 240);
    				printf (" %c", array[row][col]);
    			}
    		}
    		printf ("\n");
    	}
    	printf ("\n\t%d%d", up, accross);
    
    
    	while ( !esc ) 
    	{    
    		esc = GetAsyncKeyState ( VK_ESCAPE );
    
    		if ( GetAsyncKeyState ( VK_UP ) & SHRT_MAX )
    		{
    			if (accross-1<0)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else 
    			{
    				accross=accross-1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    
    		}
    
    		else if ( GetAsyncKeyState ( VK_DOWN ) & SHRT_MAX )
    		{
    			if (accross+1>7)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else
    			{
    				accross=accross+1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    		}
    
    		else 
    		if ( GetAsyncKeyState ( VK_LEFT ) & SHRT_MAX )
    		{
    			if (up-1<0)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else 
    			{
    				up=up-1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    		}
    
    		else if ( GetAsyncKeyState ( VK_RIGHT ) & SHRT_MAX )
    		{
    			if (up+1>7)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else
    			{
    				up=up+1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    		}
    	}
    	return EXIT_SUCCESS;
    }

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    It works for moving a character about an array, but it doesn't change the array, it just prints over the array. I want to be able to save the cell reference which the character is printed over.

    any ideas how to do it?

  6. #6
    Logic Programmer logicwonder's Avatar
    Join Date
    Nov 2005
    Location
    Kerala, India
    Posts
    52
    Explain your requirements with an example?
    L GIK wins!!!
    Salutes from logicwonder
    Enjoy programming

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    right the program prints an 8x8 array. There is a special character which moves around the array when you press the direction keys. I want it so that if I press something, a designated character or number, for example, it'll store what the special character is over ... understand? Sorry for being so unclear before

  8. #8
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    do you wanna move the character around the array yourself, or should it move of it's own random free will?

    why don't you just use two variables that always hold the position of the character, and are constantly being updated as the special character moves about? that way when you press your store key, you can just copy the values of those two variables at that moment...

  9. #9
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    See the code does that, but the problem I'm having is that I can't get it into that special state eloquantly. If you run the program it prints the array and when you move the position, it updates the position below the array.

    Is there a way so that if I don't press the arrow keys and I do press say "f", it'll save the location to two variables, one for the colume and one for the row? If I don't press "f" it'll continue moving the away

  10. #10
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Code:
    char ch; //add into variable initialisation
    
    // add the following just before the endof the "while( ! esc) { }"
    
    		if ((ch = getch()) == 'f')
    		{
    			printf ("\nSaved Data: %d%d\n", up, accross);
    			system ("PAUSE");
    		}
    
    //the following is the end of the said while(){}
    }
    return EXIT_SUCCESS;
    nifty little program this

  11. #11
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    But it doesn't work

    nifty ef :P lol

  12. #12
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by twomers
    But it doesn't work

    nifty ef :P lol
    works for me:

    in your first post you forgot a closing bracket after the 'f'...

    Code:
    #include <conio.h>
    #include <stdio.h> 
    #include <limits.h> 
    #include <stdlib.h> 
    #include <windows.h> 
    #include <time.h> 
    
    
    int main ( void )
    {
    	system("color f0");
    	system ("title MoveAry");
    	system("mode con:cols=17 lines=15");
    	srand(0);
    	char array[8][8];
    	char yarra='a';
    	char ch;
    	int up=2;
    	int accross=2;
    	int row, col;
    	short esc = 0;
    	
    	HANDLE hConsole;
    	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    
    	for (row=0; row<8; row++)
    	{
    		for (col=0; col<8; col++)
    		{
    			array[row][col]=yarra;
    			yarra++;
    		}
    	}
    
    	for (row=0; row<8; row++)
    	{
    		for (col=0; col<8; col++)
    		{
    			if (col==up && row==accross)
    			{
    				SetConsoleTextAttribute(hConsole, 252);
    				printf (" %c", (char)0x0f);
    				SetConsoleTextAttribute(hConsole, 240);
    			}
    			else 
    			{
    				SetConsoleTextAttribute(hConsole, 240);
    				printf (" %c", array[row][col]);
    			}
    		}
    		printf ("\n");
    	}
    	printf ("\n\t%d%d", up, accross);
    
    
    	while ( !esc ) 
    	{   
    		
    		esc = GetAsyncKeyState ( VK_ESCAPE );
    
    		if ( GetAsyncKeyState ( VK_UP ) & SHRT_MAX )
    		{
    			if (accross-1<0)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else 
    			{
    				accross=accross-1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    
    		}
    
    		else if ( GetAsyncKeyState ( VK_DOWN ) & SHRT_MAX )
    		{
    			if (accross+1>7)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else
    			{
    				accross=accross+1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    		}
    
    		else 
    		if ( GetAsyncKeyState ( VK_LEFT ) & SHRT_MAX )
    		{
    			if (up-1<0)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else 
    			{
    				up=up-1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    		}
    
    		else if ( GetAsyncKeyState ( VK_RIGHT ) & SHRT_MAX )
    		{
    			if (up+1>7)
    			{
    				printf ("\rCan't do that!!");
    			}
    			else
    			{
    				up=up+1;
    				system ("CLS");
    				for (row=0; row<8; row++)
    				{
    					for (col=0; col<8; col++)
    					{
    						if (col==up && row==accross)
    						{
    							SetConsoleTextAttribute(hConsole, 252);
    							printf (" %c", (char)0x0f);
    							SetConsoleTextAttribute(hConsole, 240);
    						}
    						else 
    						{
    							SetConsoleTextAttribute(hConsole, 240);
    							printf (" %c", array[row][col]);
    						}
    					}
    					printf ("\n");
    				}
    			printf ("\n\t%d%d", up, accross);
    			}
    		}
    		
    			if ((ch = getch()) == 'f')
    			{
    				printf ("\nSaved Data: %d%d\n", up, accross);
    				system ("PAUSE");
    			}
    	}
    	return EXIT_SUCCESS;
    }

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Yay, it does work!!!! thanks a lot!!!

  14. #14
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    stupid brace error ... lol. Before that I was puttint the other thing in the wrong place, just after it moved the piece, meh, this makes it shorter than it would have been beforehand. Any ideas how to make it shorter :P

  15. #15
    Cached User mako's Avatar
    Join Date
    Dec 2005
    Location
    Germany.Stuttgart
    Posts
    69
    Quote Originally Posted by twomers
    stupid brace error ... lol. Before that I was puttint the other thing in the wrong place, just after it moved the piece, meh, this makes it shorter than it would have been beforehand. Any ideas how to make it shorter :P

    nope sorry, can't help you ther. I don't even understand half the code, it's using routines I haven't started using yet... I'm glad it works so far

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simulate Keys with a Keyboard Hook
    By guitarist809 in forum Windows Programming
    Replies: 3
    Last Post: 11-14-2008, 08:14 PM
  2. blocking or passing keys with global hook
    By pmouse in forum Windows Programming
    Replies: 4
    Last Post: 08-29-2007, 02:54 PM
  3. 3D moving
    By bluehead in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2005, 05:46 AM
  4. Movement with arrow keys
    By louis_mine in forum C Programming
    Replies: 3
    Last Post: 02-06-2005, 04:35 PM
  5. Interfacing with arrow keys...
    By adityakarnad in forum Game Programming
    Replies: 1
    Last Post: 08-30-2003, 10:25 PM