none
Detecting If CTRL+ALT+DEL (or) ALT+F4 is Pressed...

    Question

  • I know that you cannot disable Ctrl Alt Del for security reasons. I was able to figure out how to disable ALT+F4 in my application though. What I am wanting to know is if anyone can give me some code to where If CTRL ALT DEL is pressed then my application will close. I have tried everything, and I just cannot seem to get the program to recognize if the user presses ctrl alt delete.

    So basically I need some code that detects if a user presses ctrl alt delete then if htye did it closes the application.

    (I am making a quiz program.)

    PS: I am on Windows 7 RC and I also use Windows Vista. I am using Visual Studio 2008 Professional Edition.

    Friday, May 15, 2009 9:25 PM

Answers

  • You can use the User32 API call GetAsyncKeyState function.  I tested the following code and got it to close the form before Windows took over.  I would not push up the timer interval too much past 100 because it might not detect it in time (it failed at 200 for me).

    Public Class Form1
    
        Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer
        Private WithEvents timer1 As New Timer
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            timer1.Interval = 100
            timer1.Start()
        End Sub
    
        Public Function InputCheck() As Boolean
            'Ctrl = 17
            'Alt = 18
            'Del = 46
            InputCheck = GetAsyncKeyState(17) AndAlso GetAsyncKeyState(18) AndAlso GetAsyncKeyState(46)
        End Function
    
        Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
            If InputCheck() Then
                Me.Close()
            End If
        End Sub
    
    End Class



    • Edited by Dig-Boy Saturday, May 16, 2009 12:55 AM
    • Marked as answer by oatman Tuesday, May 19, 2009 9:24 PM
    Saturday, May 16, 2009 12:52 AM

All replies

  • You can use the User32 API call GetAsyncKeyState function.  I tested the following code and got it to close the form before Windows took over.  I would not push up the timer interval too much past 100 because it might not detect it in time (it failed at 200 for me).

    Public Class Form1
    
        Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Long) As Integer
        Private WithEvents timer1 As New Timer
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            timer1.Interval = 100
            timer1.Start()
        End Sub
    
        Public Function InputCheck() As Boolean
            'Ctrl = 17
            'Alt = 18
            'Del = 46
            InputCheck = GetAsyncKeyState(17) AndAlso GetAsyncKeyState(18) AndAlso GetAsyncKeyState(46)
        End Function
    
        Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
            If InputCheck() Then
                Me.Close()
            End If
        End Sub
    
    End Class



    • Edited by Dig-Boy Saturday, May 16, 2009 12:55 AM
    • Marked as answer by oatman Tuesday, May 19, 2009 9:24 PM
    Saturday, May 16, 2009 12:52 AM
  • Dont you know that the Windows is assigned CTRL+ALT+DEL to TaskManager, Dig-Boy solution will close your application but it will open TaskManager.
    kaymaf
    I hope this helps, if that is what you want, just mark it as answer so that we can move on
    Saturday, May 16, 2009 1:31 AM
  • Based on my reading of the OP I thought the idea was to not stop Task Manager from starting (I'm not sure you can actually do that anyhow), but to close the app before TM pops up.  Here's what I latched on to from the OP...


    I know that you cannot disable Ctrl Alt Del for security reasons...  What I am wanting to know is if anyone can give me some code to where If CTRL ALT DEL is pressed then my application will close...  So basically I need some code that detects if a user presses ctrl alt delete then if [they] did it closes the application.


    However, I'm really curious what the reason is for closing when Ctrl-Alt-Del is pressed?  Considering there are many reasons one might press that beyond shutting down -- not to mention the ability to cancel and return to Windows -- I'd be mighty pissed if a program I was using always closed when I did that.
    Saturday, May 16, 2009 11:40 AM
  • I am making a quiz program for my students in class to test with. I do not want them able to press CTRL ALT DEL to go onto the internet to get the answers for my test.

    Anyhow, thank you very much! Also where did you get these numbers like 17,18, 48? Is there a key/chart you look off of to see what key goes with what command?
    Tuesday, May 19, 2009 9:20 PM
  • Well, the simple answer is that I found the decimal equivalent of hexadecimal values that are assigned to Virtual Key constants in Windows API calls.  Here's a link to the Virtual KeyCode listings by MS...

    http://msdn.microsoft.com/en-us/library/ms645540.aspx

    and here's the same code above using the constants -- which is much more common in code examples than using decimal values (I was just trying to make it accessible). 
        Private Const VK_CTRL = &H11
        Private Const VK_MENU = &H12
        Private Const VK_DELETE = &H2E
    
        Public Function InputCheck() As Boolean
            InputCheck = GetAsyncKeyState(VK_CTRL) AndAlso GetAsyncKeyState(VK_MENU) AndAlso GetAsyncKeyState(VK_DELETE)
        End Function
    

    I found a link that shows some decimal equivalents of common VK values here, but there's probably a better link for this...

    http://developer.nomadph.com/part08.html


    And it looks like kaymaf is right -- good thing I'm not a betting man!  You are trying to stop the Ctrl-Alt-Delete command from occurring.  Of course this is not possible.  Your approach seems pretty draconian.  Curious...  what's to stop them from re-starting the app (presumably with the same questions) after your app closes and they go to the internet?  And what would they be using Ctrl-Alt-Delete for to get to the internet  -- which I guess is maybe maximized and set to top-most (that could be effective :)?  Are they switching to an open process of IE?

    Maybe you could have a timer running for the duration of your app that sniffs out running processes of IE or Firefox (or whatever they have in the classroom) and kills those processes.  The following code is pretty effective at that...

    Public Class Form1
    
        Private WithEvents timer1 As New Timer
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            timer1.Interval = 5000
            timer1.Start()
        End Sub
    
        Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timer1.Tick
            For Each p As Process In Process.GetProcessesByName("firefox")
                p.Kill()
            Next
            For Each p As Process In Process.GetProcessesByName("iexplorer")
                p.Kill()
            Next
        End Sub
    
    End Class
    But I would temper this approach with some kind of message kindly reminding them that internet help is not allowed.  I hope this helps.

    Wednesday, May 20, 2009 12:38 AM
  • I was thinking, you could have some fun with this by having the app keep a log of how many times the internet was attempted to be accessed during the quiz.  Then it could send the results to a log file on the network (you'll need your students to have write permission of course) and you could have an app running on your end that reads that log file, updating you on how many infractions there were and by who.  You would, of course, announce the "cheating" attempts at the end of the quiz, giving no indication that you were monitoring it.  This could put the fear of god into them that you are always watching them even when you are not.  Sounds like good fun to me.
    Wednesday, May 20, 2009 12:45 AM