- void CDemoDlg::OnTest1()
- {
- //模拟NUM LOCK按键
- keybd_event(VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
- keybd_event(VK_NUMLOCK, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
- }
- void CDemoDlg::OnTest2()
- {
- //模拟CAPS LOCK按键
- keybd_event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
- keybd_event(VK_CAPITAL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
- }
- void CDemoDlg::OnTest3()
- {
- //模拟SCROLL LOCK按键
- keybd_event(VK_SCROLL, 0, KEYEVENTF_EXTENDEDKEY | 0, 0);
- keybd_event(VK_SCROLL, 0, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
- }
复制代码
可以添加三个复选框来测试上面的代码。按钮状态 的自动获取,我们可以在PreTranslateMessage函数中添加。
- BOOL CDemoDlg::PreTranslateMessage(MSG* pMsg)
- {
- if (pMsg->message == WM_KEYUP)
- {
- BYTE KeyboardState[256];
- //获得虚拟键状态
- ::GetKeyboardState(KeyboardState);
- if (KeyboardState[VK_NUMLOCK] & 0x01)
- {
- CButton* pButton = (CButton*)GetDlgItem(IDC_TEST1);
- pButton->SetCheck(TRUE);
- }
- else
- {
- CButton* pButton = (CButton*)GetDlgItem(IDC_TEST1);
- pButton->SetCheck(FALSE);
- }
- if (KeyboardState[VK_CAPITAL] & 0x01)
- {
- CButton* pButton = (CButton*)GetDlgItem(IDC_TEST2);
- pButton->SetCheck(TRUE);
- }
- else
- {
- CButton* pButton = (CButton*)GetDlgItem(IDC_TEST2);
- pButton->SetCheck(FALSE);
- }
- if (KeyboardState[VK_SCROLL] & 0x01)
- {
- CButton* pButton = (CButton*)GetDlgItem(IDC_TEST3);
- pButton->SetCheck(TRUE);
- }
- else
- {
- CButton* pButton = (CButton*)GetDlgItem(IDC_TEST3);
- pButton->SetCheck(FALSE);
- }
- }
- return CDialog::PreTranslateMessage(pMsg);
- }
复制代码
上位机MFC如何模拟键盘按键
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|