• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

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

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

1 Solution
AIDetonAuthor Commented:
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


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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Here the constents too ...


I have to go now ... bye

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
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
AIDetonAuthor Commented:
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.

AIDetonAuthor Commented:
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now