Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Inkey$ question...

MalverMalver Posts: 8Member
Is there anyway to use Inkey$ to make smooth keyboard input (as well as multiple keys down at once)? If not, is there another way to achieve this in Qbasic?

Thanks in advance,

- Malver
«1

Comments

  • WaltPWaltP Posts: 335Member
    : Is there anyway to use Inkey$ to make smooth keyboard input (as well as multiple keys down at once)? If not, is there another way to achieve this in Qbasic?
    :
    : Thanks in advance,
    :
    : - Malver
    :
    Please define your request better. Smooth keyboard input??
    What do you want to do with mutiple keys at once? What type of keys?

    Examples and code will help explain what you want.

    ----------------
    Walt


  • MalverMalver Posts: 8Member
    : Please define your request better. Smooth keyboard input??
    : What do you want to do with mutiple keys at once? What type of keys?

    Keyboard keys? I need to be able to detect if a key is down or not. Is that clearer?

    : Examples and code will help explain what you want.

    I can't provide an example if I don't know how to do it!
  • TBushmakerTBushmaker Posts: 9Member
    INKEY$ means INput a single KEYstroke from the keyboard, no "return" or "enter" is necessary to terminate this function.

    ?Smooth Keyboard Input? What does this mean?

    ?Multiple keys down? Such as + to do something?

    : Is there anyway to use Inkey$ to make smooth keyboard input (as well as multiple keys down at once)? If not, is there another way to achieve this in Qbasic?
    :
    : Thanks in advance,
    :
    : - Malver
    :

  • WaltPWaltP Posts: 335Member
    : : Please define your request better. Smooth keyboard input??
    : : What do you want to do with mutiple keys at once? What type of keys?
    :
    : Keyboard keys? I need to be able to detect if a key is down or not. Is that clearer?

    Not really. Your request can be interpreted in many ways:

    easy: I need to know if a key has been pressed and what key it was -- standard keyboard function

    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.

    impossible: I need to know that the 'U' and 'K' keys are pressed at the same time to set the detinator and blow up the computer.

    I've learned not to make assumptions because many people post very strange requests.


    ----------------
    Walt


  • MalverMalver Posts: 8Member
    : 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.
  • tlmcduffietlmcduffie Posts: 9Member
    : : 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 "*";
    10 k$=inkey$
    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 !!!
    198 pdown%=down%:pover%=over%
    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:

    10 k$=inkey$
    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
    30 kr$=right$(k$,1)
    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

    23 kr$=right$(k$,1)

    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!!

  • mercman2000mercman2000 Posts: 80Member
    On your arrow keys example, I noticed you just did HPMK for detection. The first example on arrow keys I saw utilized CHR$(0) + "H" for the up arrow key. You said there's two bytes that are sent back. Is the CHR$(0) the first one? I'm assuming doing it this way will let the program interpret the keypress as the actual up arrow, and it won't get it confused with the user pressing the actual letter H (or h, as the case may be). I could be wrong, but that's my take on it. You probably want to modify the program as such so there isn't confusion, and you have those keys left open for usage.

    : 10 k$=inkey$
    : 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
    : 30 kr$=right$(k$,1)
    : 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

    An alternate structure you can use (no line numbers) is the following:

    DO
    kbd$ = INKEY$
    IF kbd$ = CHR$(0) + "H" THEN PRINT "Up Arrow"
    IF kbd$ = CHR$(0) + "P" THEN PRINT "Down Arrow"
    IF kbd$ = CHR$(0) + "K" THEN PRINT "Left Arrow"
    IF kbd$ = CHR$(0) + "M" THEN PRINT "Right Arrow"
    IF kbd$ = CHR$(27) THEN EXIT DO
    LOOP
    END

    The CHR$(27) is the ESC key on your keyboard. Press it to exit from this code.
  • tlmcduffietlmcduffie Posts: 9Member
    I have to admit the DO structure along with checking chr$(0) probably looks cleaner and is fewer lines of code than the example I supplied. It is just a matter of preference as to the style the programmer chooses.

    To answer your question on how the program differentiates the arrows from other keystrokes, note how the first program checks for the length: len(k$)=2. If the length is NOT 2, then an ordinary key, other than an arrow was pressed. Also note that the first program does not evaluate any keystroke until a non-null character is detected, at which time only the second byte is checked. The DO structure, however, is repeatedly performing five IF's, each checking 2 bytes and additionally invoking the chr$ function 5 times on every pass through the loop. Considering this I must argue the latter may not be quite as fast or efficient.

    Thanks for the feedback!!

  • MalverMalver Posts: 8Member
    : 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.
    [snip code]
    :
    : Hope this helps!! Good Luck!!

    I tested out the code that you provided, and it does not satisfy what I specified that I needed.

    Take your asterisk-moving program for example. If you hold down the right arrow key, you will move to the right once, and then move continuously to the right after a moment. I want to be able to have it move continuously as soon as the key is pressed, as opposed to what you have posted here.
  • mercman2000mercman2000 Posts: 80Member
    The following code is really sensitive to keyboard presses.

    This responds to the right arrow key only

    CLS
    DO
    LOCATE 1, 1
    IF INP(&H60) = 77 THEN x = x + 1: PRINT x
    LOOP

    A quick keypress on this system yields a loop of 1800+ times, demonstrated by x.

    Or, is this what you are after:

    CLS
    LOCATE 1, 1
    PRINT "*"
    row = 1
    column = 1
    action$ = "null"
    DO
    kbd$ = INKEY$
    IF kbd$ = CHR$(0) + "M" THEN action$ = "right"
    IF kbd$ = CHR$(27) THEN action$ = "null"
    SELECT CASE action$
    CASE "null"
    CASE "right"
    'LOCATE [row%] [,[column%]
    LOCATE row, column: PRINT " "
    column = column + 1
    IF column > 80 THEN column = 1
    LOCATE row, column: PRINT "*"
    FOR z = 0 TO 25000: NEXT z
    END SELECT
    LOOP

    Press the right arrow key to start things, press esc to stop it at any time.
«1
Sign In or Register to comment.