typedef bool (WINAPI * InitializeWinIo)();
InitializeWinIo initializeWinIo;
typedef void (WINAPI * ShutdownWinIo)();
ShutdownWinIo shutdownWinIo;
typedef bool (WINAPI * GetPortVal)(WORD wPortAddr, PDWORD pdwPortVal, BYTE bSize);
GetPortVal getPortVal;
typedef bool (WINAPI * SetPortVal)(WORD wPortAddr, DWORD dwPortVal, BYTE bSize);
SetPortVal setPortVal;
HINSTANCE hDLL = NULL;
bool LoadFunWinio()
{
if (hDLL != NULL)
{
return true;
}
hDLL = LoadLibrary(_T("winio32.dll"));
if (hDLL == NULL)
{
return false;
}
initializeWinIo = (InitializeWinIo)GetProcAddress(hDLL, ("InitializeWinIo"));
if (!initializeWinIo)
{
// handle the error
FreeLibrary(hDLL);
return false;
}
shutdownWinIo = (ShutdownWinIo)GetProcAddress(hDLL, ("ShutdownWinIo"));
if (!shutdownWinIo)
{
// handle the error
FreeLibrary(hDLL);
return false;
}
getPortVal = (GetPortVal)GetProcAddress(hDLL, ("GetPortVal"));
if (!getPortVal)
{
// handle the error
FreeLibrary(hDLL);
return false;
}
setPortVal = (SetPortVal)GetProcAddress(hDLL, ("SetPortVal"));
if (!setPortVal)
{
// handle the error
FreeLibrary(hDLL);
return false;
}
}
const int KBC_KEY_CMD = 0x64;
const int KBC_KEY_DATA = 0x60;
///等待键盘缓冲区为空
void KBCWait4IBE()
{
DWORD dwVal = 0;
bool flag = getPortVal(0x64, &dwVal, 1);
while ((dwVal & 0x2) > 0);
{
Sleep(100);
bool flag = getPortVal(0x64, &dwVal, 1);
}
}
/// 模拟键盘标按下
void MyKeyDown(int vKeyCoad)
{
bool bRtn = true;
int btScancode = 0;
btScancode = MapVirtualKey(vKeyCoad, 0);
KBCWait4IBE(); // 等待键盘缓冲区为空
bRtn = setPortVal(KBC_KEY_CMD, 0xD2, 1);// 发送命令
KBCWait4IBE();
bRtn = setPortVal(KBC_KEY_DATA, 0xe2, 1);// 写入按键信息
KBCWait4IBE(); // '等待键盘缓冲区为空
bRtn = setPortVal(KBC_KEY_CMD, 0xD2, 1);// '发送键盘写入命令
KBCWait4IBE();
bRtn = setPortVal(KBC_KEY_DATA, btScancode, 1);// '写入按下键
}
void MyKeyUp(int vKeyCoad)
{
bool bRtn = true;
int btScancode;
btScancode=MapVirtualKey(byte(vKeyCoad),0);
KBCWait4IBE();
bRtn = setPortVal(KBC_KEY_CMD,0xD2,1);
KBCWait4IBE();
bRtn = setPortVal(KBC_KEY_DATA,0xE0,1);
KBCWait4IBE();
bRtn = setPortVal(KBC_KEY_CMD,0xD2,1);
KBCWait4IBE();
bRtn = setPortVal(KBC_KEY_DATA,(btScancode|0x80),1);
}
/// 模拟鼠标弹出
void MyMouseUp(int vKeyCoad)
{
int btScancode = 0;
btScancode = MapVirtualKey((byte)vKeyCoad, 0);
KBCWait4IBE(); // '等待键盘缓冲区为空
setPortVal(KBC_KEY_CMD, 0xD3, 1); //'发送写入命令
KBCWait4IBE();
setPortVal(KBC_KEY_DATA, (btScancode | 0x80), 1);// '写入信息释放键
}
void CcalldllDlg::OnBnClickedOk()
{
if(!LoadFunWinio())
{
MessageBox(_T("load err"));
return;
}
if(!initializeWinIo())
{
MessageBox(_T("load err"));
}
}
void CcalldllDlg::OnBnClickedButton1()
{
if (hDLL != NULL)
{
shutdownWinIo();
}
}
void CcalldllDlg::OnBnClickedCancel()
{
OnBnClickedButton1();
OnCancel();
}
void CcalldllDlg::OnBnClickedCheck1()
{
this->UpdateData();
if(m_timebegin)
{
this->SetTimer(1, 800, NULL);
}
else
{
this->KillTimer(1);
}
}
void CcalldllDlg::OnTimer(UINT_PTR nIDEvent)
{
MyKeyDown('a');
Sleep(100);
MyKeyUp('a');
}
winio 模拟按键(无PS2键盘,未测试)
最新推荐文章于 2022-02-08 22:28:57 发布