Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Listen to window messages and respond to keyevents (vb.net 2005)

Posted on 2007-08-03
9
Medium Priority
?
781 Views
Last Modified: 2012-06-21
I would like to know how to catch all keypresses in a specific window, irregardless of what control in the window has focus, provided the window has focus. Then I want to respond to the keystrokes. I tried using the builtin form event, keydown, or keypress or any of the other ones similar, but the event is not being triggered. How would I listen to the messages on the window and respond to keystrokes?

Thanks alot Experts! I totally appreciate all your help. ~Michael
0
Comment
Question by:JeffreyDurham
  • 4
  • 2
6 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 19627602
Hi Michael,

Is this "specific window" part of your application?...or an external application?

If external, this cannot be done with pure VB.Net as you would a helper DLL written in another language such as C++.

You can however use a low level keyboard hook to get all keystrokes across all applications in pure VB.Net.

If this is only for YOUR application only, what types of keys are you looking to trap?  Letter keypresses or function keys for instance?

In general, there are several ways you can do it:

(a) Set the KeyPreview property of the Form to True and then use the forms KeyDown/KeyUp and/or KeyPress events.

(b) Use something like ProcessCmdKey() in the form:
http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx

Can you give us more specifics?...
0
 

Author Comment

by:JeffreyDurham
ID: 19627810
>> Can you give us more specifics?...
Absolutaly :)

>>Is this "specific window" part of your application?...or an external application?
It's a specific window in my application. Inside it is a axWebBrowser (or the older com library version) and a textbox which holds the address of the doc, and a 'Go' button. It is always loading pdf documents inside the webbrowser, so technically this would be difficult to do through that object. This is why I wanted to just watch the keystrokes.

>>You can however use a low level keyboard hook to get all keystrokes across all applications in pure VB.Net.
I either want to do this or try your other solution. if I understand you correctly with the previewKey property making the form autolisten to whatever, that'd be the solution. So I'm going to try both, but hopefully that one will do the trick.

>>If this is only for YOUR application only, what types of keys are you looking to trap?  Letter keypresses or function keys for instance?
I am specifically only really looking to trap the number keys on above the letters, the numbers on the keypad, and the letter keys. No functions or shifts or anything like that.

Thanks Idle_Mind! ~Michael
0
 

Author Comment

by:JeffreyDurham
ID: 19627878
Ok, I've used the keyPreview property on the form, and have programmed these events.. all of them fire all of the time, which is perfect, except for the axWebBrowser1_PreviewKeyDown, which doesn't seem to always fire when the control has focus.. not sure why. I'm going to check out your other suggestions and decide what I ultimately want to do.
~Michael
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 19628280
You  may need another option that is one level "less extreme" than using a global low level keyboard hook.

Use the IMessageFilter interface which allows you to preview ALL messages intended for your entire application before they get dispatched to the correct form/control.

See here for some examples if IMessageFilter:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22696110.html
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22705287.html

You would be looking for WM_KEYDOWN messages...  ;)

0
 

Author Comment

by:JeffreyDurham
ID: 19628953
The only issue is that for some reason the webbrowser control (with the adobe document inside it) won't post events when it has the focus. This is all my code that I've got for this form, and this issue..

Option Explicit On

