Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AvalonEdit is not showing the data in CompletionWindow for KeyDown event

I am using AvalonEdit as my TextEditor and is not showing the data in the CodeCompletionWindow when it is called from Key_Down button, however everything works fine when handled as Text_Entered event. Below is the sample code

[Serializable]
public class CodeEditor : TextEditor
{
    public CompletionWindow CompletionWindow = null;
    public CodeEditor()
    {
        //CompletionWindow pops up without any data.
        this.TextArea.KeyDown += TextArea_KeyDown; 
        //CompletionWindow pops up and data is displayed.
        this.TextArea.TextEntered += this.OnTextEntered;
    }

    void TextArea_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Space && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
        {
            ShowCompletion("");
        }
    }

    void OnTextEntered(object sender, TextCompositionEventArgs e)
    {
        //e.Handled = true;

        if (e.Text == "\n" || e.Text == "\t" || e.Text == " ")
        {
            return;
        }

        this.ShowCompletion(e.Text);
    }

    private void ShowCompletion(string enteredText)
    {
        CompletionWindow = new CompletionWindow(TextArea);
        IList<ICompletionData> data = CompletionWindow.CompletionList.CompletionData;
        data.Add("ABC");
        CompletionWindow.Show();
        CompletionWindow.Closed += delegate
        {
            CompletionWindow = null;
        };
    }
}
like image 999
Saqwes Avatar asked Aug 15 '15 07:08

Saqwes


1 Answers

Got the answer. Added e.Handled = true in KeyDown Event Handler.

Working code.

void TextArea_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Space && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
    {
        ShowCompletion("");
        e.Handled = true;
    }
}
like image 165
Saqwes Avatar answered Nov 12 '22 03:11

Saqwes