disable alt F4

How do you disable alt f4 ??
Tried this :
if (Key = VK_F4) and (ssAlt in Shift) then Key := 0;
but it does not work...
srbenavrbeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mikelittlewoodCommented:
Key := #0
0
mikelittlewoodCommented:
nevermind ...

  if (Key = VK_F4) and (ssAlt in Shift) then
  begin
    ShowMessage('ALT F4');
    Key := 0;
  end;

worked fine for me.

If you are trying to do it for the whole form, you might have to place it in the OnKeyDown of the form and inherit it to the other components maybe.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mikelittlewoodCommented:
Set your KeyPreview property of your main form to true and put that code on the main form keydown event.
This means the main forms key event will trigger before any components.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

robert_marquardtCommented:
I think you are barking the wrong tree. You should not block all means of closing your app. Write an OnCloseQuery handler.
0
mikelittlewoodCommented:
But he isnt blocking all means of closing his app, just if the user presses ALT + F4.
He must have the usual exit application options.
0
Qosai_DBACommented:
Hi srbenavrbe,

you can use OnCloseQuery event to prevent form close

this code may help you:

procedure TfrmPayVoucher.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  CanClose := not(frmPayVoucher.BtnSave.Enabled);
  if(not CanClose)then
    Case (MessageDlg('Do you want to save changes?',
            mtInformation,[mbYes,mbNo,mbCancel],0))Of
      mrYes :  frmPayVoucher.BtnSaveClick(Self);
      mrNo  :  begin
                 frmPayVoucher.BtnCancelClick(Self);
                 frmPayVoucher.FormShow(Self);
                 CanClose := True;
               end;
      mrCancel:
    end;
end;

 if the CanClose Parameter equal False the form will not close
else if it's True the form will close

Regards,
Khalid.
0
prevarantCommented:
Hi srbenavrbe

//The code should look like this:

public
  procedure AppMessage(var Msg: TMSG; var HAndled: Boolean);
end;

{...}

implementation

{...}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := AppMessage;
end;

procedure TForm1.AppMessage(var Msg: TMSG; var Handled: Boolean);
begin
  // let your application handle all messages initially
  Handled := False;
  case Msg.Message of
    WM_SYSKEYDOWN:
      if Msg.wParam = VK_F4 then
        Handled := True; // don't allow ALT-F4
  end;
end;

Regards
Marko

0
ZhaawZSoftware DeveloperCommented:
To "globally disable" alt+f4:

procedure TForm1.Button1Click(Sender: TObject);
begin
RegisterHotKey(handle, 1, mod_alt, vk_f4);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
UnregisterHotKey(handle, 1);
end;

After registering hot key with mod_alt and vk_f4 parameters, windows (any of them) will not be closed after pressing f4 - instead of it your application will receive wm_hotkey message.
0
ZhaawZSoftware DeveloperCommented:
Just another way (something like prevarant posted, but with catching only form's messages, not whole application's)


unit Unit1;

interface

uses
  Windows, Messages, Classes, Forms;

type
  TForm1 = class(TForm)
  private
    procedure WndProc (var msg : TMessage); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WndProc;
begin
if (msg.Msg = wm_syskeydown) then begin
  if (msg.wParam = vk_f4) and (msg.lParam and (1 shl 29) <> 0) then msg.Msg := wm_null;
end;
inherited;
end;

end.
0
ZhaawZSoftware DeveloperCommented:
srbenavrbe, the way you did it (as I understood)

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if (ssAlt in shift) and (key = vk_f4) then key := 0;
end;


Works fine, but disables 'close' only on pressing alt+f4 (user will still be able to close an application with his mouse).
This works only with onKeyDown event (not with onKeyUp), because form is closed when you press alt+f4, not release them.
0
ZhaawZSoftware DeveloperCommented:
You may also work with form's onShortCut event to get similar result as if working with onKeyDown event. onShortCut event occurs before any other with keyboard releated events. If you change 'Handled' variable to true in this event, other events (onKeyDown, onKeyUp, onKeyPress) will not 'see' that a key was pressed.

procedure TForm1.FormShortCut(var Msg: TWMKey; var Handled: Boolean);
begin
if (msg.CharCode = vk_f4) and (msg.KeyData and (1 shl 29) <> 0) then handled := true;
end;

msg.Msg will be the message (usually wm_keydown or wm_keyup) that is received. There's no need to check it in this case.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.