본문 바로가기

공부/컴퓨터

[Win32/MFC] 키보드 후킹....

반응형
========================================================================
void CInstallhookDlg::OnOk()
{
        static HINSTANCE hinstDLL;
        typedef BOOL (CALLBACK *inshook)(HWND _hTargetWnd);
        inshook instkbhook;
        hinstDLL = LoadLibrary((LPCTSTR) "hodll.dll");
        instkbhook = (inshook)GetProcAddress(hinstDLL, "installhook");
        instkbhook(m_hWnd);
        //ShowWindow(SW_MINIMIZE);
}
========================================================================





HWND hTargetWnd;

UINT CF_HTML = RegisterClipboardFormat("HTML Format");
UINT CF_USER_RTF = RegisterClipboardFormat(CF_RTF);
UINT CF_USER_RETEXTOBJ = RegisterClipboardFormat(CF_RETEXTOBJ);


LRESULT __declspec(dllexport)__stdcall  CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
        int ScanCode    = (lParam >> 16) & 0x000000ff;                // 91번. 92번 일때가 WinKey 임.
        BOOL bPrevState  = (lParam >> 30) & 0x00000001;   // 0 이면 이전 키를 놓은 상태.
        BOOL bKeyUp      = (lParam >> 31) & 0x00000001;   // 0 이면 현재 누르기 시작한 상태.

        // 여기서는 키가 눌러졌다가 놓을때도 호출이 되게 된다.
        // 즉 두번 호출이 되게 된다.
        // 한번 걸러주던지 아니면, 다른곳에서 걸러 주어야 한다.

        SHORT nPressLWin = GetAsyncKeyState(VK_LWIN);
        // VK_LWIN 이 눌러진 상태이고
        if ( nPressLWin < 0 )
        {
                // 키를 누른 제일 처음.
                if (ScanCode == 47)
                {
                        // V 가 입력이 되면 ( 대 소문자 구분하지 않는다. )
                        if ( bKeyUp )
                        {
                                //TODO
                                if ( IsClipboardFormatAvailable(CF_USER_RTF) ) {
                                        AfxMessageBox("CF_USER_RTF");

                                } else if ( IsClipboardFormatAvailable(CF_USER_RETEXTOBJ) ) {
                                        AfxMessageBox("CF_USER_RETEXTOBJ");

                                } else if ( IsClipboardFormatAvailable(CF_HTML) ) {
                                        AfxMessageBox("CF_HTML");

                                } else if ( IsClipboardFormatAvailable(CF_TEXT) ) {
                                        AfxMessageBox("CF_TEXT");

                                } else if ( IsClipboardFormatAvailable(CF_BITMAP) ) {
                                        AfxMessageBox("CF_BITMAP");

                                } else if ( IsClipboardFormatAvailable(CF_SYLK) ) {
                                        AfxMessageBox("CF_SYLK");

                                }
                                
                                // 입력된 키 무시
                                wParam = 0;
                                lParam = 0;
                                nCode = 0;
                                //TODOEND
                                //::SendMessage(hTargetWnd, WM_WINV, wParam, lParam);
                        }
                }
                return 1;
        }

        LRESULT RetVal = CallNextHookEx( hkb, nCode, wParam, lParam );        
        return  RetVal;
}

BOOL __declspec(dllexport)__stdcall installhook(HWND _hTargetWnd)
{
        hTargetWnd = _hTargetWnd;
        hkb=SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,hins,0);
        return TRUE;
}

BOOL __declspec(dllexport)  UnHook()
    {
     BOOL unhooked = UnhookWindowsHookEx(hkb);
     return unhooked;
}
============================================================================
반응형