How can I emulate typing of a real human in either TWebBrowser or TChromium. It can be either of those two.
Asked
Active
Viewed 2,429 times
2 Answers
4
Will need some adaptions, might be helpful for furthor development
procedure SendKeys(sText: String);
var
i : Integer;
shift : Boolean;
vk, scancode : Word;
ch : Char;
c, s : Byte;
const
vk_keys : Array[0..9] of Byte=(VK_HOME, VK_END, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT, VK_PRIOR, VK_NEXT, VK_INSERT, VK_DELETE);
vk_shft : Array[0..2] of Byte=(VK_SHIFT, VK_CONTROL, VK_MENU);
flags : Array[FALSE..TRUE] of Integer = (KEYEVENTF_KEYUP, 0);
C_ALTGRS = ['\','@','~','²','³','€','{','}','[',']'];
begin
shift:=FALSE;
for i:=1 to Length(sText) do begin
ch:=sText[i];
if (ch>=#250) then begin
s:=Ord(ch)-250;
shift:=NOT Odd(s);
c:=vk_shft[s shr 1];
scancode:=MapVirtualKey(c, 0);
Keybd_Event(c, scancode, flags[shift], 0);
end else begin
vk:=0;
if (ch>=#240) then
c:=vk_keys[Ord(ch)-240]
else if (ch>=#228) then
c:=Ord(ch)-116
else if (ch<#32) then
c:=Ord(ch)
else begin
vk:=VkKeyScan(ch);
c:=LoByte(vk);
end;
scancode:=MapVirtualKey(c, 0);
if (sText[i] in C_AltGRS) then Keybd_Event(VK_RMENU, MapVirtualKey(VK_RMENU,0), 0, 0)
else if (NOT shift AND (Hi(vk)>0)) then Keybd_Event(VK_SHIFT, $2A, 0, 0 );
Keybd_Event( c, scancode, 0, 0 );
Keybd_Event( c, scancode, KEYEVENTF_KEYUP, 0 );
if (sText[i] in C_AltGRS) then Keybd_Event(VK_RMENU,MapVirtualKey(VK_RMENU,0), KEYEVENTF_KEYUP, 0)
else if (NOT shift AND (Hi(vk)>0)) then Keybd_Event(VK_SHIFT, $2A, KEYEVENTF_KEYUP, 0);
end;
Application.ProcessMessages;
end;
end;
procedure TForm4.FormCreate(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.google.de');
end;
procedure TForm4.SpeedButton1Click(Sender: TObject);
begin
SendKeys('test');
SendKeys(#13);
end;
![](../../users/profiles/1699210.webp)
bummi
- 26,435
- 13
- 58
- 97
-
@Santos, I wouldn't call it the best; the `SendInput` function is the preferred way. – TLama Feb 17 '13 at 17:30
-
It works with UPPERCASElowercase 0123456789`!"£$%^&*()_+=-*/?|\, but gives Range check error when ąčęėįšųūž used. – Edijs Kolesnikovičs Apr 26 '14 at 06:14
4
This should be enough to get You started.
procedure EmulateKeyPress();
var
_Input: TInput;
begin
_Input.Itype := INPUT_KEYBOARD;
_Input.ki.wVk := VK_SHIFT; // $31 is the VK_1
_Input.ki.wScan := 0;
_Input.ki.dwFlags := 0;
_Input.ki.time := 0;
_Input.ki.dwExtraInfo := 0;
if SendInput(1, _Input, SizeOf(_Input)) = 0 then
begin
ShowMessage('Input event was Blocked');
Exit
end;
_Input.ki.dwFlags := KEYEVENTF_KEYUP;
SendInput(1, _Input, SizeOf(_Input));
end;
![](../../users/profiles/782265.webp)
ertx
- 1,426
- 2
- 14
- 21
-
How do you make sure these events are being sent to the correct control? – Leonardo Herrera Dec 21 '12 at 13:56
-
This code should send input to currently active control, you may use additional code to set your control as active, afaik, there's no way of emulating a KeyPress event to non-active control. Don't forget to check if your control is visible and enabled, before activating it. – ertx Dec 21 '12 at 14:39