What is an easy way to simulate user input programmatically?

I have a dialog box that appears as a result of an error. I want the dialog to remain open for at least 30 seconds and close 30 seconds after the last user input (mouse or keyboard) has been received.

I can implement this by checking the value returned by GetLastInputInfo and closing the dialog when it is more than 30 seconds ago, but if a dialog appears when the user has not been on the mouse or keyboard for 30 seconds, the GetLastInputInfo test passes immediately and the dialog closes immediately . I could do this with a different timer, but I suppose it would be much easier to simulate a mouse move or a (harmless) keystroke immediately before opening the dialog box. This would also have the advantage, apparently, of driving the system out of the splash screen.

What is the simplest 1-line Delphi code snippet for this?

+4
source share
2 answers

the simplest is the keybd_event function (one line of code)

 keybd_event(Ord('A'), 0, 0, 0); 

You can also use SendInput , but more than one line is required :)

 Var pInputs : TInput; begin pInputs.Itype := INPUT_KEYBOARD; pInputs.ki.wVk := Ord('A'); pInputs.ki.dwFlags := KEYEVENTF_KEYUP; SendInput(1, pInputs, SizeOf(pInputs)); end; 
+6
source

Enter a few 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; 
0
source

All Articles