Jump to content
miguelandradebr

GetWindowHandle + Ctrl V

Recommended Posts

Hello

 

I want to create a Delphi application to type a password on Kaspersky Password Manager.

 

How I Can create a delphi application to select the input on Kaspersky Password Manager? I need to use GetWindowHandle function? Later I need to paste my password on the password input on Password Manager

 

Thanks

 

 

Captura de tela 2023-12-21 183550.png

Share this post


Link to post

In the past I had an application which imitated keypresses to type text to any field in any application using SendInput. Later the typing logic has been extracted and is now accessible as TAEVirtualKeyboard for generic unicode or TAEVirtualEnUsKeyboard for forced EN-US keyboard layouts.

 

The backdraw is, for this to work the window and the input field has to have focus. It might be a good starting point still, though.

Share this post


Link to post
On 12/22/2023 at 6:06 AM, aehimself said:

In the past I had an application which imitated keypresses to type text to any field in any application using SendInput. Later the typing logic has been extracted and is now accessible as TAEVirtualKeyboard for generic unicode or TAEVirtualEnUsKeyboard for forced EN-US keyboard layouts.

 

The backdraw is, for this to work the window and the input field has to have focus. It might be a good starting point still, though.

Hello @aehimselfour your component can be installed in what delphi version?

 

Do you know if it works with Delphi 7?

 

Thank you in advance!

Share this post


Link to post

The component itself already heavily relies on generics and other quality-of-life improvements later versions brought so as it is no, it won't compile.

 

However, the base principle is SendInput and an array of TInput records which should be present / can easily be imported in D7. Since it does nothing else but translating your string into TInputs and call the WinApi method, with a little bit of work invested (which I was too lazy to do so) yes, it should work just fine.

Share this post


Link to post
On 12/24/2023 at 2:53 PM, aehimself said:

The component itself already heavily relies on generics and other quality-of-life improvements later versions brought so as it is no, it won't compile.

 

However, the base principle is SendInput and an array of TInput records which should be present / can easily be imported in D7. Since it does nothing else but translating your string into TInputs and call the WinApi method, with a little bit of work invested (which I was too lazy to do so) yes, it should work just fine.

ok thank you!

Share this post


Link to post

 

the friend Ledklom shared this with me in PM:

 

tips.delphidabbler.com/tips/170

 


procedure PostKeyEx32(key: Word; const shift: TShiftState;  specialkey: Boolean);type  TShiftKeyInfo = record    shift: Byte;    vkey: Byte;  end;  ByteSet = set of 0..7;const  shiftkeys: array [1..3] of TShiftKeyInfo = (    (shift: Ord(ssCtrl) ; vkey: VK_CONTROL),    (shift: Ord(ssShift) ; vkey: VK_SHIFT),    (shift: Ord(ssAlt) ; vkey: VK_MENU)  );var  flag: DWORD;  bShift: ByteSet absolute shift;  j: Integer;begin  for j := 1 to 3 do  begin    if shiftkeys[j].shift in bShift then      keybd_event(        shiftkeys[j].vkey, MapVirtualKey(shiftkeys[j].vkey, 0), 0, 0    );  end;  if specialkey then    flag := KEYEVENTF_EXTENDEDKEY  else    flag := 0;  keybd_event(key, MapvirtualKey(key, 0), flag, 0);  flag := flag or KEYEVENTF_KEYUP;  keybd_event(key, MapvirtualKey(key, 0), flag, 0);  for j := 3 downto 1 do  begin    if shiftkeys[j].shift in bShift then      keybd_event(        shiftkeys[j].vkey,        MapVirtualKey(shiftkeys[j].vkey, 0),        KEYEVENTF_KEYUP,        0      );  end;end;
	


 

usage:


//Upercase

PostKeyEx32(Ord('A'), [ssShift], False);

//Lowercase
PostKeyEx32(Ord('F'), [], False);
PostKeyEx32(Ord('2'), [ssShift], True); //Arroba special symbol char @

 

Share this post


Link to post

When I run my code inside delphi using debugger all works fine.

 

When I compile exe and try the run the application its dont type write the keys.... very strange I'm using Delphi 7 and Windows 11

 

here is my code:

procedure TForm1.Button3Click(Sender: TObject);
var
  H : HWnd;
 begin

      H := FindWindow(nil, pchar('Kaspersky Password Manager'));
      if H <> 0 then
      begin
     // Try to bring target window to foreground
     ShowWindow(H,SW_SHOW);
     SetForegroundWindow(H);

PostKeyEx32(Ord('O'), [ssShift], False);
PostKeyEx32(Ord('Y'), [], False);
PostKeyEx32(Ord('2'), [ssShift], True); //Arroba special symbol char @

PostKeyEx32(VK_RETURN, [], False) ;

 

suggestion how to solve?

Edited by miguelandradebr

Share this post


Link to post
8 hours ago, miguelandradebr said:

When I run my code inside delphi using debugger all works fine.

 

