Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Qbasic key repeat  (Read 7760 times)

0 Members and 1 Guest are viewing this topic.

mickster9

    Topic Starter


    Rookie

    Qbasic key repeat
    « on: September 22, 2008, 11:54:44 AM »
    Im making a game in qb where the arrow keys control a sprite on screen. I currently use: k$ = INKEY$. Then check k$ to change to x/y coordinates of the sprite by +/- 10.
    The problem is that when any direction is held down the sprite will move very quickly in that direction. I want it so that when an arrow key is pressed, the sprite will move once, then the key must be released for it to move againn in that direction.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: Qbasic key repeat
    « Reply #1 on: September 22, 2008, 12:32:37 PM »
    you'll need to create a "flag" variable, such as "LASTKEY", you would set this variable to the value of K$ at the end of the game loop (or equivalent). When your comparing the values of K$ to determine which direction to move, you make sure that the current value of LASTKEY is "", which would mean that this is the first time the key was held down.
    I was trying to dereference Null Pointers before it was cool.

    mickster9

      Topic Starter


      Rookie

      Re: Qbasic key repeat
      « Reply #2 on: September 22, 2008, 01:45:46 PM »
      Ive tried but i still cant get it to work.
      Heres how the main loop of the game is structured:
      Code: [Select]
      DO
      ...
      ...
      ...
       k$ = INKEY$

       IF k$ = CHR$(27) THEN END
       IF k$ = CHR$(0) + CHR$(72) THEN y = y - 10
       IF k$ = CHR$(0) + CHR$(80) THEN y = y + 10
       IF k$ = CHR$(0) + CHR$(75) THEN x = x - 10
       IF k$ = CHR$(0) + CHR$(77) THEN x = x + 10
      ...
      ...
      ...
      LOOP

      Can anyone show me how exactly to get it to work with this code.

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Qbasic key repeat
      « Reply #3 on: September 22, 2008, 02:11:33 PM »
      basically, you only want to handle a keystroke if the previously detected key was "" (or no key)
      That is- as long as they push a key, it will not continue with the symptoms you describe.


      try this- LASTKEY$ holds the key from the previous iteration.
      Code: [Select]
      DO
      ...
      ...
      ...
       k$ = INKEY$
       IF LASTKEY$="" THEN
       IF k$ = CHR$(27) THEN END
       IF k$ = CHR$(0) + CHR$(72) THEN y = y - 10
       IF k$ = CHR$(0) + CHR$(80) THEN y = y + 10
       IF k$ = CHR$(0) + CHR$(75) THEN x = x - 10
       IF k$ = CHR$(0) + CHR$(77) THEN x = x + 10
      END IF

      LASTKEY$=K$
      ...
      ...
      ...
      LOOP


      I'm a little rusty with QuickBasic, but I'm pretty sure INKEY$ returns an empty string when no key is pressed.
      I was trying to dereference Null Pointers before it was cool.

      mickster9

        Topic Starter


        Rookie

        Re: Qbasic key repeat
        « Reply #4 on: September 22, 2008, 02:24:29 PM »
        Thankyou I was correct in understanding the first time after all but it doesn't work.
        Any clue as to why?
        And according to the help file: INKEY$ returns a null string if there is no character to return.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Qbasic key repeat
        « Reply #5 on: September 22, 2008, 02:29:46 PM »
        null string and "" are the same thing. Looks like I'm gonna have to fire up my old laptop and figure this out.
        I was trying to dereference Null Pointers before it was cool.

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: Qbasic key repeat
        « Reply #6 on: September 24, 2008, 06:57:07 AM »
        I tried to get it to work, but for some reason, it doesn't work. Maybe a Null String is different then an empty one in QB.


        One possible solution would be to use "ON KEY" events.
        I was trying to dereference Null Pointers before it was cool.

        mickster9

          Topic Starter


          Rookie

          Re: Qbasic key repeat
          « Reply #7 on: September 24, 2008, 10:19:56 AM »
          Thanks for trying anyway. ill just have to keep trying and maybe i could use 'on key'

          BC_Programmer


            Mastermind
          • Typing is no substitute for thinking.
          • Thanked: 1140
            • Yes
            • Yes
            • BC-Programming.com
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 11
          Re: Qbasic key repeat
          « Reply #8 on: September 24, 2008, 10:26:41 AM »
          Oh wait a second- is this QBASIC, or QuickBasic?
          I was trying to dereference Null Pointers before it was cool.

          mickster9

            Topic Starter


            Rookie

            Re: Qbasic key repeat
            « Reply #9 on: September 24, 2008, 11:05:49 AM »
            I am using qbasic 1.1 now but i may also have quickbasic 4.5 to use if it helps

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: Qbasic key repeat
            « Reply #10 on: September 24, 2008, 11:49:41 AM »
            I don't know about QBASIC having ON KEY, I haven't used that in a VERY long time- QuickBasic 4.5 likely has it, however.

            Generally, you have ON KEY (keycode) GOTO (label)

            and then you have the code for that keycode- I believe the exact syntax is presented in the online help.

            On the other hand, Now that I think about it- there is no way to perform the flag testing I mentioned beforehand, so I'm not sure how that would work...


            I just did, however, recently recall what a "null string" means in QB- Instead of comparing to an empty string "" as I did, I believe one would have to use VarPtr or StrPtr or a similiar function, to determine that it's pointer isn't 0.


            I apologize for my vagueness, I haven't done QB very much in over 3 or 4 years. If I get time I might check this out myself, but I'm almost positive there will be some use of a Pointer-type function.
            I was trying to dereference Null Pointers before it was cool.

            mickster9

              Topic Starter


              Rookie

              Re: Qbasic key repeat
              « Reply #11 on: September 25, 2008, 02:36:13 PM »
              I still havent got sorted yet...
              From what i can find out searching the internet a null string in qbasic/quickbasic is the same as an empty string or ""
              Ive investigated VarPtr and it seems that it returns the memory offset of a variable which remains the same regardless of the value of the variable. ie i cannot use it to check when the string is null as it will not change.
              I dont know of any pointer functions and could not find anything else in the help index. I also think ON KEY events will not help because it does not solve the problem only provides another way of retrieving the input..

              BC_Programmer


                Mastermind
              • Typing is no substitute for thinking.
              • Thanked: 1140
                • Yes
                • Yes
                • BC-Programming.com
              • Certifications: List
              • Computer: Specs
              • Experience: Beginner
              • OS: Windows 11
              Re: Qbasic key repeat
              « Reply #12 on: September 25, 2008, 05:01:26 PM »
              This seems like such a simple problem. We must be overlooking something obvious here.

              Your definitely right about the ON KEY only providing a different method of input-


              In the interim, what you could do is programmatically change the typematic rate for the duration of your program- it won't stop the repeat, but you can greatly reduce it.
              I was trying to dereference Null Pointers before it was cool.

              alexskywalker



                Greenhorn

                Re: Qbasic key repeat
                « Reply #13 on: May 16, 2009, 11:29:13 AM »
                I used the method of solving this problem as stated at the beginning of the thread, and it worked just fine for me. my program does not work, however, this is due to graphics issues.

                mickster9

                  Topic Starter


                  Rookie

                  Re: Qbasic key repeat
                  « Reply #14 on: May 17, 2009, 02:52:36 PM »
                  wow i was suprised to see this resurrected.
                  I gave up with qbasic a long time ago but i'm still interested...

                  Could you show me at least part of your code if it works for you.
                  Also which program version are you running and on what system?

                  I'm also happy to help with your graphics issues if you need it.