Recent

Author Topic: MouseAndKeyInput not consistent  (Read 1890 times)

CyberFilth

  • Jr. Member
  • **
  • Posts: 88
    • My github account
MouseAndKeyInput not consistent
« on: December 05, 2017, 06:10:08 pm »
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;
Code: Pascal  [Select][+][-]
  1. procedure Tprogram.Zzap(Data: PtrInt);
  2. var
  3.   StoreCLPBpassword: String;
  4. begin
  5.   // Copies the users password into the StoreCLPBpassword variable
  6.   StoreCLPBpassword:=ResultPassword.Text;
  7.   // Copy username to clipboard
  8.   Clipboard.AsText:=ResultUser.Text;
  9.   // Minimise application
  10.   WindowState:=wsMinimized;
  11.  
  12.   sleep(200);
  13.  
  14.   // Paste Username using CTRL+V
  15.   KeyInput.Apply([ssCtrl]);
  16.   KeyInput.Press('V');
  17.   KeyInput.Unapply([ssCtrl]);
  18.  
  19.   sleep(200);
  20.  
  21.   //TAB across to the next field
  22.   KeyInput.Press(VK_TAB);
  23.  
  24.   sleep(200);
  25.   // Clear clipboard contents  
  26.   Clipboard.Clear;
  27.  
  28.  
  29.   // Copy password to clipboard
  30.   Clipboard.AsText:=StoreCLPBpassword;
  31.  
  32.   sleep(200);
  33.  
  34.     // Paste Password
  35.     KeyInput.Apply([ssCtrl]);
  36.     KeyInput.Press('V');
  37.     KeyInput.Unapply([ssCtrl]);
  38.  
  39.   sleep(200);
  40.  
  41.    // Press Enter
  42.   KeyInput.Press(VK_RETURN);
  43.   // Clear Clipboard
  44.   Clipboard.Clear;
  45.   // Restore Application
  46.   WindowState:=wsNormal;
  47. end;
  48.  
  49. procedure Tprogram.TBAutoClick(Sender: TObject);
  50. begin
  51.   Application.QueueAsyncCall(@Zzap, 0);
  52. end;  
  53.  
    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 ?
    Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

    CyberFilth

    • Jr. Member
    • **
    • Posts: 88
      • My github account
    Re: MouseAndKeyInput not consistent
    « Reply #1 on: December 06, 2017, 02:36:14 pm »
    I forgot to add that, so far, I've only tried this code on Linux 64bit GTK.
    I'm aware that there is an issue with some clipboard functionality under GTK so I'll give it a try under Qt and Windows.
    Running Windows 10 & Xubuntu 20.04 | Lazarus 2.0.12 | FPC 3.2.0

     

    TinyPortal © 2005-2018