I'm trying to develop a routine in Delphi XE to output text to the screen to simulate typing text in a game. It's not as simple as concatenating characters to a TMemo for example, as this results in flickering even when using beginupdate/endupdate. I've used the technique here, converted in to Delphi code as best I can and modified it based on comments here but I'm not getting the correct text. I'm getting garbage I guess because the characters are somehow not in the right format. Does anyone see what is wrong here or know a better way to simulate typing?
procedure SendEnter();
var
KeyInputs: array [0..1] of TInput;
begin
ZeroMemory(@KeyInputs,sizeof(KeyInputs));
KeyInputs[0].Itype := INPUT_KEYBOARD;
KeyInputs[0].ki.wVk := VK_RETURN;
KeyInputs[0].ki.dwFlags := 0;
KeyInputs[1].Itype := INPUT_KEYBOARD;
KeyInputs[1].ki.wVk := VK_RETURN;
KeyInputs[1].ki.dwFlags := KEYEVENTF_EXTENDEDKEY;
SendInput(2, KeyInputs[0], SizeOf(TInput));
end;
function TForm2.PrintOutLine(sLine: string): boolean;
var
i: integer;
VKRes: SmallInt;
VK: byte;
KeyInputsU: array [0..3] of TInput;
KeyInputsL: array [0..1] of TInput;
bUppercase: boolean;
begin
Memo1.SetFocus;
i:= 1;
while (i<=Length(sLine)) do begin
bUppercase:= sLine[i]=UpCase(sLine[i]);
VKRes:= VkKeyScanEx(sLine[i],GetKeyboardLayout(0));
VK:= VKRes;
if (bUppercase) then begin
ZeroMemory(@KeyInputsU,sizeof(KeyInputsU));
KeyInputsU[0].Itype := INPUT_KEYBOARD;
KeyInputsU[0].ki.wVk := VK_LSHIFT;
//KeyInputsU[0].ki.dwFlags := 0;
KeyInputsU[1].Itype := INPUT_KEYBOARD;
KeyInputsU[1].ki.wVk := vk;
KeyInputsU[1].ki.dwFlags := KEYEVENTF_UNICODE ;
KeyInputsU[2].Itype := INPUT_KEYBOARD;
KeyInputsU[2].ki.wVk := vk;
KeyInputsU[2].ki.dwFlags := KEYEVENTF_UNICODE or KEYEVENTF_KEYUP;
KeyInputsU[3].Itype := INPUT_KEYBOARD;
KeyInputsU[3].ki.wVk := VK_LSHIFT;
KeyInputsU[3].ki.dwFlags := KEYEVENTF_KEYUP;
SendInput(4, KeyInputsU[0], SizeOf(TInput));
end
else begin
ZeroMemory(@KeyInputsL,sizeof(KeyInputsL));
KeyInputsL[0].Itype := INPUT_KEYBOARD;
KeyInputsL[0].ki.wVk := vk;
KeyInputsL[0].ki.dwFlags := KEYEVENTF_UNICODE;
KeyInputsL[1].Itype := INPUT_KEYBOARD;
KeyInputsL[1].ki.wVk := vk;
KeyInputsL[1].ki.dwFlags := KEYEVENTF_UNICODE or KEYEVENTF_KEYUP;
SendInput(2, KeyInputsL[0], SizeOf(TInput));
end;
Application.ProcessMessages;
Sleep(80);
inc(i);
end;
SendEnter;
Form2.SetFocus;
Result:= True;
end;