Keypress事件到一个不专心的窗口 - Win32api

时间:2018-03-05 21:20:44

标签: python python-2.7 automation bots win32gui

我想编写一个模拟按键和释放按键的脚本,以便输入进入特定窗口,即使它不是焦点,也可以是背景或最小化。我想知道这是否可能。

我制作的脚本每隔2秒就完成一次,当聚焦窗口(输入位置)是我决定的那个,否则没有任何反应,然后它再次循环。但这不是我的目标。

#include "stdio.h"

struct parameter
{
  int field;
};

struct configuration {
  parameter** paramList;
  int paramListSize;
  parameter param1;
  parameter param2;
  parameter param3;
};

parameter *initParameter(parameter *param, int value, int *paramListSize)
{
  param->field = value;
  (*paramListSize)++;
  return param;
}

int initConfig(configuration *config)
{
  config->paramListSize = 0;
  parameter* test[] =
  {
    initParameter(&config->param1, 100, &config->paramListSize),
    initParameter(&config->param2, 200, &config->paramListSize),
    initParameter(&config->param3, 300, &config->paramListSize)
  };
  config->paramList = test;

  // dirty failed attemp to fix :-)
  for (int parameterIndex = 0; parameterIndex < config->paramListSize; parameterIndex++)
  {
    config->paramList[parameterIndex] = test[parameterIndex];
  }
}

void printConfig(configuration *config)
{
  for (int parameterIndex = 0; parameterIndex < config->paramListSize; parameterIndex++)
  {
    printf("param%d:%d\n", parameterIndex + 1, config->paramList[parameterIndex]->field);
  }
}

int main(void)
{
  configuration config;
  initConfig(&config);
  printConfig(&config);
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

 this solution is for c++ but i think method's name should be same as defined in 
 win32api python 

 consider hwnd as window handle
 and Vk_A as value of key which you want to remotely pressed and released
 you need to use "SendMessage" or "PostMessage" method

 if you know window handle you can use 
   SendMessage(hwnd, WM_SETFOCUS, 0, 0) 
 or
   SendMessage(hwnd, WM_ACTIVATE, WA_ACTIVE, 0)

 then you need to use following methods together
   SendMessage(hwnd, WM_KEYDOWN, Vk_A, 0)
   SendMessage(hwnd, WM_KEYUP, Vk_A, 0)
相关问题