3

In C#, you can express characters for the KeyPress event in the form Keys.Control | Keys.M. In F#, Keys.Control ||| Keys.M doesn't work. What does?

Edit: Interesting indeed. Using System.Windows.Forms.Keys.Control ||| System.Windows.Forms.Keys.M as per Johannes Rössel's suggestion below in the F# interactive window works exactly as he shows. Writing it in a .fs file:

    form.KeyPress.Add (fun e ->
         if (e.KeyChar = (System.Windows.Forms.Keys.Control ||| System.Windows.Forms.Keys.M)) then textbox.SelectAll() )

gives me the error The type 'char' does not support any operators named '|||'. So I probably misidentified the location of the problem. There is no typecasting from Keys to char.

Muhammad Alkarouri
  • 23,884
  • 19
  • 66
  • 101

2 Answers2

4

Just little addition to Johaness Rössel's answer, there is nicer (or 'more functional') way to do this using reactive programming.

form.KeyDown
    |> Event.filter (fun e -> e.KeyData = (Keys.Control + Keys.M))
    |> Event.map (fun _ -> textbox.SelectAll())
    |> ignore
Martin Jonáš
  • 2,309
  • 15
  • 12
  • 1
    I think you can use `Event.add` instead of `Event.map` and then drop the `|> ignore`. – kvb Aug 15 '10 at 12:41
  • @Matajon, thanks. I will be looking at FRP later. I was just translating a tool I already had from IronPython and I didn't have time to make it more functional. – Muhammad Alkarouri Aug 16 '10 at 01:25
2

Use +:

Keys.Control + Keys.M

FWIW, ||| works for me, though:

> System.Windows.Forms.Keys.Control ||| System.Windows.Forms.Keys.M;;
val it : System.Windows.Forms.Keys = M, Control
Joey
  • 344,408
  • 85
  • 689
  • 683