Forum > Windows

Using SendMessage or Postmessage to simulate keyboard

<< < (2/2)

Thaddy:

--- Quote from: KodeZwerg on December 22, 2022, 11:06:20 pm ---I am unsure what the OP try to do, maybe WM_SETTEXT is better.

--- End quote ---
Note Remy's remark and Raymond Chen's column: they are right. (Now I only need to get my example to work, but al least it compiles)
All that post/sendmessage stuff worked somewhat in very old Win versions (pre XP, WIN2000) and still may work, but is not reliable enough.

KodeZwerg:
I've read the remarks and need to think for a while, I guess when I do connect to the target thread I can attach to play a little.
Here is something to send at least some Text if that is wanted  :-[ (based on GetMems version as source)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function SendTextAtToMsg(const AClassname, AControlClass, AMessage: WideString): Boolean;var  wHandle: HWND;  Len: DWORD;  s, z: WideString;begin  Result := False;  // get main window handle  wHandle := FindWindowW(PWideChar(AClassname), nil);  if (wHandle = 0) then    Exit;  // get control handle  wHandle := FindWindowExW(wHandle, FindWindowW(PWideChar(AControlClass), nil), nil, nil);  if (wHandle = 0) then    Exit;  // get current contant as proof of concept, i am sure its wrong somewhere :P  s := '';  Len := Succ(SendMessageW(wHandle, WM_GETTEXTLENGTH, 0, 0));  SetLength(s, (Succ(Len) * SizeOf(WideChar)));  SendMessageW(wHandle, WM_GETTEXT, WPARAM(Succ(Len)), LPARAM(@s[1]));  Form1.Memo1.Clear;  Form1.Memo1.Text := s;  // send to control a new string  z := AMessage;  Len := Succ((Length(z) * SizeOf(WideChar)));  SendMessageW(wHandle, WM_SETTEXT, WPARAM(Succ(Len)), LPARAM(@z[1]));  Result := True;end; procedure TForm1.Button1Click(Sender: TObject);begin  if SendTextAtToMsg('notepad', 'edit', 'Hacked') then    Memo1.Lines.Add('New Text Set')    else    Memo1.Lines.Add('Error at finding Notepad!');end;

GetMem:
Sending a simple English char like "A" will definitely work with PostMessage/SendMessage too. There is no contradiction with Raymond Chen's article.
Keybd_Event is now superseded, the preferred method is SendInput, but in order to use it, first you have to activate the target window with SetForegroundWindow. Stackoverflow is full with examples on how to use SendInput.

Remy Lebeau:

--- Quote from: Thaddy on December 22, 2022, 08:49:17 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uSent := SendInput(SizeOf(inputs),inputs, sizeof(INPUT));
--- End quote ---

In the 1st parameter, you are passing in the byte size of the array, but the function wants the number of array elements instead, so you need to use Length() instead of SizeOf():


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uSent := SendInput(Length(inputs), inputs, sizeof(INPUT));

Navigation

[0] Message Index

[*] Previous page

Go to full version