f#不能覆盖KeyPressed事件窗口表单

时间:2015-10-29 18:40:49

标签: winforms events f# override

我写了以下代码:

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)

问题是如果我按下" W"或其他随机密钥我在控制台上没有消息,为什么? 对于鼠标,一切正常。

1 个答案:

答案 0 :(得分:1)

问题是您的Editor组件没有焦点。添加以下内容可以解决问题:

e.Focus()