0

I'm trying to simulate a CTRL + V keypress (and release) by using the INPUT struct in windows.h, but I can only get it to work in simple programs such as notepad, whereas in the Steam client it doesn't actually write to it:

    INPUT ip[4];
    ip[0].type = INPUT_KEYBOARD;
    ip[0].ki.wScan = 0;
    ip[0].ki.dwFlags = 0;
    ip[0].ki.wVk = VK_CONTROL;
    ip[0].ki.time = 0;
    ip[0].ki.dwExtraInfo = 0;

    ip[1].type = INPUT_KEYBOARD;
    ip[1].ki.wScan = 0;
    ip[1].ki.dwFlags = 0;
    ip[1].ki.wVk = 0x56;
    ip[1].ki.time = 0;
    ip[1].ki.dwExtraInfo = 0;

    ip[2].type = INPUT_KEYBOARD;
    ip[2].ki.wScan = 0;
    ip[2].ki.dwFlags = KEYEVENTF_KEYUP;
    ip[2].ki.wVk = VK_CONTROL;
    ip[2].ki.time = 0;
    ip[2].ki.dwExtraInfo = 0;

    ip[3].type = INPUT_KEYBOARD;
    ip[3].ki.wScan = 0;
    ip[3].ki.dwFlags = KEYEVENTF_KEYUP;
    ip[3].ki.wVk = 0x56;
    ip[3].ki.time = 0;
    ip[3].ki.dwExtraInfo = 0;

    SendInput((sizeof(ip)/sizeof(INPUT)), ip, sizeof(INPUT));

What am I doing / thinking wrong? Full code: https://pastebin.com/CxCfeJar

Will123
  • 55
  • 1
  • 7
  • Your code works for me – Killzone Kid Apr 10 '18 at 15:25
  • Actually it does work, you are right. I realize now that the problem is that it doesn't write to the particular window that I want it to write to. (It writes to Notepad but not to Steam for example). Do you have any idea why this is? – Will123 Apr 10 '18 at 15:36
  • Call `SetForegroundWindow(targetWindow_hwnd)` so keyboard messages go to that window. – Barmak Shemirani Apr 10 '18 at 15:40
  • Please check my update of the post(pastebin - full code), I do have SetForegroundWindow in my code? in main() – Will123 Apr 10 '18 at 15:42
  • Is this a Win32 program or something odd? Give some information about "Steam" You can use Visual Studio's Spy.exe to get windows class information from "Steam" – Barmak Shemirani Apr 10 '18 at 15:53
  • https://i.gyazo.com/f72b22a4d48da96f13e6b0711a813ef5.mp4 I dont know what information is of relevance, so I made a gif of it all in spy++. Also I noticed that If i do SendInput(2, ip, sizeof(INPUT)); It pastes correctly in Steam. However it won't release the key CTRL and V from pushdown afterwards (obviously) - which is a problem. So It feels like I am indeed targeting the correct window. – Will123 Apr 10 '18 at 17:43
  • UI Automation is done using [UI Automation](https://msdn.microsoft.com/en-us/library/windows/desktop/ee684009.aspx). – IInspectable Apr 11 '18 at 07:55
  • I dont quite get what you mean, I am just simulating a Ctrl+v click? It shouldnt really have anything to do with UI Automation? Isn't it just as if i Ctrl+v:ed myself? I dont understand why my code doesn't work for "Steam" but for textdocument. – Will123 Apr 11 '18 at 18:15
  • Thus question is asked here every single day. Try a bit of research. – David Heffernan Apr 14 '18 at 07:47

0 Answers0