Levenlol
Levenlol

Reputation: 415

f# can't override KeyPressed event windows form

I wrote the following code:

open System.Windows.Forms
open System.Drawing

let f = new Form(Text="Clock", TopMost=true)
f.Show()

type Editor() =
    inherit UserControl()

    override this.OnMouseDown e =
        printfn "Mouse Down"

    override this.OnKeyDown e =
        match e.KeyCode with
           | Keys.W -> printfn "W pressed"
           | _ -> printfn "Something else pressed"

let e = new Editor(Dock = DockStyle.Fill)
f.Controls.Add(e)

The problem is that if i press "W" or another random Key i got no message on the console, why ? For the mouse all is working fine.

Upvotes: 0

Views: 79

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243096

The problem is that your Editor component does not have focus. Adding the following solves the problem:

e.Focus()

Upvotes: 1

Related Questions