Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
Hi,

I just want to ask if anyone can provide me with the code on how to trap or hook the key alt f4?

I only need a simple example.
Based from what I see on some threads, they tend to provide complicated code, and I'm just new in c#.
Basically I just want to know the simplest way to trap the key alt f4 and hope someone can provide me with it.

Thanks
Posted
Updated 26-Mar-18 20:51pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Mar-11 4:17am    
Tag the question properly especially if you need any more information. WPF? WinForms? ASP.NET?
Next time, tag accurately from the very beginning.
--SA
Dalek Dave 24-Mar-11 9:53am    
Edited for Grammar and Readability.
Madzmar25 26-Mar-11 0:11am    
Thanks for the help guys... i tried your sample using e.cancel= true and it works... thanks again

It may be useful to tell us what you want to achieve...
If you just want to prevent the user from closing the Form / Application by using Alt+F4, you may set e.Cancel = true; in your FormClosing event.
 
Share this answer
 
v2
Comments
Dalek Dave 24-Mar-11 9:53am    
Good Answer.
Sergey Alexandrovich Kryukov 24-Mar-11 12:39pm    
Good point, my 5. I'll edit my Answer to reference your advice. Maybe handling the key press is not needed at all.
--SA
Sergey Alexandrovich Kryukov 24-Mar-11 12:46pm    
I updated my Answer, added info on both WinForms and WPF.
Thank you very much.
--SA
No traps, not hooks. You need to handle System.Windows.Forms.Control.KeyDown for System.Windows.Forms, or System.Windows.FrameworkElement.KeyDown, System.Windows.FrameworkElement.PreviewKeyDown for WPF. See help on these classes and events.


[EDIT]

Maybe, handling of a keyboard event is not needed at all. As Toli noticed (please see his Answer), this is closing the a form or WPF window, so you need to handle System.Windows.Forms.Form.FormClosing (with opportunity to close the form) or System.Windows.Forms.Form.FormClosed. For WPF, see analogous System.Windows.Window.Closing and System.Windows.Window.Close events.

—SA
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 24-Mar-11 4:19am    
Tag the Question properly, then ask if you still have questions.
(Please, DO NOT POST AS Answer -- very common annoying mistake, in this case the post will be deleted.)
--SA
Madzmar25 24-Mar-11 4:51am    
CAN YOU PROVIDE ME WITH A SIMPLE CODE? THAT WILL HELP A LOT...
Sergey Alexandrovich Kryukov 24-Mar-11 12:06pm    
I don't want to put samples for all cases. You tag your Question as I asked, I'll put the sample accordingly, deal?
--SA
Sergey Alexandrovich Kryukov 24-Mar-11 12:24pm    
Please don't shout (ALL-CAPS is shouting, considered impolite in internet.) Where is your tag?
(WPF, WinFroms?)
--SA
Dalek Dave 24-Mar-11 9:53am    
Good Answer.
VB
Private Sub Frm_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'nashe ba ALT+F4 forme karnamaro bebandim

        If e.Alt AndAlso e.KeyCode = Keys.F4 Then
            e.Handled = True
        End If

        'end
    End Sub



in c#

C#
if (e.Alt && e.KeyCode == Keys.F4)
                   {

               e.Handled = true ;

                   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900