开发者

C++ code to mimic keyboard commands

开发者 https://www.devze.com 2023-04-03 15:00 出处:网络
pardon me for making a file if something relevant already exists but I have searched for too long and my head has almost melted.

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 开发者_如何学JAVAto 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.


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.


I'd look at the AutoHotKey source.

Edit: added github link

0

精彩评论

暂无评论...
验证码 换一张
取 消