Windows常用按鍵組合屏蔽

來源:互聯網
上載者:User
Ctrl-Alt-DEl 在2k 下屏蔽需要HookAPI,但其他都可以屏蔽,比如Power鍵也可以;代碼如下:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure WMPowerBroadcast(var message: TMessage); message
      WM_POWERBROADCAST;
    procedure FormDestroy(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;
  tagKBDLLHOOKSTRUCT = packed record
    vkCode: DWORD;                                          //虛擬索引值
    scanCode: DWORD;                                        //掃描碼值
    {一些擴充標誌,這個值比較麻煩,MSDN上說得也不太明白,但是
    根據這個程式,這個標誌值的第六位元(二進位)為1時ALT鍵按下為0相反。}
    flags: DWORD;
    time: DWORD;                                            //訊息時間戳記
    dwExtraInfo: DWORD; //和訊息相關的擴充資訊
  end;
  KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;

  //這個是低級鍵盤鉤子的索引值,Delphi中沒有,必須自己定義
const
  WH_KEYBOARD_LL              = 13;
  //定義一個常量好和上面哪個結構中的flags比較而得出ALT鍵是否按下
const
  LLKHF_ALTDOWN               = $20;

var
  Form1                       : TForm1;
  hhkLowLevelKybd             : HHOOK;
implementation
{
功能:低級鍵盤鉤子的回呼函數,在裡面過濾訊息
參數:nCode   是Hook的標誌
     WParam  表示訊息的類型
     LParam  是一個指向我們在上面定義的哪個結構KBDLLHOOKSTRUCT的指標
傳回值:如果不是0的話windows就把這個訊息丟掉,程式就不會再收到這個訊息了。
}

function LowLevelKeyboardProc(nCode: Integer;
  WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
  fEatKeystroke               : BOOL;
  p                           : PKBDLLHOOKSTRUCT;
begin
  Result := 0;
  fEatKeystroke := FALSE;
  p := PKBDLLHOOKSTRUCT(lParam);
  //nCode值為HC_ACTION時表示WParam和LParam參數包涵了按鍵訊息
  if (nCode = HC_ACTION) then
  begin
    //攔截按鍵訊息並測試是否是Ctrl+Esc、Alt+Tab、和Alt+Esc功能鍵。
    case wParam of
      WM_KEYDOWN,
        WM_SYSKEYDOWN,
        WM_KEYUP,
        WM_SYSKEYUP:
        begin

          Form1.Memo1.Lines.Add(IntToStr(Integer(p.vkCode)));
          {fEatKeystroke :=
            ((p.vkCode = VK_TAB) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or   // tab + alt
            ((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or   // esc + alt
            ((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or  //esc + ctrl
            ((p.vkCode = VK_DELETE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or // ctrl+alt+del
            (p.vkCode = VK_LWIN) or (p.vkCode = VK_RWIN) or (p.vkCode =VK_APPS) or  // Win + contentMenu
            ((p.vkCode = VK_F4) and ((p.flags and LLKHF_ALTDOWN) <>0));   // alt + f4
          }
          fEatKeystroke := not
            (p.vkCode = VK_F4);
        end;
    end;
  end;

  if fEatKeystroke = True then
    Result := 1;
  if nCode <> 0 then
    Result := CallNextHookEx(0, nCode, wParam, lParam);

end;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  //設定低級鍵盤鉤子
  if hhkLowLevelKybd = 0 then
  begin
    hhkLowLevelKybd := SetWindowsHookExW(WH_KEYBOARD_LL,
      LowLevelKeyboardProc, Hinstance, 0);
    if hhkLowLevelKybd <> 0 then
      MessageBox(Handle, '低級鍵盤鉤子設定成功!', '提示', MB_OK)
    else
      MessageBox(Handle, '低級鍵盤鉤子設定失敗!', '提示', MB_OK);
  end
  else
    MessageBox(Handle, '低級鍵盤鉤子已設定!', '提示', MB_OK);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  //卸載低級鍵盤鉤子
  if hhkLowLevelKybd <> 0 then
    if UnhookWindowsHookEx(hhkLowLevelKybd) <> False then
    begin
      MessageBox(Handle, '低級鍵盤鉤子卸載成功!', '提示', MB_OK);
      hhkLowLevelKybd := 0;
    end
    else
      MessageBox(Handle, '低級鍵盤鉤子卸載失敗!', '提示', MB_OK)
  else
    MessageBox(Handle, '沒有發現低級鍵盤鉤子!', '提示', MB_OK);
end;

procedure TForm1.WMPowerBroadcast(var message: TMessage);
begin
  //  if Application.MessageBox('是否關閉系統?','警告',MB_OKCANCEL + MB_DEFBUTTON2)<>IDOK then
  //  begin
  message.Result := BROADCAST_QUERY_DENY;                   //阻止系統關閉熱鍵
  //  end else Close();
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  //在Form關閉的時候檢測,如果沒有卸載鉤子就卸載之
  if hhkLowLevelKybd <> 0 then
    UnhookWindowsHookEx(hhkLowLevelKybd);
end;

end.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.