SRL Archive - OSR Help

function is returning > 50 but it's not?


rj

2013-05-16 16:46:40

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:

{$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.

t4q

2013-05-16 17:12:37

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 :redface:

honeyhoney

2013-05-17 02:43:31

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

Le Jingle

2013-05-17 04:44:42

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?

(*
* 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.