Old habbits die hard


My first and very little showcase will be my newest developement, while I was waiting for my domain to be registered and active, Comment Reminder.

This little tool is in an early stage but I think it will help me to improve and get rid of
an bad habbit of mine.
I dont comment my Projects at all so I decided to write a little application which will remind me of commenting when I am in Visual Studio.Screen1

 

How it works is very simple. and my Programm does the following steps:

  1. Check if Visual Studio is the active Process and Window
  2. Activate Keylogger
  3. Register comments (register “//” )
  4. Remind if no comments are made

Needed Variables

    static string VSProcessName = "devenv";
        static Process VSProcess;
        static IntPtr hwnd;
        static uint pid;
        static bool ProcessIsActive = false;
        static List<string> LoggedKeys = new List<string>();
        public static bool CommentFound = false;
        static int timer = 10000;

Check if Visual Studio is the active Process and Window

This is done with the help of some imports. The functions needed are in the user32.dll and we import the functions:

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

After this is done we create a function which will handle the processing of the active process.

            try
            {
                hwnd = GetForegroundWindow();
                GetWindowThreadProcessId(hwnd, out pid);
                VSProcess = Process.GetProcessById((int)pid);
                if (VSProcess.ProcessName == VSProcessName)
                {
                    ProcessIsActive = true;

                }
                else
                {
                    ProcessIsActive = false;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

Activate Keylogger

When Visual Studio is the active windows we activate a keylogger. This Keylogger will catch all keyboard inputs and we will put these in a List to process them seperatly.

For this we need two functions imported from a dll.

        [DllImport("User32.dll")]
        private static extern short GetAsyncKeyState(int vKey);

        [DllImport("User32.dll")]
        private static extern short GetAsyncKeyState(Keys vKey);

Now we come to the interesting part of the programm. A Keylogger working only with .net native functions.

            try
            {
                foreach (int i in Enum.GetValues(typeof(Keys)))
                {
                    if (GetAsyncKeyState(i) == -32767)
                    {
                        Console.Write(Enum.GetName(typeof(Keys), i) + " ");
                        LoggedKeys.Add(Enum.GetName(typeof(Keys), i));
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

We get the pressed keys and enumarate them to the displayname of the pressed button. For example the left mouse button would be 0x01 as a virtual key and we change it to the value VK_LBUTTON which is the displayname of it.

Register comments

Now all we do is register if a comment was made. For this we create a string out of the List and check for a combination of buttons we need to make comments. In my case I need to hit Shift and then 7 two times which will be captured as LShiftKey,D7,D7.

try
            {
                string joined = String.Join(",", LoggedKeys.ToArray());
                if (joined.Contains("LShiftKey,D7,D7"))
                {
                    CommentFound = true;
                    Console.WriteLine("FOUND!!!\n\r");
                    LoggedKeys.Clear();
                }
            }
            catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }

This was easy, wasn’t it?

Remind if no comments are made

All we need now is a Timer which will remember you to comment your Source Code if no comments are made. For this we make a new thread, so our main thread won’t be stuck on doing this.

int tmptime = timer;
            while (true)
            {
                tmptime--;
                if (tmptime == 0 && CommentFound == false)
                {
                    MessageBox.Show("COMMENT YOUR CODE!!");
                    tmptime = timer;
                    LoggedKeys.Clear();
                    Thread.Sleep(500);
                }
                if (tmptime == 0 && CommentFound == true)
                {
                    tmptime = timer;
                    LoggedKeys.Clear();
                    CommentFound = false;
                }
                Console.Title = tmptime.ToString();
                Thread.Sleep(100);
            }

This code will be improved in the future and I will keep you guys up to date.

Leave a comment

Your email address will not be published. Required fields are marked *