问题
I have a dialog that pops up as result of an error condition. I want the dialog to remain open for at least 30 seconds, and close 30 seconds after the last user input (mouse or keyboard) is received.
I can implement this by checking the value returned by GetLastInputInfo and closing the dialog when this is more than 30 seconds ago, but if the dialog pops up when the user hasn't been at the mouse or keyboard for 30 seconds, the GetLastInputInfo test passes immediately, and the dialog closes again immediately. I could do this with another timer, but I figure it would be much simpler to simulate the mouse being moved a bit, or issuing a (harmless) keypress, just before the dialog opens. It would also have the advantage presumably of kicking the system out of screensaver.
What's the simplest 1-line Delphi code fragment to achieve this?
回答1:
the most simple is using the keybd_event function (one line of code)
keybd_event(Ord('A'), 0, 0, 0);
Also you can use the SendInput function but requires more than one line :)
Var
pInputs : TInput;
begin
pInputs.Itype := INPUT_KEYBOARD;
pInputs.ki.wVk := Ord('A');
pInputs.ki.dwFlags := KEYEVENTF_KEYUP;
SendInput(1, pInputs, SizeOf(pInputs));
end;
回答2:
Input multiple byte characters with keybd_event:
procedure InsertText(text:string);
var i:integer;
j:integer;
ch:byte;
str:string;
begin
i:=1;
while i<=Length(text) do
begin
ch:=byte(text[i]);
if Windows.IsDBCSLeadByte(ch) then
begin
Inc(i);
str:=inttostr(MakeWord(byte(text[i]), ch));
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), 0, 0);
j:=1;
while j<=Length(str) do
begin
keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 0, 0);
keybd_event(96+strtoint(str[j]), MapVirtualKey(96+strtoint(str[j]), 0), 2, 0);
j:=j+1;
end;
keybd_event(VK_MENU, MapVirtualKey(VK_MENU, 0), KEYEVENTF_KEYUP, 0);
end
else begin
keybd_event(VkKeyScan(text[i]),0,0,0);
keybd_event(VkKeyScan(text[i]),0,2,0);
end;
Inc(i);
end;
end;
来源:https://stackoverflow.com/questions/7723104/whats-a-simple-way-of-programmatically-simulating-user-input