0

I am trying to write international program and need to send some text to "other text edit programs" like word or notepad or a browser. On the other hand I am not sure that I can find an international way(because of the different keyboard layouts)

it would be nice to use a code like below

SendMessage(FindActiveWindowsHWND,WM_SETTEXT,0,Integer(PChar('My String')));

and I dont have function like FindActiveWindowsHWND

Edit: The code I am tried but not satisfied so far;

procedure FindActiveWindowsHWND();
var
 ThreadInfo: TGUIThreadInfo;
 activewindowsHwnd: HWND;
begin
  GetGUIThreadInfo(0,ThreadInfo);
  activewindowsHwnd:= ThreadInfo.hwndActive; (or ThreadInfo.hwndFocus);
end;

also I used Sendinput function like this

procedure SendKey(vKey: SmallInt; booDown: boolean);
var
  GInput: array[0..0] of tagINPUT; //GENERALINPUT;
  // doesn't have to be array :)
begin
  GInput[0].Itype := INPUT_KEYBOARD;
  GInput[0].ki.wVk := vKey;
  GInput[0].ki.wScan := 0;
  GInput[0].ki.time := 0;
  GInput[0].ki.dwExtraInfo := 0;

  if not booDown then
    GInput[0].ki.dwFlags := KEYEVENTF_KEYUP
  else
    GInput[0].ki.dwFlags := 0;

  SendInput(1, GInput[0], SizeOf(GInput));
end;

then

SendKey(65,true); //to send an "A" for example

but instead it sent an "a" and when I try to send an "a" using SendKey(97,true) it sent "1". it is really interesting that I have to send shift key down to write uppercase letters

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user3057015
  • 41
  • 1
  • 7
  • 1
    Use the automation api rather than faking input – David Heffernan Jan 17 '14 at 21:48
  • The documentation for `GetGUIThreadInfo()` says: "Note that you must set the cbSize member to sizeof(GUITHREADINFO) before calling this function." You are not doing that. And you should utilize the `KEYEVENTF_UNICODE` flag when using `SendInput()`, then you don't have to deal with scancodes and virtual keys. – Remy Lebeau Jan 18 '14 at 16:54

1 Answers1

1

You can use GetGUIThreadInfo() to get the HWND of the currently focused window in another process. Not all window types accept WM_SETTEXT, though. You could use SendInput() to put Unicode characters into the keyboard queue, though. Or use the Automation API, like David said, though not all window types implement that.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • another thing I couldn't get hwnd of the active window using getguiThreadInfo() funciton. Can you give me an example? – user3057015 Jan 18 '14 at 05:11
  • and also how can I use sendinput to send international key strokes? I can use it to send a keystroke but how can I be sure that "it works in every keyboard layouts"? – user3057015 Jan 18 '14 at 05:12
  • [Windows Automation API](http://msdn.microsoft.com/en-us/library/windows/desktop/ff486375.aspx). – Remy Lebeau Jan 18 '14 at 05:13
  • Rather than show you an example, please edit your original question to show what code you have tried so far, so someone can tell you if you are doing something wrong with it. And read the documentation, it tells you how to send Unicode cbaracters, you don't need to deal with layouts. – Remy Lebeau Jan 18 '14 at 05:16
  • thanks for the information about Windows Automation API, on the otherhand I didnt understand how and where can I use it – user3057015 Jan 18 '14 at 08:35
  • I don't understand why you ask here what automation api is rather than doing web search – David Heffernan Jan 19 '14 at 08:06
  • Because I spent 20 hours last weekend searching an api for international virtual key send... – user3057015 Jan 20 '14 at 07:47