26 minutes ago, Brian Evans said:

The replacement (SendInput function (winuser.h) - Win32 apps | Microsoft Learn) specifically mentions applications are permitted to inject input only into applications that are at an equal or lesser integrity level. Likely the same applies to the older API. This blocking just silently does nothing - no keys arrive.

 

However, it may be that the focus of the foreground window is not yet active when the program generates the key sequence.

The fact that it works during debugging and that in normal runtime it doesn't,  makes me think so.

Try putting a sleep(1000) after the SetForeground.
Then maybe decrease the sleep...

Share this post


Link to post
12 minutes ago, DelphiUdIT said:

 

 

However, it may be that the focus of the foreground window is not yet active when the program generates the key sequence.

The fact that it works during debugging and that in normal runtime it doesn't,  makes me think so.

Try putting a sleep(1000) after the SetForeground.
Then maybe decrease the sleep...

sleep its not working, I've tried to to use a timer with 5 seconds... and all works just using/running app inside Delphi/debugger

Share this post


Link to post
52 minutes ago, Brian Evans said:

The replacement (SendInput function (winuser.h) - Win32 apps | Microsoft Learn) specifically mentions applications are permitted to inject input only into applications that are at an equal or lesser integrity level. Likely the same applies to the older API. This blocking just silently does nothing - no keys arrive. 

 

 

Seems that You're right.

 

How I can turn around?

 

Maybe I can create a driver or Windows Service?

 

I accept suggestions

 

Thanks in advance!

Share this post


Link to post

This code works, but just from inside delphi/debugger:

 

  Clipboard.AsText := 'mypass';
  Keybd_event(VK_CONTROL, 0, 0, 0);
  Keybd_event(Byte('V'), 0, 0, 0);
  Keybd_event(Byte('V'), 0, KEYEVENTF_KEYUP, 0);
  Keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);

  Keybd_event(VK_RETURN, 0, 0, 0);
  Keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);

 

when you try from .exe file still dont working...

Share this post


Link to post

Hello friends

 

Is there no way to add any text to an application created in Delphi? Is it possible to paste any text into the notepad?

It seems like something easy to implement but I haven't found a way yet.

 

Thanks

Share this post


Link to post
Posted (edited)
On 12/27/2023 at 5:42 PM, miguelandradebr said:

  Clipboard.AsText := 'mypass';

  Keybd_event(VK_CONTROL, 0, 0, 0);
  Keybd_event(Byte('V'), 0, 0, 0);
  Keybd_event(Byte('V'), 0, KEYEVENTF_KEYUP, 0);
  Keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);

  Keybd_event(VK_RETURN, 0, 0, 0);
  Keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);

Do not use keybd_event(), use SendInput() instead.

11 hours ago, miguelandradebr said:

Is there no way to add any text to an application created in Delphi?

Only if you have permission to access the other app.

11 hours ago, miguelandradebr said:

Is it possible to paste any text into the notepad? It seems like something easy to implement but I haven't found a way yet.

Into a simple app like Notepad, yes. But given that Kaspersky is a security app, I doubt they want people to automate it like this, so the approach you are attempting will likely never work with it.

Edited by Remy Lebeau

Share this post


Link to post

With a few exceptions for debugging purposes lower integrity processes cannot mess with higher integrity processes. That includes sending input or paste operations. 

 

The quick example for why this is not allowed would be a command window with Administrator privileges - not a good thing security wise if all other apps could paste whatever they wanted into it.  

 

Share this post


Link to post
10 hours ago, David Heffernan said:

Are you running your IDE elevated? 

 

Changing the permission on .exe file to admin all is fine now, what do you mean with IDE elevated?

Share this post


Link to post
Posted (edited)
1 hour ago, miguelandradebr said:

I've fixed it changing the .exe file to admin permissions, thanks guys!

If your app actually needs admin rights, then you should add an application manifest to your project to specify its requestedExecutionLevel at compile-time, not change the properties of the EXE file after the compile is finished.  Modern Delphi versions even have a project setting for that very purpose:

http://docwiki.embarcadero.com/RADStudio/en/Manifest_File

http://docwiki.embarcadero.com/RADStudio/en/Application_Manifest

1 hour ago, miguelandradebr said:

what do you mean with IDE elevated?

Basically, what you did for your app, but for the IDE instead, so it runs as an admin, and then any project it runs will also run as an admin.

Edited by Remy Lebeau

Share this post


Link to post
12 hours ago, Remy Lebeau said:

If your app actually needs admin rights, then you should add an application manifest to your project to specify its requestedExecutionLevel at compile-time, not change the properties of the EXE file after the compile is finished.  Modern Delphi versions even have a project setting for that very purpose:

http://docwiki.embarcadero.com/RADStudio/en/Manifest_File

http://docwiki.embarcadero.com/RADStudio/en/Application_Manifest

Basically, what you did for your app, but for the IDE instead, so it runs as an admin, and then any project it runs will also run as an admin.

 

Thank you!

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×