1

I'm looking for C++ help with virtual key combination, I'm looking for a simple key combination to do a action. I'm trying to get Shift+G to work. I've tried what I know and it's given me different results but nothing what I'm trying to do.

if (VK_SHIFT & 0x47) // Shift+G test
{
    if(::GetKeyState(0x47)<0);      
    {
        CBaseItem* pItemInfo = g_pItemTableHash->GetData(
            g_pMainPlayer->m_pInv_Guardian[0].GetID());

        if (GUARDIAN_ITEM_TYPE_ADULT == pItemInfo->BaseItem_Guardian.bType)
        {
            if(CUserInterface::GetInstance()->m_bGuardian==FALSE)
            {               
                // ON //
                CTDS_DUNGEON_CALLGARDIAN CallGuardinan;
                CallGuardinan.bZipCode = 0;
                g_pNet->SendMsg( (char*)&CallGuardinan, CallGuardinan.GetPacketSize(),
                    SERVER_INDEX_ZONE);
            }
        }
    }
}

all that really matters is the first line, any one know how I can fix this so key combination SHIFT+G works?

Jashaszun
  • 9,207
  • 3
  • 29
  • 57

1 Answers1

2

The biggest problem in your code is the ; at the end of

 if(::GetKeyState(0x47)<0);      // !!!!! 

The ; causes the following indented inner if-block to be executed in any case !

Here a similar simplified code that works very well:

    while(true) {
        if(::GetKeyState(VK_SHIFT) & 0x8000)  // is shift pressed ?  
        {
            if(::GetKeyState(0x47) & 0x8000)  // if G also pressed ?  
            {
                std::cout << "Shift G pressed" << std::endl;
                break;
            }
            else std::cout << "Shift..." << std::endl;
        }
    }
}

Additional infos:

The expression (VK_SHIFT & 0x47) is a constant which is completely independent of the input. VK_SHIFT is 0x10 and 0x10 & 0x47 yields 0. So your outer if-block gets never executed.

Christophe
  • 68,716
  • 7
  • 72
  • 138