Hello everyone,

Is there an equivalent to SendKeys.Send in C++? I have looked into keybd_event and SendInput, but to my knowledge you can only send one key at a time with those functions.

Thanks for any help.

Edit: I'm not using the .Net Framework so I can't use any of that stuff.

Dani AI

Generated

Short answer: use the Win32 SendInput API. SendInput accepts an array of INPUT structures, so you can build a whole key sequence (downs + ups) and submit it in one call instead of trying to fire a single key per call. That covers ’s looping suggestion but is more efficient; keybd_event is deprecated in favor of SendInput. was right that C++ itself has no SendKeys helper — you must call OS APIs. (learn.microsoft.com)

Practical example (Unicode-safe typing — avoids layout issues by using KEYEVENTF_UNICODE):

#include <windows.h>
#include <vector>
#include <string>

std::wstring s = L"Hello";
std::vector<INPUT> in;
for (wchar_t ch : s) {
  INPUT down = {};
  down.type = INPUT_KEYBOARD;
  down.ki.wVk = 0;
  down.ki.wScan = ch;
  down.ki.dwFlags = KEYEVENTF_UNICODE;
  in.push_back(down);

  INPUT up = down;
  up.ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
  in.push_back(up);
}
SendInput((UINT)in.size(), in.data(), sizeof(INPUT));

This pushes WM_KEYDOWN/WM_KEYUP via the Unicode packet mechanism; use virtual-key + scan-code mode if you need physical key simulation (games, special drivers). (learn.microsoft.com)

Caveats and troubleshooting: the target window usually needs focus (foreground rules are restricted by the system), and processes running at different integrity levels or in different sessions may be blocked from injecting input (UIPI / UIAccess / session‑0 issues). If SendInput seems unreliable, ensure the target is in the same session and integrity level, try small delays between events, and use proper key-down then key-up ordering for modifiers. (learn.microsoft.com)

If you control the target application, prefer automation over synthetic input when possible — COM/OLE automation for apps that expose it, or the Windows UI Automation APIs for robust, layout-independent control (these avoid timing/focus fragility of SendInput), as suggested. (learn.microsoft.com)

Recommended Answers

All 4 Replies

Member Avatar for Member #46692

>you can only send one key at a time with those functions

Can't you put it in a loop?

>>Is there an equivalent to SendKeys.Send in C++?

No. C++ language knows nothing (other than what it inherited from C language in stdio.h) about the keyboard because its os specific. You have to use os-specific api function calls to send them one character at a time like jamthwee suggested.

If I had the source of the application being controlled, I would use ole/COM automation instead to avoid the possible hit or miss performance of SendKeys but what would you learn if you could not try? So, I don't know either
BUT I refer you to a codeproject article called 'Sendkeys in C++' because someone thinks it is possible and the article has a high rating.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.