Solved

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

Posted on 2004-04-16
10
283 Views
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

AIDeton
0
Comment
Question by:AIDeton
10 Comments
 
LVL 6

Expert Comment

by:mmusante
ID: 10843169
0
 

Author Comment

by:AIDeton
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

AIDeton

0
 
LVL 6

Expert Comment

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

0
 
LVL 6

Expert Comment

by:mmusante
ID: 10843762
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 6

Expert Comment

by:mmusante
ID: 10843781
Here the constents too ...

(http://www.webtropy.com/articles/art9-1.asp?f=GetKeyboardState)

I have to go now ... bye
0
 
LVL 17

Accepted Solution

by:
zzzzzooc earned 450 total points
ID: 10848246
Form1:
--------------------

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
0
 
LVL 1

Expert Comment

by:vware
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

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

Private Type MSLLHOOKSTRUCT
    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

    Dim Data As KBDLLHOOKSTRUCT
    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)
    Else
        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
0
 

Author Comment

by:AIDeton
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.

AIDeton
0
 

Author Comment

by:AIDeton
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

AIDeton
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now