Capturing ESC key in DataReport window


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Capturing ESC key in DataReport window

  1. #1
    Jeff Ross Guest

    Capturing ESC key in DataReport window

    Hello,

    A friend wants to be able to press ESC in a DataReport window and have the
    window close. I'm not finding any events/properties within the DataReport
    to help with this like you can in a Form_KeyDown event. I know the
    DataReport is very limited. Still maybe there is an API way of solving the
    problem.

    Any suggestions?

    Thanks,
    Jeff Ross



  2. #2
    Paul Mc Guest

    Re: Capturing ESC key in DataReport window


    G'day Jeff.

    I gave data reports a very brief look in a year or two ago so I am not saying
    that there is no simpler way of doing this. But, you can subclass a window
    to capture extra keyboard events that are not exposed through the VB event
    system.

    There are a few things to be careful of. When you subclass a window you must
    *always* unhook the window before its destruction.

    If you are intercepting keyboard events, as is the case in this code, you
    are actually subclassing the entire application thread (not a single window)
    so you will want to make sure that the hook is released when the datareport
    window loses focus; it will just slow the app down with unnecessary overhead.
    Plus, you do *not* want to step through your code using keyboard shortcuts
    when the window is hooked - try it and you will see why ;-)

    Also, you will always want to pass on the messages to the original handler
    after you have done the extra processing - that is what the CallNextHookEx
    function call does in the code below...

    Post all of this into a normal .bas module. The KeyboardProc cannot be in
    a class....

    Cheers,
    Paul

    'API for subclassing your thread
    Private Declare Function CallNextHookEx Lib "user32" _
    (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, ByVal
    lParam As Long) 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


    'Module level vars
    'Original window proc - you need this
    Private mlH_Keyboard_Hook As Long

    'Control flag
    Private bKeyboardIsHooked As Boolean

    'This sub subclasses the app thread for keyboard events
    Public Sub HookKeyboard(ByVal bHook As Boolean)
    'The bHook param flags whether to hook/unhook
    On Error Resume Next

    Const WH_KEYBOARD As Long = 2

    If bHook Then
    'Hooking it twice would overwrite the value of mlH_Keyboard_Hook

    If bKeyboardIsHooked Then Exit Sub
    mlH_Keyboard_Hook = SetWindowsHookEx(WH_KEYBOARD, _
    AddressOf KeyboardProc, 0&, App.ThreadID)
    bKeyboardIsHooked = True
    Else
    If Not bKeyboardIsHooked Then Exit Sub
    Call UnhookWindowsHookEx(mlH_Keyboard_Hook)
    bKeyboardIsHooked = False
    End If

    End Sub

    'This sub handles the keyboard events - must be in bas module

    Public Function KeyboardProc(ByVal nCode As Long, ByVal wParam As Long, ByVal
    lParam As Long) As Long

    On Error Resume Next

    'Do your processing

    'Always return control to the original message handler before exiting.
    KeyboardProc = CallNextHookEx(mlH_Keyboard_Hook, nCode, wParam, lParam)

    End Function


  3. #3
    Jeff Ross Guest

    Re: Capturing ESC key in DataReport window

    Paul,

    Thanks for the info. I was sure there had to be a way.

    Jeff



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center