정리

[Win32 API] GetAsyncKeyState

저장소/VC++
참고 : http://debugjung.tistory.com/trackback/364
MSDN : http://msdn.microsoft.com/ko-kr/site/ms646293

마우스의 버튼을 동시에 눌렀을 때 상태를 확인하는 방법.
마우스가 아닌 다른 키도 가능.

일단 테스트를 위해 WM_LBUTTONDOWN, WM_RBOUTTONDOWN 메시지를 확인한다.

void CTestDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default

    SHORT nAsyncKey = GetAsyncKeyState(VK_RBUTTON);
    CString sTemp = _T("");
    sTemp.Format(_T("OnLButtonDown - Flags: 0x%08X / AsyncKey: 0x%04X\n"), nFlags, nAsyncKey);

    ::OutputDebugString(sTemp);

    CDialog::OnLButtonDown(nFlags, point);
}

void CTestDlg::OnRButtonDown(UINT nFlags, CPoint point)
{
    // TODO: Add your message handler code here and/or call default

    SHORT nAsyncKey = GetAsyncKeyState(VK_LBUTTON);
    CString sTemp = _T("");
    sTemp.Format(_T("OnRButtonDown - Flags: 0x%08X / AsyncKey: 0x%04X\n"), nFlags, nAsyncKey);

    ::OutputDebugString(sTemp);

    CDialog::OnRButtonDown(nFlags, point);
}

GetAsyncKeyState의 Return Value는 SHORT으로 의미하는 내용은 다음과 같다.
(debugjung 님이 정리해주신 내용 퍼옴)

0x0000: 이전에 누른 적이 없고 호출시점에 안 눌린 상태
0x8000: 이전에 누른 적이 없고 호출시점에 눌린 상태
0x8001: 이전에 누른 적이 있고 호출시점에 눌린 상태
0x0001: 이전에 누른 적이 있고 호출시점에 안 눌린 상태

매크로를 이용한 응용 방법은 다음과 같다.

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code)       ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

간단하게 마우스 이벤트로 테스트 해보면 금방 이해가 된다. 끝.