我写了以下代码:
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"或其他随机密钥我在控制台上没有消息,为什么? 对于鼠标,一切正常。
答案 0 :(得分:1)
问题是您的Editor
组件没有焦点。添加以下内容可以解决问题:
e.Focus()