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

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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


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

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

601 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