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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you use the arrow keys? 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
When making a game, Instead of typing "r" then return to move right how do i use the arrow keys to move right?
is it something like
if a$ = "arrowkey" goto 10???????
i have no idea!!!
Thanks in advance for the help
 
they are very easy once you get used to them, here are the codes you want.

IF RIGHT$(a$,1) = "K" THEN GOTO xx 'left arrow
IF RIGHT$(a$,1) = "M" THEN GOTO xx ' right arrow
IF RIGHT$(a$,1) = "H" THEN GOTO xx 'up arrow
IF RIGHT$(a$,1) = "P" THEN GOTO xx 'down arrow


GOD BLESS AMERICA!
 
This respond is true, although it does miss the fact that the code received via INKEY$ for the arrow keys is actually CHR$(0) + the letter.

CHR$(0) + CHR$(72) = Up
CHR$(0) + CHR$(75) = Left
CHR$(0) + CHR$(76) = Middle Key (5)
CHR$(0) + CHR$(77) = Right
CHR$(0) + CHR$(80) = Down

(I use the numbers because that is how QBasic lists them in the help)
 
While godofcuboids's way does work, it would be easier to use my way. Here's why. The way he posts it you have to keep turning the keys on and off, my way you don't, I just think it would be easier to use my way, although his is perfectly fine and maybe better if you didn't need to turn the keys off.
 
This appears to be a two part question.

In a game, the player is not really expected to press the return key unless it's absolutely needed. Read up on the following commands in your help files: DO...LOOP, GOSUB (instead of GOTO--unless absolutely needed), and INKEY$.

As far as keys, well...do you plan on using letters (such as qbasicking suggests), the numeric keypad (as GodofCuboids suggests), or the extended arrow keys (usually between the character keys and the numeric keypad)???

--MiggyD It's better to have two heads to solve a problem from different angles than to have tunnel vision to a dead end.
 
to miggyd:

The way I have posted does NOT use letters. The way I have typed it in uses the arrow keys BETWEEN the alphabetical keypad and the numeric one. I don't know where you got the idea that I was using letters. Type up a program using my code your self and hit the arrors, they will work!

Try this little code:

CLS
PRINT "Press the space bar to exit program"
DO
WHILE a$ = ""
a$ = INKEY$
WEND
IF RIGHT$(a$,1) = "K" THEN PRINT "left arrow"
IF RIGHT$(a$,1) = "M" THEN PRINT "right arrow"
IF RIGHT$(a$,1) = "H" THEN PRINT "up arrow"
IF RIGHT$(a$,1) = "P" THEN PRINT "down arrow"
IF a$ = " " THEN END
LOOP


The keys used in my code are shown by asterisks here

BCKSPACE INSERT HOME PAGEUP NUMLOCK /
DELETE END PAGEDN 7 8
ENTER 4 5
SHIFT \ * 1 2
CRTL * * * 0
 
qbasicking:

If you see the original posting, the A$ is used solitarily which indicates that 'Lewis Green' is expecting a single byte character returned instead of a two-byte return--which is what 'GodofCuboids' is showing.

Although, I do not program for games (mostly business applications for antiquated systems--dare I say "cheap bosses"). I'd have to say that using RIGHT$ is very cleaver.

However, if the CAPS LOCK is ON or the SHIFT key(s) is/are pressed then someone else--let us say in dual player mode for example--can press either a 'k', 'm', 'h', or a 'p'. It will be interpreted as thought it was from the extended keys. Which is not good for the user at the arrow keys. So that is why I said your code is character based.

What 'GodofCuboids' has indicated is correct. A two-byte return is needed to verify that the actual scan code being returned IS from the extend arrow keys. Not simply a fluke that the CAPS LOCK or the SHIFT key was pressed.

Test it out. Use the arrow keys then press either SHIFT key or have the CAPS LOCK set to ON. You'll see.

--MiggyD It's better to have two heads to solve a problem from different angles than to have tunnel vision to a dead end.
 
