Results 1 to 9 of 9

Thread: GetKeyDown and GetKeysDown

  1. #1
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default GetKeyDown and GetKeysDown

    Funnily enough, it's very hard to use IsKeyDown etc. in cases, so I though why not make something that runs through all the keys and stops when it finds a key that is down or in GetKeysDown, all the keys that are down. With these functions, it makes it very easy to use in cases. So, here we go:
    SCAR Code:
    function GetKeyDown: Char;
    var
      i: Integer;
    begin
      repeat
          for i:= 0 to 127 do
        if IsKeyDown(Chr(i)) then
          begin
            Result := Chr(i);
            Exit;
          end;
      until(IsKeyDown(Result))
    end;
    SCAR Code:
    function GetKeysDown: array of Char;
    var
      i: Integer;
    begin
      for i:= 0 to 127 do
        if IsKeyDown(Chr(i)) and IsKeyDown(Chr(i)) then
        begin
          SetArrayLength(Result,High(Result)+2);
          Result[High(Result)] := Chr(i);
        end;
    end;

    If you want to test them both (though more GetKeysDown), you can run the following script, and start typing to see the keys come up in the Debug box
    SCAR Code:
    program New;
    var
      i: Integer;
      CharsDown: array of Char;

    function GetKeyDown: Char;
    var
      i: Integer;
    begin
      repeat
          for i:= 0 to 127 do
        if IsKeyDown(Chr(i)) then
          begin
            Result := Chr(i);
            Exit;
          end;
      until(IsKeyDown(Result))
    end;

    function GetKeysDown: array of Char;
    var
      i: Integer;
    begin
      for i:= 0 to 127 do
        if IsKeyDown(Chr(i)) and IsKeyDown(Chr(i)) then
        begin
          SetArrayLength(Result,High(Result)+2);
          Result[High(Result)] := Chr(i);
        end;
    end;

    begin
      repeat
        ClearDebug;
        CharsDown:= GetKeysDown;
        for i:= 0 to High(CharsDown) do
          Writeln(CharsDown[i]);
        SetArrayLength(CharsDown,0);
        Wait(500);
      until (GetKeyDown = 'q')
    end.
    Which will run until you push down the 'q' button.

    Enjoy
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  2. #2
    Join Date
    Mar 2007
    Posts
    1,223
    Mentioned
    0 Post(s)
    Quoted
    5 Post(s)

    Default

    kwl!!...probli use this in my sccript but im not shurr...so if any of the key gets pressed down it stops?

  3. #3
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    If you're using GetKeyDown, then yes, if using GetKeysDown, then no. What it does is return the key/s that are currently pressed down, so you do
    "case KeyDown of
    'a': blah;
    'b': blah;
    etc."
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  4. #4
    Join Date
    Mar 2007
    Posts
    3,116
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    HOT! I've been needing this for a script you will see soon! Credits for you and a good function for me!

  5. #5
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    This would be better:
    SCAR Code:
    function GetKeyDown: Char;
    var
      i: Integer;
    begin
      for i:= 0 to 127 do
        if IsKeyDown(Chr(i)) then
          if IsKeyDown(Chr(i)) then
          begin
            Result := Chr(i);
            Exit;
          end;
    end;
     
    function GetKeysDown: array of Char;
    var
      i: Integer;
    begin
      for i:= 0 to 127 do
        if IsKeyDown(Chr(i)) then
          if IsKeyDown(Chr(i)) then
          begin
            SetArrayLength(Result,High(Result)+2);
            Result[High(Result)] := Chr(i);
          end;
    end;

    Why? There is some kind of "bug" which lets the last pressed button(s) be in some kind of buffer. Meaning that when you pres 'a' and run the script it would writeln A, although you didn't press it..
    To "defeat" this bug you should call the procedure twice, I've no clue why it works but somehow it clears the buffer for that char I think.. Try it
    Verrekte Koekwous

  6. #6
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    I tested it with my test script, and it "appeared" to work, so I'll go through it again and check.
    Edit: Did a bit more testing and it seems to hold a set of keys pressed from a bit in the buffer and not just the last - I was surprised to see something resembling "until(KeyDown = 'q')" appear in the debug box, so I did a bit more and it does appear to store a few of the keys in the buffer, though it stops after running through the function a few times. I guess as long as the GetKeyDown function isn't the first thing you use, it should be ok, unless you're typing as you do it, though I have tested it with Scar typing functions, but I guess it would work with them too...
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

  7. #7
    Join Date
    May 2006
    Location
    Amsterdam
    Posts
    3,620
    Mentioned
    5 Post(s)
    Quoted
    0 Post(s)

    Default

    Quote Originally Posted by mixster05 View Post
    I tested it with my test script, and it "appeared" to work, so I'll go through it again and check.
    Edit: Did a bit more testing and it seems to hold a set of keys pressed from a bit in the buffer and not just the last - I was surprised to see something resembling "until(KeyDown = 'q')" appear in the debug box, so I did a bit more and it does appear to store a few of the keys in the buffer, though it stops after running through the function a few times. I guess as long as the GetKeyDown function isn't the first thing you use, it should be ok, unless you're typing as you do it, though I have tested it with Scar typing functions, but I guess it would work with them too...
    I know it stores multiple keys, but this "bug" is also during runtime.. Atleast, when I was creating my game it would suddenly jump without pressing the space bar. The prob was that the space stayed in the buffer thats why I called the function twice, to be sure it is being pressed at the moment.. After running the function once, it is "fixed".. Easier fix would be the double If check, you can be 100% sure that the key is really being pressed, that's what we want huh!
    Verrekte Koekwous

  8. #8
    Join Date
    Dec 2006
    Location
    Sweden
    Posts
    10,812
    Mentioned
    3 Post(s)
    Quoted
    16 Post(s)

    Default

    Quote Originally Posted by mastaraymond View Post
    I know it stores multiple keys, but this "bug" is also during runtime.. Atleast, when I was creating my game it would suddenly jump without pressing the space bar. The prob was that the space stayed in the buffer thats why I called the function twice, to be sure it is being pressed at the moment.. After running the function once, it is "fixed".. Easier fix would be the double If check, you can be 100% sure that the key is really being pressed, that's what we want huh!
    I realised this too. In my script, before, I would have them hit the C key to stop the script, and it would randomly terminate.


    Send SMS messages using Simba
    Please do not send me a PM asking for help; I will not be able to help you! Post in a relevant thread or make your own! And always remember to search first!

  9. #9
    Join Date
    Jun 2007
    Location
    Wednesday
    Posts
    2,446
    Mentioned
    3 Post(s)
    Quoted
    1 Post(s)

    Default

    Edited the original code now to include a double check. The GetKeyDown *should* now work 100% properly and although I didn't do an extra if, I'm sure making GetKeysDown check the same thing twice *should* work the same and run smoothly.
    By reading this signature you agree that mixster is superior to you in each and every way except the bad ways but including the really bad ways.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •