• 0

[VB.NET] Capture keypress regardless the focused window


Question

Hi, I'm feeling fustrated. I have hours looking for a good code to capture the keypress events in any windows no matter if my application is focused or not. I need to create an application to work in background to capture the F2 key.

I found this example but I can't make it work:

I don't know what to do with the code colored in RED shown below, this is the part that doesn't work.

----------------------------

Implements IMessageFilter

Const WM_KEYDOWN As Integer = &H100

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Application.AddMessageFilter(New MyAppFilter)

End Sub

Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage

Select Case m.Msg

Case WM_KEYDOWN

Select Case m.WParam.ToInt32

Case Keys.F8

MsgBox("")

End Select

End Select

End Function

Edited by daniel_rh
Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I take it your Form class is implementing the interface IMessageFilter, if so, you should be able to modify the code:

Application.AddMessageFiler(Me)

Another note, your PreFilterMessage method doesn't seem to return anything, this wouldn't compile.

Link to comment
Share on other sites

  • 0
I take it your Form class is implementing the interface IMessageFilter, if so, you should be able to modify the code:

Application.AddMessageFiler(Me)

Another note, your PreFilterMessage method doesn't seem to return anything, this wouldn't compile.

Thanks! I found the solution...

http://www.dreamincode.net/code/snippet1909.htm

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.