Example #1
0
        public static void KeyboardWrite(string ks)
        {
            ks = ks.ToUpper();

            for (int i = 0; i < ks.Length; i++)
            {
                List <byte> Keys = new List <byte>();
                if (ks[i] == '{')
                {
                    int iof;
                    if ((iof = ks.Substring(i).IndexOf('}')) != -1)
                    {
                        Console.WriteLine("i : " + i + ", iof : " + iof);

                        string   content     = ks.Substring(i + 1, iof - 1);
                        string[] SpecialKeys = content.Contains("+") ? content.Split('+') : new string[] { content };

                        foreach (string SpecialKey in SpecialKeys)
                        {
                            byte?CurrentKey = null;
                            switch (SpecialKey)
                            {
                            case "ENTER":
                                CurrentKey = 0x0D;
                                break;

                            case "ALT":
                                CurrentKey = 0x12;
                                break;

                            case "CTRL":
                                CurrentKey = 0x11;
                                break;

                            case "TAB":
                                CurrentKey = 0x09;
                                break;

                            case "START":
                                CurrentKey = 0x5B;
                                break;

                            case "ESC":
                                CurrentKey = 0x1B;
                                break;
                            }
                            if (SpecialKey.StartsWith("F"))
                            {
                                string fnum = SpecialKey.Substring(1);
                                try {
                                    CurrentKey = (byte)(0x70 + (byte)Int32.Parse(fnum) - 1);
                                }
                                catch { }
                                Console.WriteLine("FNUM : " + fnum);
                            }

                            if (!CurrentKey.HasValue)
                            {
                                if (content.EndsWith("MS") && !content.Contains("+"))
                                {
                                    int ms = Int32.Parse(SpecialKey.Substring(0, SpecialKey.Length - 2));
                                    Console.WriteLine("Waiting " + ms);

                                    Thread.Sleep(ms);

                                    continue;
                                }
                                else
                                {
                                    CurrentKey = (byte)SpecialKey[0];
                                }
                            }

                            Keys.Add(CurrentKey.Value);

                            Console.WriteLine("Special Key: " + SpecialKey);
                        }

                        i += iof;
                    }
                }
                else
                {
                    Keys.Add((byte)ks[i]);
                }

                foreach (byte b in Keys)
                {
                    Console.WriteLine("Pressed: " + b);
                    KeyDown(b);
                }
                foreach (byte b in Keys)
                {
                    Console.WriteLine("Released: " + b);
                    KeyUp(b);
                }
            }
        }