?
Solved

disable alt F4

Posted on 2005-03-17
11
Medium Priority
?
951 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

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!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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