输入字符串到指定的窗口

来源:互联网 发布:linux系统可以ghost 编辑:程序博客网 时间:2024/04/30 06:00

#define UPPERCASE 0X03

#define LOWERCASE 0X02

#define NUMBER  0x01

还有一个CanInput(vWk)就是判断是大写,小写还是数字,再返回指定的值,如UPPERCASE

void SendInputKeypress(CString str,HWND hWnd)
{
 CArray<INPUT,INPUT&> inputList;
 BYTE vWk;
 
 BOOL isShift=FALSE;
 for(int i=0;i<str.GetLength()*2;i+=2)
 {
  vWk=(BYTE)str.GetAt(i/2);
  isShift=this->CanInput(vWk);
  
  if(isShift==LOWERCASE)
  {
   vWk-=32;
  }
  if(isShift==UPPERCASE)
  {
   INPUT in;
   ZeroMemory(&in,sizeof(in));
   in.type=INPUT_KEYBOARD;
   in.ki.dwFlags=0;
   in.ki.wVk=VK_SHIFT;
   inputList.Add(in);
  }

  INPUT Keys[2];
  ZeroMemory(Keys,sizeof(Keys));
  Keys[0].type=Keys[1].type=INPUT_KEYBOARD;
  Keys[0].ki.wVk  = Keys[1].ki.wVk = vWk;
  Keys[1].ki.dwFlags = KEYEVENTF_KEYUP;  // THIS IS IMPORTANT
  inputList.Add(Keys[0]);
  inputList.Add(Keys[1]);

  if(isShift==UPPERCASE)
  {
   INPUT in;
   ZeroMemory(&in,sizeof(in));
   in.type=INPUT_KEYBOARD;
   in.ki.dwFlags=KEYEVENTF_KEYUP;
   in.ki.wVk=VK_SHIFT;
   inputList.Add(in);
  }
 }
 LPINPUT AllKey=new INPUT[inputList.GetCount()];
 for(int i=0;i<inputList.GetCount();i++)
 {
  AllKey[i]=inputList.GetAt(i);
 }
 
 SetForegroundWindow(hWnd);
 while(!::SendInput((UINT)inputList.GetCount(), AllKey, sizeof(INPUT)))
 {
  SetForegroundWindow(hWnd);
 }
    delete[] AllKey;

原创粉丝点击