Results 1 to 3 of 3

Thread: Trapping Sleep/Power/Wake up Key

  1. #1
    kais
    Guest

    Trapping Sleep/Power/Wake up Key

    How can i trap the Power/Sleep/Wake up key, The key code for all these key is 255 (Got from Key down event of the form)

    Actually i want to execute an exe when ever the user press these key. Can any one let me know how to do this????

    Do we have to use any API to trap the Key pressed???

    Guys/gals Its urgent, do come up with u r views and ideas.

  2. #2
    jim mcnamara
    Guest
    Playing this back to you:

    You want to trap the ASCII 255 (wake up) character anytime it is sent to the system and then decide to do something with it.. It doesn't matter which program had/has the focus.

    Your only choice is a system-wide hook. These can be implemented only as a .dll They monitor ALL user I/O to the system. There are some .dll's you can get at:

    www.desaware.com - they make a keyboard/mouse hook program that traps all of the i/o

    I do not know of any free versions. Try a www.google.com search

  3. #3
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    It is possible to do it even in VB.


    Module code
    VB Code:
    1. Option Explicit
    2. Public Type KBDLLHOOKSTRUCT
    3.     vkCode As Long
    4.     scanCode As Long
    5.     flags As Long
    6.     time As Long
    7.     dwExtraInfo As Long
    8. End Type
    9. Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    10. Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer
    11. Public 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
    12. Public Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
    13. Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    14. Public Const HC_ACTION = 0
    15. Public Const WM_KEYDOWN = &H100
    16. Public Const WM_KEYUP = &H101
    17. Public Const WM_SYSKEYDOWN = &H104
    18. Public Const WM_SYSKEYUP = &H105
    19.  
    20. Public Const WH_KEYBOARD_LL = 13
    21. Public Const LLKHF_ALTDOWN = &H20
    22.  
    23.  
    24. Dim m_udtKEYBOARDHOOK As KBDLLHOOKSTRUCT
    25.  
    26. Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    27.    
    28.     If (nCode = HC_ACTION) Then
    29.         If wParam = WM_KEYDOWN Or wParam = WM_SYSKEYDOWN Or wParam = WM_KEYUP Or wParam = WM_SYSKEYUP Then
    30.             CopyMemory m_udtKEYBOARDHOOK, ByVal lParam, Len(m_udtKEYBOARDHOOK)
    31.             'change this to call your actual EXE, otherwise it will crash
    32.             If .vkCode = 255 Then Shell "C:\YourProgram.exe", vbNormalFocus
    33.         End If
    34.     End If
    35.    
    36.     KeyboardProc = CallNextHookEx(0, nCode, wParam, ByVal lParam)
    37. End Function
    Form code
    VB Code:
    1. Dim m_lngKeyboad As Long
    2.  
    3. Private Sub Form_Load()
    4.     m_lngKeyboad = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf KeyboardProc, App.hInstance, 0)
    5. End Sub
    6.  
    7. Private Sub Form_Unload(Cancel As Integer)
    8.     If m_lngKeyboad <> 0 Then UnhookWindowsHookEx m_lngKeyboad
    9. End Sub
    Note Do not close the application while in IDE, but press X in the form's title bar. If you close it by pressing END button in IDE you wont be able to hook it again without restarting the IDE.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width