In response to another pergunta
.
One way to achieve this by programming in Delphi is to use Hooks (#
Consider the following example ( tested in 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 ) on the system to intercept the Esc key, so we can change its behavior, such as abort.
Now another example ( Console Application ) that aborts 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.
Hooks
(< in> Virtual-Key Codes ) you will find the key code.