Function that notices Alt+Tab; crtl+alt+del etc

Posted on 2004-04-16
Last Modified: 2011-10-03
I need a function that ends the program as soon as somebody uses hotkeys such as alt+tab ctrl+alt+ del and so on. >Windows< +"E"

I have found many functions that disable those hotkeys but i can not use those.
Please help

Question by:AIDeton

Expert Comment

ID: 10843169

Author Comment

ID: 10843686
I looked at this Getkeystate before but I encountered a fatal problem problem

the getkeystate function checks wether a key is pressed or not. and the returns a bool value. Please correct me if I am wrong.
It sounds like exactly what I need but I have not yet found the vb constant for the alt button. and I doubt that the keyascii value will help me there.

does anybody know the constant for the alt button ? i tried vbkeyalt but it does not work



Expert Comment

ID: 10843732
OK ... 'GetKeyboardStatus' would be more useful ...
I'm looking for an example ...

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.


Expert Comment

ID: 10843762

Expert Comment

ID: 10843781
Here the constents too ...


I have to go now ... bye
LVL 17

Accepted Solution

zzzzzooc earned 450 total points
ID: 10848246

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
    Timer1.Interval = 100
    Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
    If IsKeyDown(vbKeyMenu) = True And IsKeyDown(vbKeyTab) = True Then
        Call MsgBox("Alt+Tab")
    ElseIf IsKeyDown(vbKeyControl) = True And IsKeyDown(vbKeyMenu) = True And IsKeyDown(vbKeyDelete) = True Then
        Call MsgBox("Ctrl+Alt+Delete")
    ElseIf IsKeyDown(91) = True And IsKeyDown(vbKeyE) = True Then
        Call MsgBox("Windows+E")
    End If
End Sub
Private Function IsKeyDown(ByVal lKeyCode As Long) As Boolean
    IsKeyDown = GetAsyncKeyState(lKeyCode) And &H8000
End Function

Expert Comment

ID: 10859728
There is a more efficient way to that. Instead of polling the keyboard states with a timer every x miliseconds, you can use the low level callback API from Windows. This will minimize the system load and your procedure will only be triggered when a key was pressed. It will also allow you to create that code without having to use a form.

Add the following code to a new module, then Call InitHook() in Sub Main and TermHook() when your unload your app. When you press a key, KeyHandler() will be called, and you can use code similar to the above to determine button states (codes).

Option Explicit

'API functions
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)

'Data structures
Private Type POINT
    x As Integer
    y As Integer
End Type

    vkCode As Long
    scanCode As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type

    pt As POINT
    mouseData As Long
    flags As Long
    time As Long
    dwExtraInfo As Long
End Type

'Callback constants
Private Const WH_KEYBOARD_LL As Long = 13
Private Const WH_MOUSE_LL As Long = 14
Private Const HC_ACTION As Long = 0

'Keyboard constants
Private Const WM_KEYDOWN As Long = &H100
Private Const WM_KEYUP As Long = &H101
Private Const WM_SYSKEYDOWN As Long = &H104
Private Const WM_SYSKEYUP As Long = &H105

'Mouse constants
Private Const WM_LBUTTONDOWN As Long = &H201
Private Const WM_LBUTTONUP As Long = &H202
Private Const WM_MOUSEMOVE As Long = &H200
Private Const WM_MOUSEWHEEL As Long = &H20A
Private Const WM_RBUTTONDOWN As Long = &H204
Private Const WM_RBUTTONUP As Long = &H205
Private Const WHEEL_DELTA As Long = 120

Private KeyHookHandle As Long
Private MouseHookHandle As Long

Public Sub KeyHandler(Code As Long)
        MsgBox Code
End Sub

Public Function KeyHook(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    KeyHook = True
    If nCode = HC_ACTION Then
        CopyMemory Data, ByVal lParam, Len(Data)
        Select Case wParam
        Case WM_KEYUP
            KeyHandler Data.vkCode
        End Select
        KeyHook = CallNextHookEx(KeyHookHandle, nCode, wParam, lParam)
        KeyHook = CallNextHookEx(KeyHookHandle, nCode, wParam, lParam)
    End If

End Function

Public Sub InitHook()
    KeyHookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyHook, App.hInstance, 0)
End Sub

Public Sub TermHook()
    UnhookWindowsHookEx KeyHookHandle
End Sub

Author Comment

ID: 10907015
hi vware,

I did what you told me (and thatnks I always wanted to know how to create a program in VB without a form)
But here i have some questions:

1. when i run the program how do I end it again? (I know silly question)
   because the editor says it did already end. but the msgboxes keep  
   popping up

2. It still only gives me one button. what do I have to do to determin if lets  
   say alt +tab has been pressed? because there it only gives me the value     for alt

But thanks though you have helped me a lot already.


Author Comment

ID: 10963741
uhm Vware I tried your code now and it has one problem it is impossible to check wether alt etc is pushed because the event is released when you release the button... so that means the program notices it too late.. it at all... the functions can not handle a multiple keypress....
I have increased(from 250-450) the points sicne it seems to be a more difficult problem and I seriously need the answer soon.

Thank you for your help


Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

803 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