研究了很久,终于把屏蔽序列键给搞定了,老规矩了,做成控件以便今后的使用
unit RaKeyBlock;
interface
uses
SysUtils, Classes, windows, ShlObj, Registry, shellapi, Messages;
type
TRaKeyBlock = class(TComponent)
private
fBCAD: Boolean;
fBAT: Boolean;
fBCE: Boolean;
fEnabled: Boolean;
fBAE: Boolean;
fBCR: Boolean;
fBCK: Boolean;
fBP: Boolean;
fBS: Boolean;
fCKC: Cardinal;
fBWA: Boolean;
fBCAE: Boolean;
procedure SetBCAD(const Value: Boolean);
procedure SetBAT(const Value: Boolean);
procedure SetBCE(const Value: Boolean);
procedure SetEnabled(const Value: Boolean);
procedure SetBAE(const Value: Boolean);
procedure SetBCK(const Value: Boolean);
procedure SetBCR(const Value: Boolean);
procedure SetBP(const Value: Boolean);
procedure SetBS(const Value: Boolean);
procedure SetBWA(const Value: Boolean);
procedure SetBCAE(const Value: Boolean);
protected
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property BlockCtrlAltDelete: Boolean read fBCAD write SetBCAD;
property BlockAltTab: Boolean read fBAT write SetBAT;
property BlockCtrlEsc: Boolean read fBCE write SetBCE;
property BlockAltEsc: Boolean read fBAE write SetBAE;
property BlockCtrlEnter: Boolean read fBCR write SetBCR;
property BlockSleep: Boolean read fBS write SetBS;
property BlockPower: Boolean read fBP write SetBP;
property CustomKeyCode: Cardinal read fCKC write fCKC default 0;
property BlockCustomKey: Boolean read fBCK write SetBCK;
property BlockWinApps: Boolean read fBWA write SetBWA;
property BlockCtrlAltEnter: Boolean read fBCAE write SetBCAE;
property Enabled: Boolean read fEnabled write SetEnabled default false;
end;
type
KBDLLHOOKSTRUCT = record
vkCode: DWORD;
scanCode: DWORD;
flags: DWORD;
Time: DWORD;
dwExtraInfo: DWORD; end;
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
const
LLKHF_ALTDOWN = KF_ALTDOWN shr 8;
WH_KEYBOARD_LL = 13;
var
hhkNTKeyboard: HHOOK = 0;
KH:TRaKeyBlock;
aBCAD:boolean=False;
aBWA:boolean=False;
aBCE:boolean=False;
aBAT:boolean=False;
aBAE:boolean=False;
aBCR:boolean=False;
aBCAE:boolean=False;
aBP:boolean=False;
aaBS:boolean=False;
aBCK:boolean=False;
aCKC:Cardinal=0;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Rarnu Components', [TRaKeyBlock]);
end;
{ TRaKeyBlock }
constructor TRaKeyBlock.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
end;
procedure EnableCTRLALTDEL(YesNo: Boolean);
const
sRegPolicies = '/Software/Microsoft/Windows/CurrentVersion/Policies';
begin
with TRegistry.Create do
try
RootKey := HKEY_CURRENT_USER;
if OpenKey(sRegPolicies + '/System/', True) then
begin
case YesNo of
false:
begin
WriteInteger('DisableTaskMgr', 1); //任务管理
WriteInteger('DisableLockWorkstation', 1); //用户锁定计算机
WriteInteger('DisableChangePassword', 1); //用户更改口令
end;
True:
begin
WriteInteger('DisableTaskMgr', 0);
WriteInteger('DisableLockWorkstation', 0);
WriteInteger('DisableChangePassword', 0);
end;
end;
end;
CloseKey;
if OpenKey(sRegPolicies + '/Explorer/', True) then
begin
case YesNo of
false:
begin
WriteInteger('NoChangeStartMenu', 1); //开始菜单
WriteInteger('NoClose', 1); // 关闭系统菜单
WriteInteger('NoLogOff', 1); //注销菜单
WriteInteger('NoRun', 1); //运行菜单
WriteInteger('NoSetFolders', 1); //设置菜单
end;
True:
begin
WriteInteger('NoChangeStartMenu', 0);
WriteInteger('NoClose', 0);
WriteInteger('NoLogOff', 0);
WriteInteger('NoRun', 0);
end;
end;
end;
CloseKey;
finally
Free;
end;
end;
function LowLevelKeyboardFunc(nCode: INTEGER; w_Param: WPARAM;
l_Param: LPARAM): LRESULT; stdcall;
var
boolKey: Boolean;
p: PKBDLLHOOKSTRUCT;
const
VK_SLEEP = $5F;
VK_POWER = $5E;
begin
boolKey := false;
if nCode = HC_ACTION then
begin
case w_Param of
WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP:
begin
p := PKBDLLHOOKSTRUCT(l_Param);
//---------!-~------------------------------------------------
{ if ((GetAsyncKeyState(VK_RBUTTON) and $8000) <> 0) then boolKey := true;
if (CHAR(p.vkCode) >= '!') and (CHAR(p.vkCode) <= '~') and
((GetKeyState(VK_CONTROL) and $8000) <> 0) then boolKey := true;
if (p.vkCode = VK_SPACE) and
((GetKeyState(VK_CONTROL) and $8000) <> 0) then boolKey := true; }
//---------F1-F12 ----------------------------------------------
{ if (p.vkCode = VK_F1) or (p.vkCode = VK_F2) or (p.vkCode = VK_F3) or
(p.vkCode = VK_F4) or (p.vkCode = VK_F5) or (p.vkCode = VK_F6) or
(p.vkCode = VK_F7) or (p.vkCode = VK_F8) or (p.vkCode = VK_F9) or
(p.vkCode = VK_F10) or (p.vkCode = VK_F11) or (p.vkCode = VK_F12) then boolKey := true;
if ((p.vkCode = VK_F1) or (p.vkCode = VK_F2) or (p.vkCode = VK_F3) or
(p.vkCode = VK_F4) or (p.vkCode = VK_F5) or (p.vkCode = VK_F6) or
(p.vkCode = VK_F7) or (p.vkCode = VK_F8) or (p.vkCode = VK_F9) or
(p.vkCode = VK_F10) or (p.vkCode = VK_F11) or (p.vkCode = VK_F12)) and
(((GetKeyState(VK_MENU) and $8000) <> 0) or ((GetKeyState(VK_CONTROL) and $8000) <> 0) or ((GetKeyState(VK_SHIFT)and$8000) <> 0)) then boolKey := true; }
//-------系统热键---------------------------------------------
//WIN(Left or Right)+APPS
if aBWA then
begin
if (p.vkCode = VK_LWIN) or (p.vkCode = VK_RWIN) or (p.vkCode = VK_APPS) then boolKey := True;
end;
//CTRL+ESC
if aBCE then
begin
if (p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0) then boolKey := True;
end;
//ALT+TAB
if aBAT then
begin
if (p.vkCode = VK_TAB) and ((GetAsyncKeyState(VK_MENU) and $8000) <> 0) then boolKey := True;
end;
//ALT+ESC
if aBAE then
begin
if (p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0) then boolKey := True;
end;
//CTRL+ENTER
if aBCR then
begin
if (p.vkCode = VK_RETURN) and ((GetKeyState(VK_CONTROL) and $8000) <> 0) then boolKey := True;
end;
//CTRL+ALT+ENTR
if aBCAE then
begin
if (p.vkCode = VK_RETURN) and ((p.flags and LLKHF_ALTDOWN) <> 0) and ((GetKeyState(VK_CONTROL) and $8000) <> 0) then boolKey := True;
end;
//POWER
if aBP then
begin
if (p.vkCode = VK_POWER) then boolKey := True;
end;
//SLEEP
if aaBS then
begin
if (p.vkCode = VK_SLEEP) then boolKey := True;
end;
//-------------------------------------------------------------------------------------------------------------------------------------
//Custom
if aBCK then
begin
if (p.vkCode = aCKC) then boolKey := True;
end;
//-------------------------------------------------------------------------------------------------------------------------------------
//如果有其他要屏闭的键,添加在此处
end;
end;
end;
//捕获这些组合键,按键消息由自己处理,必须返回 1
if boolKey then begin Result := 1; Exit end;
//其他的按键,交由别的线程处理(过滤)
Result := CallNextHookEx(0, nCode, w_Param, l_Param);
end;
destructor TRaKeyBlock.Destroy;
begin
Enabled:=false;
inherited Destroy;
end;
procedure TRaKeyBlock.SetBAE(const Value: Boolean);
begin
fBAE := Value;
aBAE:=fBAE;
Enabled := false;
end;
procedure TRaKeyBlock.SetBAT(const Value: Boolean);
begin
fBAT := Value;
aBAT:=fBAT;
Enabled := false;
end;
procedure TRaKeyBlock.SetBCAD(const Value: Boolean);
begin
fBCAD := Value;
aBCAD:=fBCAD;
Enabled := false;
end;
procedure TRaKeyBlock.SetBCAE(const Value: Boolean);
begin
fBCAE := Value;
aBCAE:=fBCAE;
Enabled := false;
end;
procedure TRaKeyBlock.SetBCE(const Value: Boolean);
begin
fBCE := Value;
aBCE:=fBCE;
Enabled := false;
end;
procedure TRaKeyBlock.SetBCK(const Value: Boolean);
begin
fBCK := Value;
aBCK:=fBCK;
Enabled := false;
end;
procedure TRaKeyBlock.SetBCR(const Value: Boolean);
begin
fBCR := Value;
aBCR:=fBCR;
Enabled := false;
end;
procedure TRaKeyBlock.SetBP(const Value: Boolean);
begin
fBP := Value;
aBP:=fBP;
Enabled := false;
end;
procedure TRaKeyBlock.SetBS(const Value: Boolean);
begin
fBS := Value;
aaBS:=fBS;
Enabled := false;
end;
procedure TRaKeyBlock.SetBWA(const Value: Boolean);
begin
fBWA := Value;
aBWA:=fBWA;
Enabled := false;
end;
procedure TRaKeyBlock.SetEnabled(const Value: Boolean);
begin
fEnabled := Value;
//设置可用
case fEnabled of
True:
begin
if hhkNTKeyboard <> 0 then Exit;
hhkNTKeyboard := SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardFunc, HInstance, 0);
if fBCAD then
begin
EnableCTRLALTDEL(false);
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
end;
end;
false:
begin
if hhkNTKeyboard = 0 then Exit;
UnhookWindowsHookEx(hhkNTKeyboard); // 卸载钩子
EnableCTRLALTDEL(true);
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil);
hhkNTKeyboard := 0;
end;
end;
end;
end.