Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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

Posted on 2004-04-16
Medium Priority
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
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

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 ...

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.


Expert Comment

ID: 10843781
Here the constents too ...


I have to go now ... bye
LVL 17

Accepted Solution

zzzzzooc earned 900 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

636 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