locked
How to check key combination in richeditbox

    Question

  • Hello,

    How to check key combination on richeditbox like shift + delete.

    Thanks in advance.

    Thursday, February 20, 2014 10:20 AM

All replies

  • I think this will help:

    http://msdn.microsoft.com/en-us/library/windows/apps/hh868246.aspx


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, February 20, 2014 1:40 PM
    Moderator
  • Hi Matt,

    Thanks for the link. But richeditbox keydown event is  not firing when delete key is pressed and I want to test for shift + delete. Anything that can be done to get it work?

    Friday, February 21, 2014 5:33 AM
  • Try this code:

     protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
            }
    
            async void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
            {
                var DeleteKey = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Delete);
                var ShiftKey = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
                Debug.WriteLine("Delete: " + DeleteKey.ToString());
                Debug.WriteLine("Shift " + ShiftKey.ToString());
                
                if (DeleteKey != CoreVirtualKeyStates.None && ShiftKey != CoreVirtualKeyStates.None)
                {
                    var MyMessageDialog = new MessageDialog("Shift + Delete are pressed!");
                    await MyMessageDialog.ShowAsync();
                }
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 21, 2014 1:17 PM
    Moderator
  • Hi Matt

    I tried your code ,but it contains undesired behavior.

    1>when the first time shift + enter is pressed message box is shown. Another time if I press only shift key(right side of keyboard) or only delete key than also message dialog opens (pls test that sometimes you have to press shift and delete alternately or more than one time).

    2>when I press first shift key than leave it and after that if I press delete key than also it shows dialog.

    I want dialog only on both shift and delete combination

    I tried following code to stop such behaviors but still its not working:

      CoreVirtualKeyStates DeleteKey = CoreVirtualKeyStates.None;
            CoreVirtualKeyStates ShiftKey = CoreVirtualKeyStates.None;
            async void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
            {
                 DeleteKey = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Delete);
                 ShiftKey = CoreWindow.GetForCurrentThread().GetKeyState(VirtualKey.Shift);
                Debug.WriteLine("Delete: " + DeleteKey.ToString());
                Debug.WriteLine("Shift " + ShiftKey.ToString());
    
                if (DeleteKey != CoreVirtualKeyStates.None && ShiftKey != CoreVirtualKeyStates.None)
                {
                    var MyMessageDialog = new MessageDialog("Shift + Delete are pressed!");
                    await MyMessageDialog.ShowAsync();
                }
            }
    
            void CoreWindow_KeyUp(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
            {
                 DeleteKey = CoreVirtualKeyStates.None;
                 ShiftKey = CoreVirtualKeyStates.None;
            }



    • Edited by zee_patel Friday, February 21, 2014 2:00 PM
    Friday, February 21, 2014 1:53 PM
  • Try this line of code instead:

    if

    (DeleteKey == CoreVirtualKeyStates.Down && ShiftKey == CoreVirtualKeyStates.Down)


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Friday, February 21, 2014 4:05 PM
    Moderator
  • Matt, I tried that also, but it is not working smoothly. I is not responding at first time. I have to press twice shift + delete combination. And sometimes its breaks when I press some modifier key individually as specially shift and delete.

    I also tried for shift+D combination as a temporary solution using following code, but in this  key 'D' is been added to textbox when shift + D combination is pressed, e.handled = false is not working.

     if (e.Key == VirtualKey.Shift) isShiftKeyPressed = true;
                else if (isShiftKeyPressed)
                {
                    if (e.Key == VirtualKey.D)
                    {
                        e.Handled = false;
                        var MyMessageDialog = new MessageDialog("Shift + D are pressed!");
                        await MyMessageDialog.ShowAsync();
                        isShiftKeyPressed = false;
                    }
                }

    Pls can you give me perfect solution for shift + delete combination.


    • Edited by zee_patel Monday, February 24, 2014 5:57 AM
    Monday, February 24, 2014 5:56 AM