MoonKnight
MoonKnight

Reputation: 23831

Detecting the Asterisk Key on KeyDown Event

Firstly I have seen the following answer https://stackoverflow.com/a/7731051/626442 and it is not quite sufficient for my needs.

I have written an editor which has basic intellisense capabilities (see A New and Full Implementation of Generic Intellisense for an insight). I have implemented some basic SQL Server completion using this editor, but I keep getting intellisense popping up when I type the * key. I want to prevent this. Currently I do the following:

private void TextArea_KeyDown(object sender, KeyEventArgs e)
{
    if (!e.Control && e.KeyCode == Keys.Space || e.Shift)
        return;

    IntellisenseEngine.DisplayCompletion(this, (char)e.KeyValue);
}

I have recently redeveloped my control and I want to build on existing restrictions as to when and when not to show the insight window. A subset of what I want would be:

+------------------------------+-------------------+
¦   ¦ Modifier   ¦ Keys        ¦ Show Completion   ¦
¦---+------------+-------------¦-------------------¦
¦ 1 ¦ Shift      ¦ None        ¦ No                ¦
¦ 2 ¦ Shift      ¦ * (see note)¦ No                ¦
¦ 3 ¦ None       ¦ Space       ¦ No                ¦
¦ 4 ¦ Any        ¦ Arrow Keys  ¦ No                ¦
+------------------------------+-------------------+

et al. Note, the "*" e.KeyCode is D8, this is obviously not keyboard invariant and dependent on locale, hence is not sufficient.

Essentially I want my SQL intellisense to act like SQL Server Management Studio's (SQLMS), my questions are:

  1. How can I detect the asterisk char key being pressed independent of keyboard locale.

  2. What other key contions should I impose to make to suppress the pop-up of the intellisense window and to make it act like SQLMS?

I have tried using

private void TextArea_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Control) != Keys.Control && <Detect Space Bar> || 
         (Control.ModifierKeys & Keys.Shift) == Keys.Shift && e.KeyChar == '*')
    return;

    IntellisenseEngine.DisplayCompletion(this, (char)e.KeyValue);
}

But then I have the problem of detecting the space bar.

Thanks for your time.

Upvotes: 1

Views: 2841

Answers (2)

McNets
McNets

Reputation: 10807

This works for me:

private void Principal_KeyDown(object sender, KeyEventArgs e)
{
    // if no special keys detected (May, Ctrl, etc)

    char keyChar = Convert.ToChar(e.KeyValue);
    if (keyChar == '\u0010')
    {
        // your code 
    }
}

Upvotes: 0

Dotnetpickles
Dotnetpickles

Reputation: 1026

Check the modified code . Hope it will work

private void TextArea_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((Control.ModifierKeys & Keys.Contro) != Keys.Control && e.KeyChar == ' ' || 
         (Control.ModifierKeys & Keys.Shift) == Keys.Shift && e.KeyChar == '*')
    return;

    IntellisenseEngine.DisplayCompletion(this, (char)e.KeyValue);
}

Upvotes: 1

Related Questions