如何在Windows 7上禁用CTRL ALT键?

时间:2015-03-17 23:08:18

标签: delphi

我知道唯一的方法是使用regedit重新映射密钥。 有人用delphi做过吗? (禁用它并再次启用它)

http://www.northcode.com/blog.php/2007/07/25/Securing-Windows-For-Use-As-A-Kiosk

1 个答案:

答案 0 :(得分:2)

文章中的信息将转换为Delphi,如下所示:

uses
  Registry;

const
  DisableScancodes: packed array[0..11] of DWORD = (
    $00000000, // version = 0
    $00000000, // flags = 0
    $00000009, // # of mappings = 9
    $E05B0000, // disable Windows key
    $E05C0000, // disable Windows key
    $E05D0000, // disable Windows menu key
    $00440000, // disable F10 key
    $001D0000, // disable Left Ctrl key
    $00380000, // disable Left Alt key
    $E01D0000, // disable Right Ctrl key
    $E0380000, // disable Right Alt key
    $00000000  // end of list
  );

var
  Reg: TRegistry;
begin
  Reg := TRegistry.Create(KEY_WRITE);
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\SYSTEM\CurrentControlSet\Control\Keyboard Layout', True) then
    begin
      try
        // to enable the mapping
        Reg.WriteBinaryData('Scancode Map', DisableScancodes, SizeOf(DisableScancodes));

        // to disable the mapping
        Reg.DeleteValue('Scancode Map');
      finally
        Reg.CloseKey;
      end;
    end;
  finally
    Reg.Free;
  end;
end;

如果您需要更加动态地启用/禁用哪些扫描码,则必须使用TRegistry.ReadBinaryData()来读取当前Scancode Map值(如果存在),根据需要进行修改,以及然后使用TRegistry.WriteBinaryData()保存更改。尝试这样的事情:

unit ScanCodeMap;

interface

type
  TMappedScancode = record
    Scancode: WORD;
    MappedTo: WORD;
  end;

procedure AddScancodeMappings(const Values: array of TMappedScancode);
procedure AddScancodeMapping(const Value: TMappedScancode);
procedure AddScancodeMapping(Scancode, MappedTo: WORD);

procedure RemoveScancodeMappings(const Scancodes: array of WORD);
procedure RemoveScancodeMapping(Scancode: WORD);

procedure DisableScancodes(Scancodes: array of WORD);
procedure DisableScancode(Scancode: WORD);

implementation

uses
  Windows, Registry;

type
  PScancodeMapHdr = ^TScancodeMapHdr;
  TScancodeMapHdr = packed record
    Version: DWORD;
    Flags: DWORD;
    NumMappings: DWORD;
  end;

  TScancodeMap = record
    Version: DWORD;
    Flags: DWORD;
    Mappings: array of TMappedScancode;
  end;

procedure AddScancodesToMap(var Map: TScancodeMap; const Values: array of TMappedScancode);
var
  I, J, Idx: Integer;
begin
  for I := 0 to High(Values) do
  begin
    Idx := -1;
    for J := 0 to High(Map.Mappings) do
    begin
      if Map.Mappings[J].Scancode = Values[I].Scancode then
      begin
        Idx := J;
        Break;
      end;
    end;
    if Idx = -1 then
    begin
      SetLength(Map.Mappings, Length(Map.Mappings)+1);
      Idx := High(Map.Mappings);
    end;
    Map.Mappings[Idx].MappedTo := Values[I].MappedTo;
  end;
end;

procedure RemoveScancodesFromMap(var Map: TScancodeMap; const Scancodes: array of WORD);
var
  I, J: Integer;
begin
  for I := 0 to High(Scancodes) do
  begin
    for J := 0 to High(Map.Mappings) do
    begin
      if Map.Mappings[J].Scancode = Scancodes[I] then
      begin
        if J < High(Map.Mappings) then
          Move(Map.Mappings[J+1], Map.Mappings[J], (High(Mappings)-J) * SizeOf(TMappedScancode));
        SetLength(Map.Mappings, Length(Map.Mappings)-1);
        Break;
      end;
    end;
  end;
end;

procedure WriteScanCodeMap(const Map: TScancodeMap);
var
  Reg: TRegistry;
  Data: array of Byte;
  Tmp: PByte;
  Hdr: PScancodeMapHdr;
  I: Integer;
