工控编程吧
标题: 上位机MFC如何模拟键盘按键 [打印本页]
作者: qq263946146 时间: 2019-8-20 10:27
标题: 上位机MFC如何模拟键盘按键
- 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);
- }
复制代码
(, 下载次数: 0)