Does anyone know how to programmatically press a key in the same way as if it were pressed on a keyboard? PostMessage fails, SendInput fails, keybd_event fails, SendMessage fails, and I even tried an unusual way of overlaying a keystroke using Vcl.Touch.Keyboard. I even wrote a quick vb script file that calls sendkeys, which also failed. I am having trouble trying to do this in a program called Sellerdeck http://www.sellerdeck.com/
I tried all of the following, they all work with notepad, but do not work in this program. Window successfully delivered to
procedure bringToForegroung; Var findhandle1 : cardinal; begin findhandle1 := FindWindow(NIL, 'Business Plus - Test site: Online Catalogue'); ShowWindow(findhandle1,SW_SHOW); SetForegroundWindow(findhandle1); end;
one.
procedure SendF10; Var findhandle1 : cardinal; begin findhandle1 := FindWindow(NIL, 'Business Plus - Test site: Online Catalogue'); ShowWindow(findhandle1,SW_SHOW); SetForegroundWindow(findhandle1); PostMessage(findhandle1, WM_KEYDOWN, VK_F10, 0); PostMessage(findhandle1, WM_KEYUP, VK_F10, 0); end;
2.
procedure SendAltF; var KeyInputs: array of TInput; //-------------------------------------------- procedure KeybdInput(VKey: Byte; Flags: DWORD); begin SetLength(KeyInputs, Length(KeyInputs)+1); KeyInputs[high(KeyInputs)].Itype := INPUT_KEYBOARD; with KeyInputs[high(KeyInputs)].ki do begin wVk := VKey; wScan := MapVirtualKey(wVk, 0); dwFlags := Flags; end; end; begin KeybdInput(VK_MENU, 0); // Alt KeybdInput(Ord('F'), 0); KeybdInput(Ord('F'), KEYEVENTF_KEYUP); KeybdInput(VK_MENU, KEYEVENTF_KEYUP); // Alt SendInput(Length(KeyInputs), KeyInputs[0], SizeOf(KeyInputs[0])); end;
3.
procedure SendKey2(Wnd,VK : Cardinal; Ctrl,Alt,Shift : Boolean); var MC,MA,MS : Boolean; begin // Try to bring target window to foreground ShowWindow(Wnd,SW_SHOW); SetForegroundWindow(Wnd); // Get current state of modifier keys MC:=Hi(GetAsyncKeyState(VK_CONTROL))>127; MA:=Hi(GetAsyncKeyState(VK_MENU))>127; MS:=Hi(GetAsyncKeyState(VK_SHIFT))>127; // Press modifier keys if necessary (unless already pressed by real user) if Ctrl<>MC then keybd_event(VK_CONTROL,0,Byte(MC)*KEYEVENTF_KEYUP,0); if Alt<>MA then keybd_event(VK_MENU,0,Byte(MA)*KEYEVENTF_KEYUP,0); if Shift<>MS then keybd_event(VK_SHIFT,0,Byte(MS)*KEYEVENTF_KEYUP,0); // Press key keybd_event(VK,0,0,0); keybd_event(VK,0,KEYEVENTF_KEYUP,0); // Release modifier keys if necessary if Ctrl<>MC then keybd_event(VK_CONTROL,0,Byte(Ctrl)*KEYEVENTF_KEYUP,0); if Alt<>MA then keybd_event(VK_MENU,0,Byte(Alt)*KEYEVENTF_KEYUP,0); if Shift<>MS then keybd_event(VK_SHIFT,0,Byte(Shift)*KEYEVENTF_KEYUP,0); end;
4.
procedure SendF10v2; Var findhandle1 : cardinal; begin findhandle1 := FindWindow(NIL, 'Business Plus - Test site: Online Catalogue'); ShowWindow(findhandle1,SW_SHOW); SetForegroundWindow(findhandle1); SendMessage(findhandle1, WM_KEYDOWN, VK_F10, 0); SendMessage(findhandle1, WM_KEYUP, VK_F10, 0); end;
5.
uses Vcl.Touch.Keyboard, Vcl.Touch.KeyboardTypes; type procedure FormCreate(Sender: TObject); protected {ie dont make form active} procedure CreateParams(var Params: TCreateParams); override; {ie dont make keyboard form active} procedure TForm1.CreateParams(var Params: TCreateParams); begin inherited CreateParams(Params); with Params do begin ExStyle := ExStyle or WS_EX_NOACTIVATE; WndParent := GetDesktopwindow; end; end; procedure TForm1.SpeedButton1Click(Sender: TObject); var KeyData: TKeyData; begin KeyData := VKey(VK_F10, -1); SendKey(KeyData, ksDown); SendKey(KeyData, ksUp); end;
6. (VBS)
Option Explicit Dim objShell Set objShell = CreateObject("WScript.Shell") objShell.Run """C:\Program Files (x86)\SellerDeck 2013\Catalog.exe"" /s ""Test site"" /n Administrator /w Administrator", 0, True Wscript.Sleep 10000 objShell.SendKeys "%F" Wscript.Sleep 1000 objShell.SendKeys "%F" Wscript.Sleep 1000 objShell.SendKeys "%F" Wscript.Sleep 1000 objShell.SendKeys "%F" Wscript.Sleep 1000 objShell.SendKeys "%F" Wscript.Sleep 1000 objShell.SendKeys "%F" WScript.Quit
As I said, from these works in notepad, otherwise I would have thought that it was my computer that was the problem. I am trying to automate some basic tasks in this Salesdeck program. Is there any point to accurately simulate a keystroke? I hope I just missed something really basic.