Page 1 of 1

Delphi For Loop Help

PostPosted: March 24th, 2010, 7:01 pm
by Mike1990
This is the code i use, but the timer for loop doesn't terminate and shows a message box up loads of times. It should only show the message box up when a key is pressed and should only show it once

Code: Select all
procedure TForm2.keyloggerTimer(Sender: TObject);
var
i: Integer;
key: String;
begin

  for i := 97 to 122 do
  begin
    if GetAsyncKeyState(i) <> 0 then
      key := key + Chr(i);

    ShowMessage(key);
  end;

end;


Could anyone help me?

Re: Delphi For Loop Help

PostPosted: March 24th, 2010, 7:34 pm
by Kambiz
Try this code:

Code: Select all
procedure TForm2.keyloggerTimer(Sender: TObject);
var
  i: Integer;
  key: String;
begin
  key := '';
  for i := 97 to 122 do
  begin
    if GetAsyncKeyState(i) <> 0 then
      key := key + Chr(i);
  end;
  if key <> '' then
  begin
    keylogger.enabled := False;
    ShowMessage(key);
    keylogger.enabled := True;
  end;
end;

Re: Delphi For Loop Help

PostPosted: March 24th, 2010, 8:26 pm
by Mike1990
Thanks for the help but still not working

Code: Select all
procedure TForm2.keyloggerTimer(Sender: TObject);
var
i: Integer;
key: String;
begin
  key := '';
  for i := 97 to 122 do
  begin
    if GetAsyncKeyState(i) <> 0 then
      key := key + Chr(i);
  end;
  if key <> '' then
  begin
    keylogger.enabled := False;
    ShowMessage(key);
    keylogger.Enabled := True;
  end;
end;


I enable the keylogger through a client and the client sends the server the enable keylogger command. When the server receives the enable keylogger command the server enables the timer.

If you could help me fix this, it would really be appreciated.

Thank you

Re: Delphi For Loop Help

PostPosted: March 25th, 2010, 1:07 am
by Kambiz
It doesn't work because the logic is wrong.

You have to use a keyboard hook. Google 'keyboard hook' for more information.

Re: Delphi For Loop Help

PostPosted: March 25th, 2010, 4:51 pm
by Mike1990
I've got it sorted now thank you for your help, but one last question.

Is it possible to send data back to the client from the server?

Thank you

Re: Delphi For Loop Help

PostPosted: March 25th, 2010, 6:34 pm
by Kambiz
Yes, client can send data to server if you are using sockets.

Re: Delphi For Loop Help

PostPosted: March 25th, 2010, 6:49 pm
by Mike1990
I can send data from the client to the server but i can't seem to be able to send data back from the server to the client

I'm using TClientSocket and TServerSocket, is it possible?