visual-c++mfc

How to handle Key press ctrl+shift+A in sdi mfc


I am new to vc++.How to handle key press Ctrl+Shift+A in sdi mfc. For Ctrl+A the code is working fine.

case _T('A'): 

     if(GetKeyState(VK_CONTROL) & 0x8000){
         MessageBox(_T("Key Ctrl+A is pressed"));
     }
     else if((GetKeyState(VK_CONTROL) & 0x8000)&&(GetKeyState(VK_SHIFT) & 0x8000)){
         MessageBox(_T("Key Ctrl+Shift+A is pressed"));
     }
    
    break;

Solution

  • Your first if-clause is true, whether the Shift key is pressed or not, so you'll never reach the else-clause. If you change the order of your statements, you'll get both:

    case _T( 'A' ): 
        if ( ( GetKeyState( VK_CONTROL ) < 0 ) && ( GetKeyState( VK_SHIFT ) < 0 ) {
            MessageBox( _T( "Key Ctrl+Shift+A is pressed" ) );
        } else if ( GetKeyState( VK_CONTROL ) < 0 ) {
            MessageBox( _T( "Key Ctrl+A is pressed" ) );
        }
        break;
    

    If you want to handle key presses globally, you could use Keyboard Accelerators instead. The most straightforward way to set up accelerators is through an ACCELERATORS resource.