Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

disable alt F4

Posted on 2005-03-17
11
Medium Priority
?
996 Views
Last Modified: 2010-04-05
How do you disable alt f4 ??
Tried this :
if (Key = VK_F4) and (ssAlt in Shift) then Key := 0;
but it does not work...
0
Comment
Question by:srbenavrbe
11 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13563703
Key := #0
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 200 total points
ID: 13563737
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
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13563750
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:robert_marquardt
ID: 13565226
I think you are barking the wrong tree. You should not block all means of closing your app. Write an OnCloseQuery handler.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 13565989
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
 
LVL 1

Expert Comment

by:Qosai_DBA
ID: 13567244
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
 
LVL 2

Expert Comment

by:prevarant
ID: 13567707
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13568299
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13568454
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13568490
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
 
LVL 11

Expert Comment

by:ZhaawZ
ID: 13568539
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question