Skip to content

Handling Keyboard Inputs with Active Patterns

MIkE edited this page Sep 19, 2018 · 6 revisions

An active pattern lets you define match expressions with different number of Keys to analyse keyboard input.

Define active pattern:

let (|ContainsKeys|_|) (keys : List<Keys>) (pressedKeys : Set<Keys>) : unit option =
    match containsKeys pressedKeys keys with
    | true ->  Some ()
    | false -> None

Match expression:

match pressedKeys with
| KeysSet.ContainsKeys [ Keys.P; Keys.LeftCtrl ]
| KeysSet.ContainsKeys [ Keys.P; Keys.RightCtrl ] -> ()
| KeysSet.ContainsKey Keys.OemPeriod -> ()
| KeysSet.ContainsKeys [ Keys.LeftCtrl; Keys.LeftAlt; Keys.Delete ] -> ()

Future Improvements

  • Boolean Queries
  • List to keep KeyPress order
Clone this wiki locally