C++模拟键盘命令的代码

发布于 2024-12-03 03:36:38 字数 1883 浏览 0 评论 0原文

如果相关的内容已经存在,请原谅我创建一个文件,但我搜索了太久,我的头几乎融化了。

那么,说到重点了。有人可以向我描述一下我需要编写的代码,以便让 C++ 应用程序发送与我同时手动按下“ctrl”键和“a”键时相同的信号吗?

我发现了很多其他链接:

http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=VS.90).aspx

显然,但是,功能不起作用。我尝试使用:

using namespace System::Windows::Forms;

但是,它什么也没做。

显然我需要包含代码。这是我的代码。我了解基础知识,是的,我可以阅读。所以,我已经包括了:

#define _WIN32_WINNT 0x0501
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <winable.h> /* Dev-C++ specific */

using namespace std;

HWND GameWindow = FindWindow(0, "C:\\Users\\...\\src\\output1.txt");


/* This is a function to simplify usage of sending keys */
void GenerateKey(int vk, BOOL bExtended) {

    KEYBDINPUT  kb = {0};
    INPUT       Input = {0};

    /* Generate a "key down" */
    if (bExtended) { kb.dwFlags  = KEYEVENTF_EXTENDEDKEY; }
    kb.wVk  = vk;
    Input.type  = INPUT_KEYBOARD;
    Input.ki  = kb;
    ::SendInput(1, &Input, sizeof(Input));

    /* Generate a "key up" */
    ZeroMemory(&kb, sizeof(KEYBDINPUT));
    ZeroMemory(&Input, sizeof(INPUT));
    kb.dwFlags  =  KEYEVENTF_KEYUP;
    if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
    kb.wVk = vk;
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;
    ::SendInput(1, &Input, sizeof(Input));

    return;
}

int main() {


    system("start C:\\Users\\...\\src\\output1.txt");
    SetForegroundWindow(GameWindow);

    GenerateKey ('A', FALSE);
    GenerateKey (VK_CAPITAL, TRUE);

    GenerateKey(0x0D, FALSE); /* enter key */

    getch();
    return 0;
}

现在我的问题是,程序实际上并没有将此 A 写入该 .txt 文件。

注意:如果从 CMD 运行,它会起作用,在实际的 CMD 上打印“A”。

pardon me for making a file if something relevant already exists but I have searched for too long and my head has almost melted.

So, to the point. Can someone describe to me the code that I need to write to have a c++ application send the same signal as when I manually press the keys 'ctrl' and 'a' at the same time?

I found, among many others, this link:

http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.send(v=VS.90).aspx

Apparently, though, function does not work. I tried to use:

using namespace System::Windows::Forms;

But, it does nothing.

Apparently I need to include code. This is my code. I know the basics and yes I can read. So, I have included that:

#define _WIN32_WINNT 0x0501
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <winable.h> /* Dev-C++ specific */

using namespace std;

HWND GameWindow = FindWindow(0, "C:\\Users\\...\\src\\output1.txt");


/* This is a function to simplify usage of sending keys */
void GenerateKey(int vk, BOOL bExtended) {

    KEYBDINPUT  kb = {0};
    INPUT       Input = {0};

    /* Generate a "key down" */
    if (bExtended) { kb.dwFlags  = KEYEVENTF_EXTENDEDKEY; }
    kb.wVk  = vk;
    Input.type  = INPUT_KEYBOARD;
    Input.ki  = kb;
    ::SendInput(1, &Input, sizeof(Input));

    /* Generate a "key up" */
    ZeroMemory(&kb, sizeof(KEYBDINPUT));
    ZeroMemory(&Input, sizeof(INPUT));
    kb.dwFlags  =  KEYEVENTF_KEYUP;
    if (bExtended) { kb.dwFlags |= KEYEVENTF_EXTENDEDKEY; }
    kb.wVk = vk;
    Input.type = INPUT_KEYBOARD;
    Input.ki = kb;
    ::SendInput(1, &Input, sizeof(Input));

    return;
}

int main() {


    system("start C:\\Users\\...\\src\\output1.txt");
    SetForegroundWindow(GameWindow);

    GenerateKey ('A', FALSE);
    GenerateKey (VK_CAPITAL, TRUE);

    GenerateKey(0x0D, FALSE); /* enter key */

    getch();
    return 0;
}

Now my problem is that, the program is not actually writing this A onto that .txt file.

NOTE: It works if ran from CMD, printing the 'A' on the actual CMD.

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

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

发布评论

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

评论(2

甜宝宝 2024-12-10 03:36:38

查看 Windows API 中的 SendInput

codeguru 论坛 粘贴的示例副本:

void GenerateKey ( int vk , BOOL bExtended)
{
  KEYBDINPUT  kb={0};
  INPUT    Input={0};
  // generate down 
  if ( bExtended )
    kb.dwFlags  = KEYEVENTF_EXTENDEDKEY;
  kb.wVk  = vk;  
  Input.type  = INPUT_KEYBOARD;

  Input.ki  = kb;
  ::SendInput(1,&Input,sizeof(Input));

  // generate up 
  ::ZeroMemory(&kb,sizeof(KEYBDINPUT));
  ::ZeroMemory(&Input,sizeof(INPUT));
  kb.dwFlags  =  KEYEVENTF_KEYUP;
  if ( bExtended )
    kb.dwFlags  |= KEYEVENTF_EXTENDEDKEY;

  kb.wVk    =  vk;
  Input.type  =  INPUT_KEYBOARD;
  Input.ki  =  kb;
  ::SendInput(1,&Input,sizeof(Input));
}

用法:

GenerateKey ('A', FALSE);
GenerateKey (VK_CAPITAL, TRUE);
GenerateKey ('A', FALSE);

另外,发送键盘输入到另一个进程,您将需要 AttachThreadInput

Have a look at the SendInput from the Windows API.

Example copy pasted from codeguru forums:

void GenerateKey ( int vk , BOOL bExtended)
{
  KEYBDINPUT  kb={0};
  INPUT    Input={0};
  // generate down 
  if ( bExtended )
    kb.dwFlags  = KEYEVENTF_EXTENDEDKEY;
  kb.wVk  = vk;  
  Input.type  = INPUT_KEYBOARD;

  Input.ki  = kb;
  ::SendInput(1,&Input,sizeof(Input));

  // generate up 
  ::ZeroMemory(&kb,sizeof(KEYBDINPUT));
  ::ZeroMemory(&Input,sizeof(INPUT));
  kb.dwFlags  =  KEYEVENTF_KEYUP;
  if ( bExtended )
    kb.dwFlags  |= KEYEVENTF_EXTENDEDKEY;

  kb.wVk    =  vk;
  Input.type  =  INPUT_KEYBOARD;
  Input.ki  =  kb;
  ::SendInput(1,&Input,sizeof(Input));
}

Usage:

GenerateKey ('A', FALSE);
GenerateKey (VK_CAPITAL, TRUE);
GenerateKey ('A', FALSE);

Also, to send keyboard input to another process you will need AttachThreadInput.

苦妄 2024-12-10 03:36:38

我会查看 AutoHotKey 源。

编辑:添加了 github 链接

I'd look at the AutoHotKey source.

Edit: added github link

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