I'm trying to use MouseAndKeyInput to automate a copy + paste action on a website login page but it only seems to work 50% of the time.
Briefly, I have a program that stores login details for a website. When I go the the login page I want it to automatically copy and paste my username and password into the login form. The code below
sometimes does this;
procedure Tprogram.Zzap(Data: PtrInt);
var
StoreCLPBpassword: String;
begin
// Copies the users password into the StoreCLPBpassword variable
StoreCLPBpassword:=ResultPassword.Text;
// Copy username to clipboard
Clipboard.AsText:=ResultUser.Text;
// Minimise application
WindowState:=wsMinimized;
sleep(200);
// Paste Username using CTRL+V
KeyInput.Apply([ssCtrl]);
KeyInput.Press('V');
KeyInput.Unapply([ssCtrl]);
sleep(200);
//TAB across to the next field
KeyInput.Press(VK_TAB);
sleep(200);
// Clear clipboard contents
Clipboard.Clear;
// Copy password to clipboard
Clipboard.AsText:=StoreCLPBpassword;
sleep(200);
// Paste Password
KeyInput.Apply([ssCtrl]);
KeyInput.Press('V');
KeyInput.Unapply([ssCtrl]);
sleep(200);
// Press Enter
KeyInput.Press(VK_RETURN);
// Clear Clipboard
Clipboard.Clear;
// Restore Application
WindowState:=wsNormal;
end;
procedure Tprogram.TBAutoClick(Sender: TObject);
begin
Application.QueueAsyncCall(@Zzap, 0);
end;
So, when a button is clicked the application
- Copies the username to the clipboard
- Minimises the application
- Simulates CTRL+V being pressed to paste the contents of the clipboard (username) into whichever window is active
- Simulates TAB being pressed to move to the next field
- |Copies Password to the clipboard
- Simulates CTRL+V being pressed again
- Simulates [ENTER] being pressed
It's hacky but it works.. sometimes. Other times TAB will be pressed before any other input. Other times there will just be the same field pasted twice.
Can anyone suggest why this is?
Or is there a better alternative to MouseAndKeyInput ?