views:

19

answers:

1

How to determine if selection is made programmatically or by user input.

I started to write something but since there is more work I decided to seek for some out-the-box procedure or rely on the community experience.

So here is what I wrote (Note that answers in C# are welcommed too):

Private Shared Function IsUserSelect() As Boolean
    If Mouse.LeftButton = MouseButtonState.Pressed Then Return True

    Dim shift As Boolean
    shift = Keyboard.IsKeyDown(Key.LeftShift)
    If Not shift Then shift = Keyboard.IsKeyDown(Key.RightShift)

    If shift Then
        Dim arrow As Boolean
        arrow = Keyboard.IsKeyDown(Key.Right)
        If Not arrow Then arrow = Keyboard.IsKeyDown(Key.Left)
        If arrow Then Return True
    End If

    Return False
End Function

To be called at the OnSelectionChanged of a TextBox and determine if the selection is made by user or virtually.

But then I realize that there are way more options: Shift+Home, Shift+PageDown and more.
I would rather trust a tested function then reinventing the puncture wheel.
Thanks for reading.

A: 

I improved my function, but I am still open for ideas:

Private Shared ReadOnly SelectionModifiers() As Key = 
     New Key() {
         Key.Left, 
         Key.Right, 
         Key.Up, 
         Key.Down, 
         Key.PageDown, 
         Key.PageUp, 
         Key.Home, 
         Key.End
     }

Private Shared Function IsUserSelect() As Boolean
    If Mouse.LeftButton = MouseButtonState.Pressed Then Return True

    If ((Keyboard.Modifiers And ModifierKeys.Shift) = ModifierKeys.Shift) Then
        Return SelectionModifiers.Any(Function(key) Keyboard.IsKeyDown(key))
    End If

    Return False
End Function
Shimmy