: : hard: I need to know that a key is physically pressed and when it is released -- keyboard manipulation like used in a game. While the 'L' key is pressed, move something to the left. When released, stop.
: This would be what I'm after. Sorry for the lack of clarity.
The INKEY$ function polls the keyboard for a single keystroke and it happens in just an instant, then your program continues on to the next sequential statement (immediately following the INKEY$). It appears you want to develop an event-driven program, and this can be done using INKEY$. Here is an example of incrementing and decrementing OVER and DOWN coordinates for an 80 X 25 grid, using the keyboard keys of U, D, L and R. The ESC key (CHR$(27) will terminate the program.
This program will move an asterisk around anywhere on the screen. Please note the importance of tracking the previous coordinates for erasing the old asterisk prior to displaying the newly-located asterisk as a key is pressed. pover% means PreviousOverand pdown% means PreviousDown. Also note the editing that takes place on the coordinates to ensure you don't attempt to move out of the 80 X 25 spectrum.
Also note the comments beginning with the ' character. Hope these help.
I have also tested this program, so you should be able to just copy and paste it into a .BAS text file and compile, link and run it.
** NOTE **
Following this program listing are some instructions on how you can use the actual arrows on the keyboard to move the asterisk around. Dont miss this !!!!!!!!!!!!!!!!!!!!
5 over%=1:down%=1:pover%=1:pdown%=1 'start in upper left-hand corner
8 color 7,0:cls
9 locate 1,1:print "*";
15 if k$=chr$(27) then end 'end program if esc key pressed
20 if K$="" then 10 'if no key was down when inkey occurred, loop back
22 'to 10 for another key.
30 if k$="L" or k$="l" then over%=over%-1 'moving to left
40 if k$="R" or k$="r" then over%=over%+1 'moving to right
50 if k$="U" or k$="u" then down%=down%-1 'moving up
60 if k$="D" or k$="d" then down%=down%+1 'moving down
70 'make sure up and down are within 80 X 25 spectrum
80 if over%<1 then over%=1
90 if over%>80 then over%=80
100 if down%<1 then down%=1
110 if down%>25 then down%=25
120 'see if coordinates changed. if they didn't, either wrong key was
130 'pressed or coordinates attempted to go off the screen.
140 if (over%=pover%) and (down%=pdown%) then 10
150 'erase old asterisk, display new asterisk
160 locate pdown%,pover%
170 print " ";
180 locate down%,over%
190 print "*";
195 '!!! VERY IMPORTANT !!! Save the previous coordinates !!!
200 goto 10
!!!!!!! Here is how you can use the arrows !!!!!!!!!!!
Whenever certain keys are pressed, the inkey$ function returns a two-byte string instead of only one. Some of these keys include the up, down, left and right arrows. Whenever one of these keys is pressed, the second byte is checked. Example:
15 if k$="" then 10 'if no key is pressed, poll keyboard again.
20 if len(k$)=2 then 30
25 end 'any key pressed other than arrows will end program
40 if kr$="H" then print "UP ARROW"
50 if kr$="P" then print "DOWN ARROW"
60 if kr$="K" then print "LEFT ARROW"
70 if kr$="M" then print "RIGHT ARROW"
80 goto 10
Make the following changes to the ASTERISK moving program to utilize the arrows:
Insert this line
Change lines 30, 40, 50 and 60 to look as follows:
30 if kr$="K" then over%=over%-1 'moving to left
40 if kr$="M" then over%=over%+1 'moving to right
50 if kr$="H" then down%=down%-1 'moving up
60 if kr$="P" then down%=down%+1 'moving down
Save and recompile your program, then use the arrows instead of L,R,U D.
Hope this helps!! Good Luck!!