Irro
Irro

Reputation: 173

Why do I get double calls to the previewKeyDown() function in c#

I use the previewKeyDown() function for some controls in my project but they always get called twice for each key press. Anyone who knows how to solve this?

And is there anyway to do a global keylistener in my project?

Upvotes: 2

Views: 3535

Answers (2)

Blake
Blake

Reputation: 56

The WebBrowser control I think has a bug, it fires that twice no matter what. I solved it via what I consider a hack, but it works. :) Mines in VB, but you get the gist of it, basically make a bool in your form's scope and use it to negate one of the two events that are fired:

    Private _skipPreviewKeyDown As Boolean = False
Private Sub WebBrowser1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles WebBrowser1.PreviewKeyDown

    If _skipPreviewKeyDown = True Then
        _skipPreviewKeyDown = False
        Exit Sub
    Else
        _skipPreviewKeyDown = True
    End If

    'Select Case e.KeyDa
    MsgBox(e.KeyValue)

End Sub

Upvotes: 4

user117499
user117499

Reputation:

Try using KeyUp event instead. I think the key repeat is causing you problem.

Upvotes: 0

Related Questions