tags:

views:

81

answers:

2

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.

+2  A: 

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
You are right, I modified the question to show where I went wrong. What is the difference between `+` and `|||` in this case?
Muhammad Alkarouri
@Muhammad: `|||` is the bitwise or operator. `+` is addition.
Mauricio Scheffer
+4  A: 

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
Matajon
I think you can use `Event.add` instead of `Event.map` and then drop the `|> ignore`.
kvb
@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