Public Class frmPDF
    Private WithEvents mf As New myFilter(Me)
    Private WithEvents mfWB As New myAxWebBrowserFilter()

    'Shadows PreviewKeyDown As KeyEventHandler

    'Constants
    Private Const cDefaultPath2PDFs$ = "C:\Maricopa Images\"
    Private Const cTimesDelayed As Long = 1

    'Globals
    Public gPath2PDFs$
    Public lastFile$, currFile$, lastMoved$, currMoved$

    'Locals
    Dim flagMoveFile As Boolean

    'Form Events

    Private Sub form_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Application.AddMessageFilter(mf)
        Application.AddMessageFilter(mfWB)
        Me.KeyPreview = True
        getGlobals()
    End Sub

    Private Sub frmPDF_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Debug.Print("FrmPDF_KeyDown fired!")
    End Sub

    Private Sub frmPDF_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        'Want to redirect the key back to the other form
        Debug.Print("FrmPDF_KeyPress fired!")
        'Main.Focus()
        'Main.setFocusActiveField(e.KeyChar)
    End Sub

    Private Sub frmPDF_MouseWheel(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseWheel
        Debug.Print("FrmPDF_MouseWheel fired!")
    End Sub

    Private Sub AxWebBrowser1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles AxWebBrowser1.GotFocus
        Debug.Print("AxWebBrowser1_GotFocus fired!")
        Me.Focus()
    End Sub

    Private Sub AxWebBrowser1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles AxWebBrowser1.PreviewKeyDown
        Debug.Print("AxWebBrowser1_PreviewKeyDown fired!")
    End Sub

    'Private Shadows Sub previewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
    '    Debug.Print("previewKeyDown() fired!")
    'End Sub


    Private Sub frmPDF_ResizeEnd(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.ResizeEnd
        'On Error Resume Next
        Me.AxWebBrowser1.Height = Me.Height - Me.AxWebBrowser1.Top - 30
        Me.AxWebBrowser1.Width = Me.Width - 16
    End Sub


    Private Sub MoveFile(ByVal sFilePath$)
        Dim fileName$, outPath$
        If My.Computer.FileSystem.FileExists(sFilePath) = True Then
            'MOVE FILE to OutFolder
            fileName = System.IO.Path.GetFileName(sFilePath)
            outPath = Preferences.getPDFSOutPath & fileName
            My.Computer.FileSystem.MoveFile(sFilePath, outPath)
        End If
    End Sub

    Public Sub loadFile(ByVal sFilePath$)
        currFile = sFilePath
        If My.Computer.FileSystem.FileExists(sFilePath) = True Then
            Me.fldPath2Load.Text = sFilePath
            Me.AxWebBrowser1.Navigate(sFilePath)
        End If
        lastFile = currFile
    End Sub

    Public Sub getGlobals()
        gPath2PDFs = GetSetting("PdfFileLauncher", "Startup", "Path2PDFs", cDefaultPath2PDFs)
        'gPath2SavePDF = GetSetting("PdfFileLauncher", "Startup", "Path2SavePDFs", cDefaultSavePath)
        'gPath2DB = GetSetting("PdfFileLauncher", "Startup", "Path2DB", cdefaultPath2DB)
    End Sub

    Public Sub setGlobals()
        'SaveSetting "PdfFileLauncher", "Startup", "Path2DB", gPath2DB
        SaveSetting("PdfFileLauncher", "Startup", "Path2PDFs", gPath2PDFs)
        'SaveSetting "PdfFileLauncher", "Startup", "Path2SavePDFs", gPath2SavePDF
    End Sub

    Private Sub Command1_Click()    'LoadBtn
        Me.AxWebBrowser1.Navigate(Me.fldPath2Load.Text)
    End Sub

End Class

Public Class myFilter
    Implements IMessageFilter
    'implements acrobat

    Private frm As Form

    Public Sub New(ByVal frm As Form)
        Me.frm = frm
    End Sub

    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
        Debug.Print(CStr(m.LParam) & ", " & CStr(m.WParam) & ", " & CStr(m.Msg))
        Select Case m.Msg

        End Select
    End Function
End Class

Public Class myAxWebBrowserFilter
   'This class prolly has no point. I was figuring in your second example you showed that
   'you could filter something besides a form, but now I'm thinking having read the documentation that
   'you are actually reading all the events to the application (before they're posted to the controls, which
'should /even/ include the webbrowser com control with the pdf (adobe acrobat) document loaded in it..
'at least I'd think so..
    Implements IMessageFilter
    'implements acrobat

    Private handle As IntPtr
    Private frm As Form

    Public Sub New()
       
    End Sub

    Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
        Debug.Print("AxWebB: " & CStr(m.LParam) & ", " & CStr(m.WParam) & ", " & CStr(m.Msg))
        Select Case m.Msg

        End Select
    End Function
End Class
0
 

Author Comment

by:JeffreyDurham
ID: 19629011
Yeah, I've tested this more and the only time your way of doing it doesn't work is when I've loaded a pdf in the window ):

~Michael
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Screencast - Getting to Know the Pipeline
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

578 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