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!

Question

Status
Not open for further replies.

proftoy

Instructor
Dec 21, 2004
4
0
0
US
Hello,
Does anyone know the keyboard map for all the keys to use with the onkey command ? I tried to use the same onkey codes with qbasic as I used to use with gwbasic. I need the codes for the arrow keys (seperate pad not numeric keys). Can anyone help ?

 
Check the help. It is pretty good in this area.
 
I don't know the code to use with the onkey funtion but maybe this could help

***** P = down, H = up, K = left, M = right *****^
Code:
k$ = INKEY$
If k$ = Chr$(0) + "M" Then
   rem right Arrow
   rem ...
end if
If k$ = Chr$(0) + "K" Then
   rem left arrow
   rem ...
end if
If k$ = Chr$(0) + "H" Then
   rem up arrow
   rem ...
end if
If k$ = Chr$(0) + "P" Then
   rem down arrow
   rem ...
end if

SkyFighter
skyfighta@hotmail.com
Get the fun out of life!

 

Programmers,
OK, the qbasic.hlp helped a lot. Thank you. Now I have another problem when I continue to use the arrow keys I get an out of stack space error. How can I supress this ?
Here is the code:

' Findkey.bas used to find what key is pressed

DEFINT A-Z
CLS
start:
KEY(11) ON
KEY(12) ON
KEY(13) ON
KEY(14) ON

ON KEY(11) GOSUB a
ON KEY(12) GOSUB b
ON KEY(13) GOSUB c
ON KEY(14) GOSUB d
GOTO start
a:
PRINT "up"
GOTO start
b:
PRINT "left"
GOTO start
c:
PRINT "right"
GOTO start
d:
PRINT "down"
GOTO start
 
From a GOSUB, you have to use a RETURN statement. That's what's causing the stack error.

Lee
 
lived's example is MUCH cleaner and more in line with modern coding style. The old GOSUB is left over from pre-Q days and is included to make it compatible with older code.

Lee
 
Thank you, for all your help it was valuable, I guess the best way to learn is to get both feet in, and try. The code is old, but hey it works ! Lee you hit the nail on the head ! Return of course !
 
gosub is still used a lot.

Rather than using on key, you might just capture the key and process it using inkey$ for example.

theloop:
a$=inkey$
if len(a$)=2 then gosub process.control.input:goto theloop
if a$=chr$(8) then 'backspace
.
.
end if
if a$=chr$(13) then '$CR
.
.
end if
process.control.input:
if left$(a$,1)=chr$(0) then
select case right$(a$,1)
case ...
end select
end if
return

 
I don't know why you would use GOSUB instead of processing all the items into 1 select statement. As trollacious said, GOSUB are not used anymore, it is only left out for compatibility, since you can do better with SELECT and SUB statements, and it takes more memory than needed for something you could easily do another way. It's not because you use it a lot that it should be. Keep it simple and efficient.

Code:
CLS
DO
    k$ = INKEY$
    SELECT CASE k$
    CASE CHR$(27)
[COLOR=red]        PRINT "escape key"
        INPUT "do you wanna exit (Y or N)", yn$
        IF UCASE$(left$(yn$,1)) = "Y" THEN END[/color]
    CASE CHR$(8)
[COLOR=red]        PRINT "backspace key"[/color]
    CASE CHR$(13)
[COLOR=red]        PRINT "enter key"[/color]
    CASE CHR$(0) + "P"
[COLOR=red]        PRINT "down arrow"[/color]
    CASE CHR$(0) + "H"
[COLOR=red]        PRINT "up arrow"[/color]
    CASE CHR$(0) + "K"
[COLOR=red]        PRINT "left arrow"[/color]
    CASE CHR$(0) + "M"
[COLOR=red]        PRINT "right arrow"[/color]
    END SELECT
LOOP

SkyFighter
skyfighta@hotmail.com
Get the fun out of life!

 
you should investigate Inp(96) (aka Inp(&60))

you might want to look at...

thread314-709202

or

thread314-431683

>> seperate pad not numeric keys

*Note: the keys are actually mapped the same... the only difference is the NumLock key status...

both sets of keys return the following ScanCodes:
72 - Up
75 - Left
77 - Right
80 - Down


Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top