比如我在Shift键按下时要执行一段代码.

解决方案 »

  1.   

    用keydown()或keypress()函数实现。
    keypress(iKeyAscii)
    iKeyAscii
    整型,标准ANSI码
    而keydown(iKeyCode,iShiftState)
    iKeyCode 
    Integer value specifying a key code, such as vbKeyF1 (the F1 key) or vbKeyHome (the HOME key).  
    iShiftState 
    Integer value specifying a bit field with bits corresponding to the SHIFT key (bit 0), the CTRL key (bit 1), and the ALT key (bit 2). These bits correspond to the values 1, 2, and 4, respectively. Some, all, or none of the bits can be set, indicating that some, all, or none of the keys are pressed.
    然后可以利用if语句,比如
    if (KeyPress(...)) then
     {
      ...
      }
      

  2.   

    BOOL CXXXDlg::PreTranslateMessage(MSG* pMsg) 
    {
        if (pMsg->message==WM_KEYDOWN)
    {
    if(pMsg->wParam==VK_SHIFT)
    MessageBox("shift被按下了");
      }
    }
      

  3.   


    void CTestDlg::OnSysChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
    {
    if (nChar == VK_F1) AfxMessageBox("F1"); CDialog::OnSysChar(nChar, nRepCnt, nFlags);
    }
    void CTestDlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
    {
      CDialog::OnChar(nChar, nRepCnt, nFlags);
    }
      

  4.   

    BOOL CSize1Dlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if( WM_KEYDOWN == pMsg->message )
    {
    if( GetKeyState( VK_SHIFT ) < 0 )//大写的字符A
    {
    AfxMessageBox( "Shift is pressed" );
    return TRUE;
    }
    }
    return CDialog::PreTranslateMessage(pMsg);
    }
      

  5.   

    BOOL CSize1Dlg::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    if( WM_KEYDOWN == pMsg->message )
    {
    if( GetKeyState( VK_SHIFT ) < 0 )
    {
    AfxMessageBox( "Shift is pressed" );
    return TRUE;
    }
    }
    return CDialog::PreTranslateMessage(pMsg);
    }