模拟键盘 keybord

点击打开链接 

 

这里是模拟键盘的主要代码:


void NotifyKBEvent(wchar_t ch)
{
 SHORT vks = VkKeyScanW(ch);
 BYTE vk = LOBYTE(vks);
 BYTE Shift = HIBYTE(vks);
 if (vk == (BYTE)-1/* || Shift == (BYTE)-1*/)
 {//UNICODE 字符
  INPUT input[2];
  input[0].type = INPUT_KEYBOARD;
  input[0].ki.wVk = 0;
  input[0].ki.wScan = ch;
  input[0].ki.dwFlags = 0x4;//KEYEVENTF_UNICODE;
  input[1].type = INPUT_KEYBOARD;
  input[1].ki.wVk = 0;
  input[1].ki.wScan = ch;
  input[1].ki.dwFlags = KEYEVENTF_KEYUP | 0x4;//KEYEVENTF_UNICODE;
  SendInput(2, input, sizeof(INPUT));
 }
 else
 {//
  if (Shift)
  {
   INPUT input[4] = {0};
   input[0].type = INPUT_KEYBOARD;
   input[0].ki.wVk = Shift;//VK_SHIFT;
   input[1].type = INPUT_KEYBOARD;
   input[1].ki.wVk = ch;
   input[2].type = INPUT_KEYBOARD;
   input[2].ki.wVk = ch;
   input[2].ki.dwFlags = KEYEVENTF_KEYUP;
   input[3].type = INPUT_KEYBOARD;
   input[3].ki.wVk = Shift;//VK_SHIFT;
   input[3].ki.dwFlags = KEYEVENTF_KEYUP;
   SendInput(4, input, sizeof(INPUT));
  }
  else
  {
   INPUT input[2] = {0};
   input[0].type = INPUT_KEYBOARD;
   input[0].ki.wVk = vks;
   input[1].type = INPUT_KEYBOARD;
   input[1].ki.wVk = vks;
   input[1].ki.dwFlags = KEYEVENTF_KEYUP;
   SendInput(2, input, sizeof(INPUT));
  }
 }
}

void NotifyKBEvent(wchar_t* chs)
{
 if (chs == NULL)
  return ;
 while(*chs) NotifyKBEvent(*chs++);
}

void SendKBEvent(WORD wVk, DWORD dwFlags = 0, DWORD dwExtraInfo = 0)
{
 INPUT input[1] = {0};
 input[0].type = INPUT_KEYBOARD;
 input[0].ki.wVk = wVk;
 input[0].ki.wScan = MapVirtualKey(wVk, 0);
 input[0].ki.dwFlags = dwFlags;
 input[0].ki.dwExtraInfo = dwExtraInfo;
 input[0].ki.time = GetTickCount();
 SendInput(1, input, sizeof(INPUT));
}

 

//这里是处理标题栏和不活动代码:

//去掉任务栏图标 和 始终不处于活动状态
 ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW | 0x08000000);
 //初始不活动
 SetWindowPos(&CWnd::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);

 

源码:点击打开链接 

 

void NotifyKBEvent(wchar_t ch) { SHORT vks = VkKeyScanW(ch); BYTE vk = LOBYTE(vks); BYTE Shift = HIBYTE(vks); if (vk == (BYTE)-1/* || Shift == (BYTE)-1*/) {//UNICODE 字符 INPUT input[2]; input[0].type = INPUT_KEYBOARD; input[0].ki.wVk = 0; input[0].ki.wScan = ch; input[0].ki.dwFlags = 0x4;//KEYEVENTF_UNICODE; input[1].type = INPUT_KEYBOARD; input[1].ki.wVk = 0; input[1].ki.wScan = ch; input[1].ki.dwFlags = KEYEVENTF_KEYUP | 0x4;//KEYEVENTF_UNICODE; SendInput(2, input, sizeof(INPUT)); } else {// if (Shift) { INPUT input[4] = {0}; input[0].type = INPUT_KEYBOARD; input[0].ki.wVk = Shift;//VK_SHIFT; input[1].type = INPUT_KEYBOARD; input[1].ki.wVk = ch; input[2].type = INPUT_KEYBOARD; input[2].ki.wVk = ch; input[2].ki.dwFlags = KEYEVENTF_KEYUP; input[3].type = INPUT_KEYBOARD; input[3].ki.wVk = Shift;//VK_SHIFT; input[3].ki.dwFlags = KEYEVENTF_KEYUP; SendInput(4, input, sizeof(INPUT)); } else { INPUT input[2] = {0}; input[0].type = INPUT_KEYBOARD; input[0].ki.wVk = vks; input[1].type = INPUT_KEYBOARD; input[1].ki.wVk = vks; input[1].ki.dwFlags = KEYEVENTF_KEYUP; SendInput(2, input, sizeof(INPUT)); } } } void NotifyKBEvent(wchar_t* chs) { if (chs == NULL) return ; while(*chs) NotifyKBEvent(*chs++); } void SendKBEvent(WORD wVk, DWORD dwFlags = 0, DWORD dwExtraInfo = 0) { INPUT input[1] = {0}; input[0].type = INPUT_KEYBOARD; input[0].ki.wVk = wVk; input[0].ki.wScan = MapVirtualKey(wVk, 0); input[0].ki.dwFlags = dwFlags; input[0].ki.dwExtraInfo = dwExtraInfo; input[0].ki.time = GetTickCount(); SendInput(1, input, sizeof(INPUT)); } //去掉任务栏图标 和 始终不处于活动状态 ModifyStyleEx(WS_EX_APPWINDOW,WS_EX_TOOLWINDOW | 0x08000000); //初始不活动 SetWindowPos(&CWnd;::wndTopMost, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值