如何使用 WriteConsoleInput/WriteConsole 将 Return/Enter 字符发送到控制台

发布于 2024-11-04 15:25:53 字数 2438 浏览 1 评论 0原文

我有以下代码。我想发送命令“uci”,然后返回/输入到控制台。但是,我这样做没有成功。请各位指教。谢谢你!

#include <conio.h>
#include <iostream>
#include <windows.h>

DWORD WINAPI test(LPVOID lp)
{
    system("edit");

    return 0;
}

int main(int argc, char* argv[])
{
    /*
    HANDLE hConIn = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, 0);
    HANDLE hConOut = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, 0);
    */
    HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD dwTmp;
    INPUT_RECORD ir[4];

    CreateThread(NULL, 0, test,(LPDWORD)0, 0, &dwTmp);

    Sleep( 1000 );

    ir[0].EventType = KEY_EVENT;
    ir[0].Event.KeyEvent.bKeyDown = TRUE;
    ir[0].Event.KeyEvent.dwControlKeyState = 0;
    ir[0].Event.KeyEvent.uChar.UnicodeChar = 'u';
    ir[0].Event.KeyEvent.wRepeatCount = 1;
    ir[0].Event.KeyEvent.wVirtualKeyCode = 'U';
    ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('U', MAPVK_VK_TO_VSC);

    ir[1].EventType = KEY_EVENT;
    ir[1].Event.KeyEvent.bKeyDown = TRUE;
    ir[1].Event.KeyEvent.dwControlKeyState = 0;
    ir[1].Event.KeyEvent.uChar.UnicodeChar = 'c';
    ir[1].Event.KeyEvent.wRepeatCount = 1;
    ir[1].Event.KeyEvent.wVirtualKeyCode = 'C';
    ir[1].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('C', MAPVK_VK_TO_VSC);

    ir[2].EventType = KEY_EVENT;
    ir[2].Event.KeyEvent.bKeyDown = TRUE;
    ir[2].Event.KeyEvent.dwControlKeyState = 0;
    ir[2].Event.KeyEvent.uChar.UnicodeChar = 'i';
    ir[2].Event.KeyEvent.wRepeatCount = 1;
    ir[2].Event.KeyEvent.wVirtualKeyCode = 'I';
    ir[2].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('I', MAPVK_VK_TO_VSC);

    ir[3].EventType = KEY_EVENT;
    ir[3].Event.KeyEvent.bKeyDown = TRUE;
    ir[3].Event.KeyEvent.dwControlKeyState = 0;
    ir[3].Event.KeyEvent.uChar.UnicodeChar = '\n';
    ir[3].Event.KeyEvent.wRepeatCount = 1;
    ir[3].Event.KeyEvent.wVirtualKeyCode = '\n';
    ir[3].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('\n', MAPVK_VK_TO_VSC);

    dwTmp = 0;
    WriteConsoleInput(hConIn, ir, 4, &dwTmp);
    //FlushConsoleInputBuffer(hConIn);
    //WriteConsole(hConIn, "uci\n", 5, &dwTmp, NULL);
    //printf(" dwTmp = %d ", dwTmp);

    Sleep(1000);

    return 0;
}

I have the following piece of code. I want to send the command "uci" and then Return/Enter to the console. However, I got no success doing so. Please kindly advise. Thank you!

#include <conio.h>
#include <iostream>
#include <windows.h>

DWORD WINAPI test(LPVOID lp)
{
    system("edit");

    return 0;
}

int main(int argc, char* argv[])
{
    /*
    HANDLE hConIn = CreateFile("CONIN$", GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, 0);
    HANDLE hConOut = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ, NULL, OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, 0);
    */
    HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
    HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD dwTmp;
    INPUT_RECORD ir[4];

    CreateThread(NULL, 0, test,(LPDWORD)0, 0, &dwTmp);

    Sleep( 1000 );

    ir[0].EventType = KEY_EVENT;
    ir[0].Event.KeyEvent.bKeyDown = TRUE;
    ir[0].Event.KeyEvent.dwControlKeyState = 0;
    ir[0].Event.KeyEvent.uChar.UnicodeChar = 'u';
    ir[0].Event.KeyEvent.wRepeatCount = 1;
    ir[0].Event.KeyEvent.wVirtualKeyCode = 'U';
    ir[0].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('U', MAPVK_VK_TO_VSC);

    ir[1].EventType = KEY_EVENT;
    ir[1].Event.KeyEvent.bKeyDown = TRUE;
    ir[1].Event.KeyEvent.dwControlKeyState = 0;
    ir[1].Event.KeyEvent.uChar.UnicodeChar = 'c';
    ir[1].Event.KeyEvent.wRepeatCount = 1;
    ir[1].Event.KeyEvent.wVirtualKeyCode = 'C';
    ir[1].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('C', MAPVK_VK_TO_VSC);

    ir[2].EventType = KEY_EVENT;
    ir[2].Event.KeyEvent.bKeyDown = TRUE;
    ir[2].Event.KeyEvent.dwControlKeyState = 0;
    ir[2].Event.KeyEvent.uChar.UnicodeChar = 'i';
    ir[2].Event.KeyEvent.wRepeatCount = 1;
    ir[2].Event.KeyEvent.wVirtualKeyCode = 'I';
    ir[2].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('I', MAPVK_VK_TO_VSC);

    ir[3].EventType = KEY_EVENT;
    ir[3].Event.KeyEvent.bKeyDown = TRUE;
    ir[3].Event.KeyEvent.dwControlKeyState = 0;
    ir[3].Event.KeyEvent.uChar.UnicodeChar = '\n';
    ir[3].Event.KeyEvent.wRepeatCount = 1;
    ir[3].Event.KeyEvent.wVirtualKeyCode = '\n';
    ir[3].Event.KeyEvent.wVirtualScanCode = MapVirtualKey('\n', MAPVK_VK_TO_VSC);

    dwTmp = 0;
    WriteConsoleInput(hConIn, ir, 4, &dwTmp);
    //FlushConsoleInputBuffer(hConIn);
    //WriteConsole(hConIn, "uci\n", 5, &dwTmp, NULL);
    //printf(" dwTmp = %d ", dwTmp);

    Sleep(1000);

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘过的浮云 2024-11-11 15:25:53

\n 为保留键码;使用 VK_RETURN 代替。

来自 https://learn.microsoft.com/en -us/windows/win32/inputdev/virtual-key-codes

\n is a reserved key code; use VK_RETURN instead.

From https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文