Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arrow-keys coding - how to use them to move shapes? 2

Status
Not open for further replies.

cluelessC

Programmer
Jul 20, 2001
10
0
0
GB
Hello, everybody!
Does anybody know how to use LeftArrow, UpArrow, etc. keys to move a shape around the screen? How do you identify them in the code to program the responces? From the Help system I gathered that I should include <ctype.h> to be able to use the &quot;iscntrl&quot; function, which, apparently, identifies Control keys. Am I on the right track?
Still, where does the ASCII code come into play?
Would be very grateful for any piece of code with annotations! Thank you in anticipation!

cluelessC
 
so make such program:
#include<conio.h>
#include<stdio.h>
int main()
{
for(int i=0;i<256;i++)
{
cout<<i<<&quot; &quot;<<(char)i<<endl;
}
return 0;
}//you will output all symbols with all theirs codes.
If you want to handle key press use kbhit()/getch(). For arrows/F1-FX, some othr keys getch() will return 0 what means a functional key. In this case you should kall getch again to see what key is pressed. To see keyboard state see in bios.h keyboard functions. They work at low level, but give more information. John Fill
1c.bmp


ivfmd@mail.md
 
There is also some info at thread206-7673 . James P. Cottingham

I am the Unknown lead by the Unknowing.
I have done so much with so little
for so long that I am now qualified
to do anything with nothing.
 
Thanks very much,
I've seen the incredible variety of symbols using John's code (with very slight modifications - total beginners like me forget the vital getchar() to hold the output!). Useful reference, thanks, John! Unfortunately, I still need to learn how to distingwish, say, numeral &quot;32&quot; from a key-code &quot;32&quot; in my WIN32 program. And, sadly, the kbhit() does not work with GUI apps, or so the &quot;Help&quot; says...
Will definitely try the snippets of code from the thread recommended by James. Cheers, mate! P.S. I like your abstract poetry!;-)

cluelessC

 
Hello, again!
It's me, cluelessC! It might be a bit late, but I decided to come back with the answer to my own question - for the benefit of beginners, like myself. Here is the code that I was after:

void __fastcall TForm1::OnKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
switch (Key)
{
case (VK_DOWN): Shape1->Top = Shape1->Top + 1; break;
case (VK_UP): Shape1->Top = Shape1->Top - 1; break;
case (VK_LEFT): Shape1->Left = Shape1->Left - 1; break;
case (VK_RIGHT): Shape1->Left = Shape1->Left + 1; break;
}
}

Notice that the code is for the 'OnKeyDown' event, and not the 'OnKeyPress' one. If the Shape1 is your shape then it will be moving as the user presses the arrow-keys on the keyboard. It was so simple in the end, wasn't it? I just didn't realise that there was a difference between the ASCII key-codes and the virtual key codes that are used here. Oh, and do remember to set the form's 'KeyPreview' property to true!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top