keyboard shortcut in UWP

I have a KeyDown event defined in usercontrol. At the moment it works when you press Enter. I want it to also handle the ctrl + v key combination so that the copied text is pasted and some action has been performed. Explanation: my KeyDown event handler receives a pressed key, if it's Enter, it gets the current focus in this control (for example, focus on some textBox), checks if there are any data in the TextBox "X", and if so, the other TextBox is autocomplete. And I need that when you insert text in the TextBox "X", autocomplete the rest of the TextBox. How to force the program and execute the insertion and execute my autocomplete code?

private async void OnKeyDown(object sender, KeyRoutedEventArgs e)
{
    if (e.Key != VirtualKey.Enter) return;

    var focusedElement = FocusManager.GetFocusedElement();

    if (focusedElement == LocationName || focusedElement == AddressTextBox || 
        focusedElement == Latitude || focusedElement == Longitude)
    {
        e.Handled = true;
    }

    if (IsAddressTextValid)
    {
        var mapLocation = await FindFirstMapLocationAsync(AddressTextBox.Text);

        if (mapLocation != null)
        {
            LatitudeText = mapLocation.Point.Position.Latitude.ToString();
            LongitudeText = mapLocation.Point.Position.Longitude.ToString();
        }
    }
}

Upvotes: 4

Views: 1561

Answers (1)

Barry Wang
Barry Wang

Reputation: 1469

To handle Ctrl+V, actually Justin has a post which can helps.

 Window.Current.CoreWindow.KeyDown += (s, e) =>
        {
            var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
            if (ctrl.HasFlag(CoreVirtualKeyStates.Down) && e.VirtualKey == VirtualKey.V)
            {
                // do your stuff
                Debug.WriteLine("ctrl+V has been triggered");
            }
            if(e.VirtualKey==VirtualKey.Enter)
            {
                Debug.WriteLine("Enter triggered");
            }

        };

But for how to force update other textboxs, not so sure about your logic. Change the text not work for you?

Upvotes: 4

Related Questions