With delphi, how can I disable the ESC key for all applications?

I need to disable esc for all programs using Delphi.

Probably the form has to stay always active and I disable the key in a way similar to the example below.

My code so far:

procedure TForm2.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin

if Key = 27 then
ShowMessage('A tecla Backspace foi pressionada');

end;
 5
Author: David, 2014-05-10

4 answers

About keyboard event recognition

If pressing ESC your message does not appear as defined in your method FormKeyDown, then check if the property KeyPreview form has been set to True.

How to override the key

Give preference to the event OnKeyPress and to override the key do:

if key = #27 then
  key := #0;
 7
Author: Comunidade, 2020-06-11 14:45:34

One way to achieve this by programming in Delphi is to use hooks(Hooks in English ).

Consider the following example (Tested On Delphi XE4, visual application):

{ Anula o funcionamento da tecla Esc }
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, 
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

  type
  tagKBDLLHOOKSTRUCT = record
  vkCode: DWord;
  scanCode: DWord;
  flags: DWord;
  time: DWord;
  dwExtraInfo: PDWord;
  end;
  TKBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;

type
  TForm1 = class(TForm)
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  khk: HHOOK;

implementation

{$R *.dfm}

function KeyboardHookProc(Code: Integer; wParam : WPARAM; lParam : LPARAM): NativeInt; stdcall;
var
p:PKBDLLHOOKSTRUCT;
begin
p := PKBDLLHOOKSTRUCT(lParam);
if (Code = HC_ACTION) and (wParam = $0100) then
if (p.vkCode = VK_ESCAPE) then
Result := 1 else Result := CallNextHookEx(0, Code, wParam, lParam);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
khk := SetWindowsHookEx(13, KeyboardHookProc, hInstance, 0);
if khk = 0  then ShowMessage('Error on start hook')
else ShowMessage('Hook started');
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
UnhookWindowsHookEx(khk);
end;

end.

This will install a hook (hook ) in the system to intercept the key Esc once this is done, we can change its behavior, such as Override.


Now another example ( Console application) that overrides the operation of the key combination Alt + Tab .

{ Anula o funcionamento da combinação de teclas Alt + Tab }
program Project2;

{$APPTYPE CONSOLE}

uses
  Windows;

type
  tagKBDLLHOOKSTRUCT = record
  vkCode: DWord;
  scanCode: DWord;
  flags: DWord;
  time: DWord;
  dwExtraInfo: PDWord;
  end;
  TKBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^TKBDLLHOOKSTRUCT;

var
  khk: HHOOK;
  MSG: tmsg;

function KeyboardHookProc(Code: Integer; wParam : WPARAM; lParam : LPARAM): NativeInt; stdcall;
var
  p:PKBDLLHOOKSTRUCT;
begin
p := PKBDLLHOOKSTRUCT(lParam);
if (Code = HC_ACTION) and (wParam = $0100) then
if (p.vkCode = VK_LMENU) or (p.flags = VK_TAB)  then
Result := 1 else Result := CallNextHookEx(0, Code, wParam, lParam);
end;

begin
khk := SetWindowsHookEx(13, KeyboardHookProc, hInstance, 0);
if khk = 0  then 
  writeln('Error on start hook') 
else 
  writeln('Hook started');

while GetMessage(MSG, 0, 0, 0) do begin
  TranslateMessage(MSG);
  DispatchMessage(MSG);
end;

UnhookWindowsHookEx(khk);
end.

to override the right side Alt use VK_RMENU*)

Aqui(Virtual-Key Codes ) you find the key code.

 7
Author: stderr, 2019-04-07 18:31:45

Alternative answer, using the registry: just create a text file " anythings.reg " with this data:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,5b,e0,38,00,00,00,00,00

Make sure the name is qualquercoisa.reg and not qualquercoisa.reg.txt. Saving the file, double-click it, and accept the import.

Attention: only do this if you are sure that this will achieve the desired result.

 5
Author: Bacco, 2014-05-15 19:55:13

I found a solution!!!

Disable Esc e :

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,05,00,00,00,00,00,5B,E0,00,00,5C,E0,00,00,5D,E0,00,00,01,00,00,00,00,00

And to disable the combination CTRL + Alt + of and Alt + Tab :

  1. Type gpedit.msc in the Windows Run box and type Enter.
  2. Select User Configuration - Administrative - Templates - System - Ctrl-Alt-Del Options.
  3. double-click remove Change Password, remove computer lock, remove Task Manager and remove logoff and select enable and then in OK.
  4. Select Computer Configuration - Administrative Templates - System - Logon.
  5. in the right pane, double-click Hide for quick user switching.
  6. select Enabled and click OK .
 3
Author: Alan PS, 2014-05-16 18:27:56