begin
  if Length(Map.Mappings) > 0 then
  begin
    SetLength(Data, sizeof(TScancodeMapHdr) + (Length(Map.Mappings) + 1) * SizeOf(DWORD));
    Tmp := PByte(Data);

    Hdr := PScancodeMapHdr(Tmp);
    Hdr.Version := Map.Version;
    Hdr.Flags := Map.Flags;
    Hdr.NumMappings := Length(Map.Mappings) + 1;
    Inc(Tmp, SizeOf(TScancodeMapHdr));

    for I := 0 to High(Map.Mappings) do
    begin
      PDWORD(Tmp)^ := (DWORD(Map.Mappings[0].Scancode) shr 16) or DWORD(Map.Mappings[0].MappedTo);
      Inc(Tmp, SizeOf(DWORD));
    end;

    PDWORD(Tmp)^ := 0;
  end;

  Reg := TRegistry.Create(KEY_WRITE);
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey('\SYSTEM\CurrentControlSet\Control\Keyboard Layout', True) then
    begin
      try
        if Length(Data) > 0 then
          Reg.WriteBinaryData('Scancode Map', Data[0], Length(Data))
        else
          Reg.DeleteValue('Scancode Map');
      finally
        Reg.CloseKey;
      end;
    end;
  finally
    Reg.Free;
  end;
end;

procedure ReadScanCodeMap(var Map: TScancodeMap);
var
  Reg: TRegistry;
  Size: Integer;
  Data: array of Byte;
  Tmp: PByte;
  Hdr: PScancodeMapHdr;
  I: Integer;
begin
  Map.Version := 0;
  Map.Flags := 0;
  SetLength(Map.Mappings, 0);

  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKeyReadOnly('\SYSTEM\CurrentControlSet\Control\Keyboard Layout') then
    begin
      try
        Size := Reg.GetDataSize('Scancode Map');
        if Size > SizeOf(TScancodeMapHdr) then
        begin
          SetLength(Data, Size);
          Reg.ReadBinaryData('Scancode Map', Data[0], Size);
          Tmp := PByte(Data);

          Hdr := PScancodeMapHdr(Tmp);
          Map.Version := Hdr.Version;
          Map.Flags := Hdr.Flags;
          Inc(Tmp, SizeOf(TScancodeMapHdr));

          if Hdr.NumMappings > 1 then
          begin
            SetLength(Map.Mappings, Hdr.NumMappings-1);
            for I := 0 to High(Map.Mappings) do
            begin
              Map.Mappings[I].Scancode := HIWORD(PDWORD(Tmp)^);
              Map.Mappings[I].MappedTo := LOWORD(PDWORD(Tmp)^);
            end;
          end;
        end;
      finally
        Reg.CloseKey;
      end;
    end;
  finally
    Reg.Free;
  end;
end;

procedure AddScancodeMappings(const Values: array of TMappedScancode);
var
  Map: TScancodeMap;
begin
  ReadScanCodeMap(Map); 
  AddScancodesToMap(Map, Values);
  WriteScanCodeMap(Map);
end;

procedure AddScancodeMapping(const Value: TMappedScancode);
begin
  AddScancodeMappings([Value]);
end;

procedure AddScancodeMapping(Scancode, MappedTo: WORD);
var
  Value: array[0..0] of TMappedScancode;
begin
  Value[0].Scancode := Scancode;
  Value[0].MappedTo := MappedTo;
  AddScancodeMappings([Value]);
end;

procedure RemoveScancodeMappings(const Scancodes: array of WORD);
var
  Map: TScancodeMap;
begin
  ReadScanCodeMap(Map); 
  RemoveScancodesFromMap(Map, Scancodes);
  WriteScanCodeMap(Map);
end;

procedure RemoveScancodeMapping(Scancode: WORD);
begin
  RemoveScancodeMappings([Scancode]);
end;

procedure DisableScancodes(Scancodes: array of WORD);
var
  Values: array of TMappedScancode;
  I: Integer;
begin
  SetLength(Values, Length(Scancodes));
  for I := 0 to High(Mappings) do
  begin
    Values[I].Scancode := Scancodes[I];
    Values[I].MappedTo := $0000;
  end;
  AddScancodeMappings(Values);
end;

procedure DisableScancode(Scancode: WORD);
begin
  AddScancodeMapping(Scancode, $0000);
end;

end.

然后你可以这样做:

uses
  ScanCodeMap;

const
  Scancodes: packed array[0..7] of WORD = (
    $E05B, // Windows key
    $E05C, // Windows key
    $E05D, // Windows menu key
    $0044, // F10 key
    $001D, // Left Ctrl key
    $0038, // Left Alt key
    $E01D, // Right Ctrl key
    $E038  // Right Alt key
  );

procedure DisableCtrlAltDel;
begin
  DisableScancodes(Scancodes);
end;

procedure EnableCtrlAltDel;
begin
  RemoveScancodeMappings(Scancodes);
end;