Results 1 to 4 of 4

Thread: function is returning > 50 but it's not?

  1. #1
    Join Date
    Sep 2010
    Posts
    5,762
    Mentioned
    136 Post(s)
    Quoted
    2739 Post(s)

    Default function is returning > 50 but it's not?

    I am trying to check to see if i pressed "a" and "b" or any other message but it seems to be returning true everytime...


    Here is my code:

    Simba Code:
    {$i srl/srl.simba}

    function PressedKey(Key:word;wait:Integer) : Boolean;
    var
      Tries, T:Integer;
    begin
      MarkTime(T);
      repeat
      until (IsKeyDown(Key) or (TimeFromMark(T) > wait));
      MarkTime(T);
      repeat
      Until (not IsKeyDown(Key) or (TimeFromMark(T) > wait));
      if not IsKeydown(Key) then Result := true;
    end;

    function PressedKeysCorrect(keys:TStringArray) : Integer;
    Var
      key:word;
      correct, i, t, c:Integer;
    begin

     for i:= 0 to high(keys) do
       begin
        case(keys[c]) of
          'a': key := 65;
          'b': key := 66;
          'c': key := 67;
          'd': key := 68;
          'e': key := 69;
          'f': key := 70;
          'g': key := 71;
          'h': key := 72;
          'i': key := 73;
          'j': key := 74;
          'k': key := 75;
          'l': key := 76;
          'm': key := 77;
          'n': key := 78;
          'o': key := 79;
          'p': key := 81;
          'q': key := 82;
          'r': key := 83;
          's': key := 84;
          't': key := 85;
          'u': key := 86;
          'v': key := 87;
          'w': key := 88;
          'x': key := 89;
          'y': key := 90;
          'z': key := 91;
         end;
         MarkTime(T);
         repeat
           wait(1)
         until (PressedKey(key,500) or (TimeFromMark(T) > 500));
         if PressedKey(key,500) then correct := correct + 1;
         c := c + 1;
       end;
     result := (correct/(length(keys)))*100;
    end;

    begin

      if (PressedKeysCorrect(['h','b'])>=99) then writeln('got it right')

    end.

  2. #2
    Join Date
    Mar 2007
    Posts
    393
    Mentioned
    1 Post(s)
    Quoted
    98 Post(s)

    Default

    Simba Code:
    begin
      MarkTime(T);
      repeat
      until (IsKeyDown(Key) or (TimeFromMark(T) > wait));
      MarkTime(T);
      repeat
      Until (not IsKeyDown(Key) or (TimeFromMark(T) > wait));
      if not IsKeydown(Key) then Result := true;
    end;
    Why do you use 2 times repeat-until loop?

    Edit: I didn't see the difference at first.. It's late, good night
    Last edited by t4q; 05-16-2013 at 09:15 PM.

  3. #3
    Join Date
    Dec 2007
    Posts
    289
    Mentioned
    4 Post(s)
    Quoted
    86 Post(s)

    Default

    Your PressedKey is always returning true.
    It goes through the two repeat loops and then if you are not holding the key down (which usually you won't be) by the time it gets to
    Code:
      if not IsKeydown(Key) then Result := true;
    then it'll return true.

    You need to make the function only return true if the key is pressed and then released within a certain time frame.

  4. #4
    Join Date
    Feb 2012
    Location
    Wonderland
    Posts
    1,988
    Mentioned
    41 Post(s)
    Quoted
    272 Post(s)

    Default

    More information as to what you're specifically trying to do would definitely help problem solve this

    From what I guessed via your code, I think you're waiting for key's to be 'surely' pressed? As well as typing only an array of single characters?
    Simba Code:
    (*
     * Types an array of single characters, as exampled in main
     * ~Le Jinlge
     *)

    function AwesomeSauce(arr: TStringArray): Integer;
    var i, t: Integer;
    begin
      for i := 0 to high(arr) do
      begin
        PressKey(GetKeyCode(Char(arr[i][1])));
        t := GetSystemTime() + 100;
        while (GetSystemTime() < t) and (isKeyDown(GetKeyCode(Char(arr[i][1])))) do
        begin
          KeyUp(GetKeyCode(Char(arr[i][1])));
          Wait(10 + Random(10));
        end;
      end;
    end;

    begin
      ActivateClient;
      AwesomeSauce(['a', 'b', 'c', 'd']);
    end.

    * Could also add a check in there to see if the character is in range of specified values, alike your 65-91 range in Original Post.

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
  •