如何攔截Alt鍵與Print Screen鍵? |
尚未結案
|
ez3cshop
一般會員 發表:13 回覆:6 積分:3 註冊:2005-01-24 發送簡訊給我 |
請教大家: 我有參考站內關於Keyboard Hook的資料,有一篇轉貼文魁一本API著作,裡面題到如何使用Hook來攔截鍵盤的訊息, 基本上, 是可以運作的. 但是, Print Screen鍵或Alt+Print Screen鍵就攔截不到, 所以想請教大家, 這部份的功能要如何去實現呢? 因為現在所接觸的專案很特殊,就是要取消螢幕拷貝的問題,以免資料被盜取. 謝謝大家! 以下為站上該篇資料的轉貼,因為我已經找不到那一篇了(K.Top的資料太豐富了...), 就此聲明,也請原作者諒解. >>>------------------------------------------------------<<< 在台灣,
關於BCB的鍵盤hook也已經有實例了,
改一改就可以錯亂送出的鍵盤字元,
以下是經作者認可的實例/文魁
enjoy it!!! Hook_WH_KEYBOARD
LRESULT CALLBACK KeyboardProc( int code, WPARAM wParam, LPARAM lParam );
功能簡易說明:攔截系統所有的鍵盤訊息
實作範例 (1)目的:
掛上、卸下攔截系統所有的鍵盤訊息,並運用自訂訊息傳遞hook資料
DLL 程式碼 (mydll.cpp)
#01 //--------------------------------------------------------------------------- #02 //掛上、卸下Keyboard hook,並運用自訂訊息傳遞hook資料 #03 //--------------------------------------------------------------------------- #04 #include //原有BCB 標頭檔 #05 #include // 原有window 標頭檔 #06 #pragma hdrstop #07 #pragma argsused #08 //自訂的鍵盤訊息,與執行檔TKeyHookForm中自訂的鍵盤訊息相互對應 #09 const int WM_KEYHOOK = WM_USER 100; #10 //--------------------------------------------------------------------------- #11 //DLL的固定宣告 #12 HHOOK hHook;//掛上的hook handle #13 HWND hAppWnd;//執行此dll的特定exe程式handle__"TKeyHookForm" #14 HINSTANCE inst;//連接檔的模組handle,固定行程 #15 //--------------------------------------------------------------------------- #16 //符合以C語言編譯的DLL輸出函式 #17 extern "C" { #18 __declspec(dllexport) __stdcall void SetHook(void); #19 __declspec(dllexport) __stdcall void RemoveHook(void); #20 } #21 //--------------------------------------------------------------------------- #22 //重載的KeyboardProc函式,持續送出鍵盤全域訊息 #23 LRESULT CALLBACK KeyboardProc(int, WPARAM, LPARAM); #24 //--------------------------------------------------------------------------- #25 //DLL的建構式&解構式 #26 int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*) #27 { #28 inst = hinst; #29 //當此DLL被呼叫or結束 #30 switch (reason) #31 { #32 case DLL_PROCESS_ATTACH ://此DLL建構式 #33 //取得執行此dll的特定程式handle #34 hAppWnd = ::FindWindow("TKeyHookForm", 0); #35 break; #36 #37 case DLL_PROCESS_DETACH://此DLL解構式 #38 break; #39 #40 case DLL_THREAD_ATTACH: //此DLL多緒建構式 #41 break; #42 #43 case DLL_THREAD_DETACH: //此DLL多緒解構式 #44 break; #45 } #46 return 1; #47 } #48 //--------------------------------------------------------------------------- #49 //注射自訂hook到hook鏈中 #50 void __stdcall SetHook(void) #51 {//若Hook尚未掛上訊息鏈中,則將本滑鼠訊息dll注射至hook鏈中 #52 if(hHook == NULL) #53 { #54 hHook = ::SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, inst, 0); #55 //顯示是否掛上hook,測試用! #56 if(hHook == NULL) #57 MessageBox(NULL, "Sorry! 無法掛上Hook。", "Hook DLL", MB_OK); #58 else #59 ::TextOut(GetDC(0),10,10,"掛上Hook。",10); #60 } #61 } #62 //--------------------------------------------------------------------------- #63 //在hook鏈中,解除自訂的hook #64 void __stdcall RemoveHook(void) #65 {//若Hook已經掛上訊息鏈,則卸下此Hook訊息 #66 if(hHook != NULL) #67 { #68 if(::UnhookWindowsHookEx(hHook) != FALSE) #69 { #70 hHook = NULL; #71 ::TextOut(GetDC(0),50,10,"釋放Hook。",10); #72 } #73 else //測試用! #74 MessageBox(NULL, "Sorry! 無法釋放Hook。", "Hook DLL", MB_OK); #75 } #76 else //測試用! #77 ::TextOut(GetDC(0),80,10,"Hook isn't NULL",16); #78 } #79 //--------------------------------------------------------------------------- #80 //重載的KeyboardProc函式,持續送出鍵盤全域訊息 #81 LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) #82 { #83 //若未掛上Hook鏈或鍵盤無訊息輸出,就交予下一hook鏈 #84 if((nCode < 0) || nCode == HC_NOREMOVE) #85 return ::CallNextHookEx(hHook, nCode, wParam, lParam); #86 //若訊息重複,則跳至下一hook鏈 #87 if(lParam & 0x40000000) #88 return ::CallNextHookEx(hHook, nCode, wParam, lParam); #89 //若在DLL建構式中,取得執行此dll的特定程式handle,即送出自訂鍵盤訊息 #90 if(hAppWnd != NULL) #91 SendMessage(hAppWnd, WM_KEYHOOK, wParam, lParam); #92 #93 return ::CallNextHookEx(hHook, nCode, wParam, lParam); #94 } #95 //--------------------------------------------------------------------------- EXE 程式碼 (Unit1.h & Unit1.cpp): Unit1.h #01 //--------------------------------------------------------------------------- #02 #ifndef Unit1H #03 #define Unit1H #04 //--------------------------------------------------------------------------- #05 #include #06 #include #07 #include #08 #include #09 #include #10 #include #11 //--------------------------------------------------------------------------- #12 //自訂的鍵盤訊息,與keydll.dll中自訂的鍵盤訊息相互對應 #13 const int WM_KEYHOOK = WM_USER 100; #14 //--------------------------------------------------------------------------- #15 class TKeyHookForm : public TForm #16 { #17 __published: // IDE-managed Components #18 TToolBar *ToolBar1; #19 TButton *Button1; #20 TButton *Button2; #21 TListBox *ListBox1; #22 void __fastcall Button1Click(TObject *Sender); #23 void __fastcall Button2Click(TObject *Sender); #24 void __fastcall FormClose(TObject *Sender, TCloseAction &Action); #25 private: // User declarations #26 HINSTANCE inst; //指向鍵盤hook結構 #27 int inthook; //遞增變數--計算按鍵次數 #28 public: // User declarations #29 //捕捉來自keydll.dll的自訂鍵盤訊息 #30 void __fastcall KeyHook(TMessage &Msg); #31 BEGIN_MESSAGE_MAP #32 VCL_MESSAGE_HANDLER(WM_KEYHOOK, TMessage, KeyHook); #33 END_MESSAGE_MAP(TForm) #34 __fastcall TKeyHookForm(TComponent* Owner); #35 }; #36 //--------------------------------------------------------------------------- #37 extern PACKAGE TKeyHookForm *KeyHookForm; #38 //--------------------------------------------------------------------------- #39 #endif Unit1.cpp #01 //--------------------------------------------------------------------------- #02 //掛上、卸下Keyboard hook,並運用自訂訊息傳遞hook資料 #03 //--------------------------------------------------------------------------- #04 #include #05 #pragma hdrstop #06 #07 #include "Unit1.h" #08 //--------------------------------------------------------------------------- #09 #pragma package(smart_init) #10 #pragma resource "*.dfm" #11 TKeyHookForm *KeyHookForm; #12 //--------------------------------------------------------------------------- #13 __fastcall TKeyHookForm::TKeyHookForm(TComponent* Owner) #14 : TForm(Owner) #15 { #16 inthook = 0;//計算按鍵次數變數歸零 #17 //將KeyHookForm維持在視窗最上面 #18 FormStyle = fsStayOnTop; #19 //掛上&卸下Hook的按鈕狀態 #20 Button1->Enabled = true; #21 Button2->Enabled = false; #22 } #23 //--------------------------------------------------------------------------- #24 //掛上Hook鏈,並設置定時器從共享記憶體中取回鍵盤訊息 #25 void __fastcall TKeyHookForm::Button1Click(TObject *Sender) #26 { #27 //計算按鍵次數變數歸零 #28 inthook = 0; #29 //在keydll.dll中,SetHook函式是執行注射Hook行程至訊息鏈中 #30 //先宣告有一SetHook函式 #31 void (*SetHook)(); #32 //載入同一目錄下的指定連接檔--keydll.dll #33 inst = ::LoadLibrary("keydll.dll"); #34 //取得dll中SetHook函式位址 #35 (FARPROC &)SetHook = ::GetProcAddress(inst,"SetHook"); #36 //執行指向dll中的SetHook函式 #37 SetHook(); #38 //掛上&卸下Hook的按鈕狀態 #39 Button1->Enabled = false; #40 Button2->Enabled = true; #41 } #42 //--------------------------------------------------------------------------- #43 //卸下Hook鏈,解除自訂的hook #44 void __fastcall TKeyHookForm::Button2Click(TObject *Sender) #45 { #46 //在keydll.dll中,RemoveHook函式是執行從訊息鏈中卸下Hook行程 #47 //宣告RemoveHook函式 #48 void (*RemoveHook)(); #49 //載入同一目錄下的指定連接檔--keydll.dll #50 inst = ::LoadLibrary("keydll.dll"); #51 //取得dll中RemoveHook函式位址 #52 (FARPROC &)RemoveHook = ::GetProcAddress(inst,"RemoveHook"); #53 //執行指向dll中的RemoveHook函式 #54 RemoveHook(); #55 //釋放dll #56 ::FreeLibrary(inst); #57 //掛上&卸下Hook的按鈕狀態 #58 Button1->Enabled = true; #59 Button2->Enabled = false; #60 } #61 //--------------------------------------------------------------------------- #62 //預防使用者結束Hook時,未卸下Hook鏈 #63 void __fastcall TKeyHookForm::FormClose(TObject *Sender, #64 TCloseAction &Action) #65 { #66 //在keydll.dll中,RemoveHook函式是執行從訊息鏈中卸下Hook行程 #67 //先宣告RemoveHook函式 #68 void (*RemoveHook)(); #69 //載入dll #70 inst = ::LoadLibrary("keydll.dll"); #71 //取得dll中RemoveHook函式位址 #72 (FARPROC &)RemoveHook = ::GetProcAddress(inst,"RemoveHook"); #73 //執行指向dll中的RemoveHook函式 #74 RemoveHook(); #75 //釋放dll #76 ::FreeLibrary(inst); #77 } #78 //--------------------------------------------------------------------------- #79 //取得攔截自鍵盤中的自訂訊息 #80 void __fastcall TKeyHookForm::KeyHook(TMessage &Msg) #81 { #82 //取得自訂訊息中的按鍵名稱 #83 char keytext[80]; #84 ::GetKeyNameText(Msg.LParam, keytext, 80); #85 //取得自訂訊息中的按鍵狀態(檢測用..意義不大...) #86 AnsiString keystate; #87 keystate = ::GetKeyState((int)Msg.WParam); #88 keystate = (keystate == "1")?"按下":"浮起";//C 之三元運算 #89 #90 //由ListBox元件顯示攔截成果 #91 ListBox1->Items->Insert(0, #92 "第 " AnsiString( inthook) " 按鍵__" #93 AnsiString(keytext) " ;狀態>>" keystate #94 ); #95 } #96 //---------------------------------------------------------------------------發表人 - ez3cshop 於 2005/07/26 10:23:42 |
taishyang
站務副站長 發表:377 回覆:5490 積分:4563 註冊:2002-10-08 發送簡訊給我 |
|
limeca
中階會員 發表:2 回覆:74 積分:60 註冊:2005-05-11 發送簡訊給我 |
|
kenlee1109
初階會員 發表:20 回覆:40 積分:27 註冊:2006-08-17 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |