Log in

View Full Version : Event Listers? and code performance



Frozenfire216
09-18-2012, 03:45 AM
So this is my first script, a very simple autotyper.


program Auto_Typer;
{$i srl/srl.simba}
{$I SPS/SPS.Simba}
{$IFDEF SIMBAMAJOR980}
{$ENDIF}


Const
MESSAGE_TO_TYPE=('samplemessage');

procedure TypeThis(Text:string; PressEnter:Boolean);
var
I: Integer;
begin
for i := 1 to Length(Text) do
begin
{$IFDEF SIMBAMAJOR980}
SendKeys(Text[i], 2);
{$ELSE}
SendKeys(Text[i], 2, 2);
{$ENDIF}
Wait(5);
end;

if (PressEnter) then
begin
TypeByte(VK_RETURN);
end;
end;

begin
ClearDebug;
SetUpSRL;
repeat
if (IsfKeyDown(6)) then
begin
wait(100)
TypeThis(MESSAGE_TO_TYPE, true)
wait(2000)
end;
until(false);
end.

what i am trying to accomplish is whenever i press f6, it types a certain line.

it works, but the problem im having is that it takes up about 50% of memory of my computers 1g memory. Is their any way to increase the performance of this code?

I figure that the infinite loop is the problem here, and I was wondering if there were any way to add event listeners, such as in Flash.

Brandon
09-18-2012, 03:58 AM
Length(Text)

should be:

L := Length(Text).. Then Iterator I to L.

This avoids the overhead of calculating the length of the text every loop. Also that infinite loop needs to give the thread time to sleep or else it will constantly hog the CPU and windows isn't too fond of that. Infact most computers/OS's aren't fond of that. Not sure about modern day comps but my really old comp was like that. I don't seem to have this problem on this current laptop. Well that's not entirely true. Sometimes it'll hog it bad and lag.. Other times, Iunno.. it's normal.

Thus outside the if statement, add a small wait inbetween. Maybe something like Sleep(10) or Sleep(100). That's should be more than enough time but then again it is pascal script so I'd say the 100 should be good. If it isn't enough, just increase it a slight bit to your liking but make sure you don't increase too much or else your hotkeys will register late.

That brings me to another point.. If you are familiar with extensions or forms, you can either create an extension that will do:
RegisterHotKey(.......) external __stdcall RegisterHotKey@User32.dll;

OR you can create a Timer which can "simulate" multithreading and have that constantly check every Iunno 100 ms interval for a keypress.

Lape interpreter could also "Possibly" be another solution for speed.. Not sure about usage reduction.

Le Jingle
09-18-2012, 03:59 AM
Might also help to remove the sps include.
Brandon pretty much covered it

Daniel
09-18-2012, 06:00 AM
Just add:

Sleep(1);

somewhere to the loop to pretty much remove most of that CPU usage.

EDIT: Brandon's already said it.. :p