With Delphi, how can I disable the ESC key for all apps?

Asked

Viewed 2,291 times

5

I need to disable the esc for all programs using Delphi.

Probably the form has to be 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;
  • the documentation is in English, you know how I set up the AHK to block the ESC ?

  • It’s been a long time since I’ve done this, I don’t remember it. But do a search, there must be some script already ready for this.

  • I couldn’t find anything to do this with the AHK, if anyone can help me....

  • my dear, your question is very vague!

  • 5

    To help those who try to answer: OP told me in the comments that wants to prevent the ESC to work in all applications, not only in the form. I commented that for this it will be better to use an AHK or something like, pq with an application in Delphi will not solve. In fact the question does not reflect well the need of the OP.

  • disable the entire system, Windows 7, 8 or Xp, if you can post q code quoted...

  • 1

    I couldn’t just use regedit?

  • no, it needs to be a program just to change the record, I’m researching a lot but it’s hard...

  • But regedit does this Ué. Basically it serves to query and change the record.

  • 1

    It would just run regedit, go to the key "SYSTEM Currentcontrolset Keyboard Layout" and by the desired value. If you don’t have "Scancode Map" in the list just add it, probably as a new binary value.

  • and in Vb as it does ?

  • 4

    @DBX8 didn’t even have to open, it has several of these by the site :D http://answall.com/questions/15994/

  • @DBX8 though I don’t know what’s best, if it’s good to expect to open and close some as duplicate. There is the question, which is duplicate of which, has answer in both. Besides, if I’m not mistaken, there are more of these :)

  • @DBX8 waiting for the answer...

  • Using AHK, folder insert this line into the script: "ESC::Return"

Show 10 more comments

4 answers

7

About the keyboard event recognition

If by pressing ESC your message does not appear as defined in your method FormKeyDown, then check the property KeyPreview of the form was defined as True.

How to undo the key

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

if key = #27 then
  key := #0;

7

A 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 done, we may alter your behavior, such as overriding.


Now another example(Console App) which cancels 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 annul the Alt right-hand side use VK_RMENU*)

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

  • I’m new to Delphi and wanted to know how I use this code, the changes of the keys I know how to do, I just don’t know how to add the code in the vcl form

  • thank you very much!!!

  • I’m new to Delphi and I’m not being able to make it work either on the console or the visual, but I’m just copying and pasting and changing the form name, I need to do something more @DBX8 ? I am using the Embarcadero XE5, it does not give errors, only q does not block keys...

  • 1

    @user3230262 I don’t know how you are developing, so I will provide an example, download the code here.

  • Man, thank you so much!!!

  • It is possible to lock CTRL+ALT+DEL ? tried and failed...

  • Okay, thank you!!!

Show 2 more comments

5

Alternative answer, using the record: just create a text file "qualquercoisa.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 this will achieve the desired result.

  • how to disable Ctrl, alt, del, Windows key, Esc and tab by registry ?

3

I found a solution!!!

Disable Esc and :

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 + Del and Alt + Tab:

  1. Typo gpedit.msc in the Windows Run box and press Enter.
  2. Select User Configuration - Administrative - Templates - System - Ctrl-Alt-Del Options.
  3. Double-click on Remove Change Password, Remove Block Computer, Remove Task Manager and Remove Logoff and select Activate and then in OK.
  4. Select Computer Configuration - Administrative Templates - System - Logon.
  5. In the right pane, double-click on Hide for quick user change.
  6. Select Enabled and click on OK.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.