Programming/Delphi

윈도우 특정키 막기

통통만두 2010. 7. 23. 10:57
반응형
type
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTURCUT;
   TKBDLLHOOKSTURCUT = record
   vkCode,
   scanCode,
   flag,
   time,
   dwExtraInfo: DWORD;
  end;

private
  hhookCurrent: HHOOK;

-------------------------------------------------------------------------------------------------------------------

function KeyBoardProc( icode: integer; wParam: integer; lParam: integer): integer; stdcall;
var
  p: PKBDLLHOOKSTRUCT;
  fEatKeystroke: Boolean;
begin
  fEatKeystroke := False;

  if ( icode = HC_ACTION ) then
  begin
     case ( wParam ) of
       WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP:
       begin
         p := PKBDLLHOOKSTRUCT( lParam );
         //                 Alt + Tab, Ctrl + Esc, Alt + F4, Alt + Esc 막기
         fEatKeystroke := ( p^.vkCode = VK_TAB ) and ( ( p^.flag and $00000020 ) <> 0 ) or
                          ( p^.vkCode = VK_ESCAPE ) and ( ( p^.flag and $00000020 ) <> 0 ) or
                          ( p^.vkCode = VK_F4 ) and ( ( p^.flag and $00000020 ) <> 0 ) or
                          ( p^.vkCode = VK_ESCAPE ) and ( ( GetKeyState( VK_CONTROL ) and $8000 ) <> 0 ) or
                          ( p^.vkCode = VK_LWIN) or
                          ( p^.vkCode = VK_RWIN) or
                          ( p^.vkCode = VK_CONTROL) or
                          ( p^.vkCode = VK_MENU);
       end;
     end;
  end;

  if fEatKeystroke then
     result  :=  1
  else
     result  :=  CallNextHookEx(0, iCode, wParam, lParam);
end;

procedure Tfrm_Main.FormCreate(Sender: TObject);
begin
   hhookCurrent := SetWindowsHookEx( 13{WH_KEYBOARD_LL}, @KeyBoardProc, HInstance, 0 );
end;

procedure Tfrm_Main.FormDestroy(Sender: TObject);
begin
   ExitWindows(EWX_force, 0);
end;


반응형