I see now what you mean about the two-byte return, but I have to disagree with you about the CAPS and Shift. The code going into the computer is always capitaized, no matter what, so any of the locks (CAPS NUM SCROLL) can be on and my code will still work.
I did figure out, however that if k,m,h,p is typed in CAPS they will produce misleading results.

if he uses the code that godofcuboids posts, it would work, but all the the locks would have to be off, unless he programs the scan codes in for all of them.
 
TO qbasicking:

Here, try this with CAPS & SHIFT keys:

'***Starts Here***
CLS

PRINT "Press the space bar to exit program"

DO

WHILE A$ = ""
A$ = INKEY$
WEND

IF A$ = CHR$(0) + CHR$(75) THEN PRINT "left arrow"
IF A$ = CHR$(0) + CHR$(77) THEN PRINT "right arrow"
IF A$ = CHR$(0) + CHR$(72) THEN PRINT "up arrow"
IF A$ = CHR$(0) + CHR$(80) THEN PRINT "down arrow"
IF A$ = " " THEN END
A$ = ""

LOOP


'***Ends Here***

--MiggyD It's better to have two heads to solve a problem from different angles than to have tunnel vision to a dead end.
 
I don't know what you two are arguing about. For Lewis Green's application you are both right. But for numerous other applications, qbasicking, you could come to grief with your version.
 
Have you read the entire post?

We're not "arguing". We're discussing coding possibilities. As you know, there is usually an alternative way of getting something to do what you want it to do.

So, we're just stating opinions and looking at possibilities of pros and cons. But, we're still having a converstation. It's better to have two heads to solve a problem from different angles than to have tunnel vision to a dead end.
 
miggyd:
good show, I was thinking of the KEY statment when I said that you would have to program all of the keys for all of the locks.
pebe:
to reiterate what miggyd said, this is not an arguement, this is merely discussion of the best way to do something among programmers.

Lewis Green: For your program both miggyd's code and mine would work fine, it is up to you to decide. If you are using a slower computer though, i suggest using my code, for having used Miggyd's code on a 486 machine, i find that it is slower. If you are using a faster computer, use Miggyd's code, for if you hit a capital k,m,h,p using mine, it will give you the same reaction of the arrow keys, but for a simple qb game I don't know why use would be using the alphabetical keyboard.
 
Mr. Green:

I agree with qbasicking, on an older system, use his code-it's much less time consuming on the system when it has to compare just one byte instead of two.

If however, you later-on provide for a second player, you may have to accept the consequences of slowing down the game play and use the code that 'GodofCuboids' has offered.

--MiggyD

PS - QbasicKing, I still think your "quick" comparison is ingenious--probably cuz I hadn't thought of it before. s-)
It's better to have two heads to solve a problem from different angles than to have tunnel vision to a dead end.
 
Sorry if I offended anybody. If so I'll withdraw my flippant comment about an argument. And YES MiggyD, I did read the entire post!

My point was really to emphasise to Lewis Green and others who may not have your experience, that what appear to be shortcuts are not always the best invention since sliced bread. They can and do cause problems if they are applied ad-lib to other programs without realising their limitations.

In this 'discussion' I cannot see why, after getting INKEY$ into a$, interpreting a keypress as IF RIGHT$(a$,1) = "K" .... is any more compact in code than IF a$= CHR$(0) + CHR$(75)...... So why invite trouble?

To Lewis Green I would say, go with MiggyD's way - the right way!

 
No offence taken. I was bewildered as to what was said that made you believed there was an argument here. I understand your point now, and apologize if my question irritated you.

I too get upset when someone asks "how to fix this scrambled egg code (that was copied for 100's of other programs--none of which is my own work) so that it'll work for me so that I can claim credit and yet know nothing?"

My only suggestion to something such as this is to red-flag it (as I have in the past) OR just ignor it (done this often too).

--MiggyD It's better to have two heads to solve a problem from different angles than to have tunnel vision to a dead end.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top