Пример #1
0
        public void KeyboardHook(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)0x0100)
            {
                int code = Marshal.ReadInt32(lParam);

                Console.WriteLine("You hit: " + code);

                int X = 0;
                int Y = 0;

                if (code == (int)'Q')
                {
                    X = 0;
                    Y = 1;
                }
                else if (code == (int)'W')
                {
                    X = 1;
                    Y = 1;
                }
                else if (code == (int)'E')
                {
                    X = 2;
                    Y = 1;
                }
                else if (code == (int)'R')
                {
                    X = 3;
                    Y = 1;
                }
                else if (code == (int)'T')
                {
                    X = 4;
                    Y = 1;
                }
                else if (code == (int)'Y')
                {
                    X = 5;
                    Y = 1;
                }
                else if (code == (int)'U')
                {
                    X = 6;
                    Y = 1;
                }
                else if (code == (int)'I')
                {
                    X = 7;
                    Y = 1;
                }
                else if (code == (int)'O')
                {
                    X = 8;
                    Y = 1;
                }
                else if (code == (int)'P')
                {
                    X = 9;
                    Y = 1;
                }
                else if (code == (int)'A')
                {
                    X = 0;
                    Y = 2;
                }
                else if (code == (int)'S')
                {
                    X = 1;
                    Y = 2;
                }
                else if (code == (int)'D')
                {
                    X = 2;
                    Y = 2;
                }
                else if (code == (int)'F')
                {
                    X = 3;
                    Y = 2;
                }
                else if (code == (int)'G')
                {
                    X = 4;
                    Y = 2;
                }
                else if (code == (int)'H')
                {
                    X = 5;
                    Y = 2;
                }
                else if (code == (int)'J')
                {
                    X = 6;
                    Y = 2;
                }
                else if (code == (int)'K')
                {
                    X = 7;
                    Y = 2;
                }
                else if (code == (int)'L')
                {
                    X = 8;
                    Y = 2;
                }
                else if (code == 186) // ;
                {
                    X = 9;
                    Y = 2;
                }
                else if (code == (int)'Z')
                {
                    X = 0;
                    Y = 3;
                }
                else if (code == (int)'X')
                {
                    X = 1;
                    Y = 3;
                }
                else if (code == (int)'C')
                {
                    X = 2;
                    Y = 3;
                }
                else if (code == (int)'V')
                {
                    X = 3;
                    Y = 3;
                }
                else if (code == (int)'B')
                {
                    X = 4;
                    Y = 3;
                }
                else if (code == (int)'N')
                {
                    X = 5;
                    Y = 3;
                }
                else if (code == (int)'M')
                {
                    X = 6;
                    Y = 3;
                }
                else if (code == 188) // ,
                {
                    X = 7;
                    Y = 3;
                }
                else if (code == 190) // .
                {
                    X = 8;
                    Y = 3;
                }
                else if (code == 191) // /
                {
                    X = 9;
                    Y = 3;
                }

                // quick fix for removing the number rows as health
                Y--;

                Console.WriteLine("you pressed at: " + X + "," + Y);

                Boolean Hit = false;

                for (int i = 0; i < Moles.Count; i++)
                {
                    Mole mole = Moles[i];
                    if (X == mole.X && Y == mole.Y)
                    {
                        Moles.Remove(mole);
                        Console.WriteLine("You hit it!");
                        Score++;
                        Hit = true;
                    }
                }

                if (!Hit)
                {
                    Health--;
                    if (Health < 1)
                    {
                        GameLose();
                    }
                }

                Console.WriteLine("Score: " + Score);


                if (code == 27)
                {
                    Running = false;
                    Application.Exit();
                    Keyboard.Clear();
                }
            }
            CallNextHookEx(Hook, nCode, wParam, lParam);
        }