Bitterblue
Bitterblue

Reputation: 14085

Key strokes to enter chars in text boxes?

How do I know programmatically if a char like '@' needs to be entered in a text box with Alt Gr or Shift held down ?

This is the list for Alt Gr: ² ³ { [ ] } \ @ € ~ | µ
And a longer list for Shift: °!"§$%&/()=?*'>;:_, letters and more

Char.IsUpper(...) helps me with the letters. But how do I solve this for the rest ? I want to be able to check them all correctly and I would like to make the system do if for me if possible.

In the end I will be sending those key strokes via keybd_event.
(I use WinForms.)

Upvotes: 2

Views: 194

Answers (2)

Bitterblue
Bitterblue

Reputation: 14085

I figured it out. VkKeyScan is the key.
(VkKeyScanEx for KeyboardLayouts etc.)

Some test code:

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern short VkKeyScan(char ch);

static void Main(string[] args)
{
    char[] chars = "²³{[]}\\@€~|µ   °!`\"§$%&/()=?*'>;:_".ToCharArray();
    Console.WriteLine("char\talt\tcontrol\tshift" + Environment.NewLine + "-----------------------------");

    for (int i = 0; i < chars.Length; i++)
    {
        char ch = chars[i];
        short vksc = VkKeyScan(ch);

        bool alt = (vksc & 1024) == 1024;
        bool control = (vksc & 512) == 512;
        bool shift = (vksc & 256) == 256;

        Console.WriteLine(ch + "\t" + alt + "\t" + control + "\t" + shift);
    }

    Console.ReadKey();
}

Upvotes: 1

Srinivas
Srinivas

Reputation: 52

you can find the key modifiers using following

if ((Control.ModifierKeys & Keys.Shift) == Keys.Shift)
{
    //control key 
}

if ((Control.ModifierKeys & Keys.Shift) == Keys.Alt)
{
    //Alt
}

Source links

Determine Which Modifier Key Was Pressed

Modifier Key Details

Upvotes: 0

Related Questions