private void Kb_KeyDown(object sender, RawKeyEventArgs args)
        {
            var keys = _settings.Get("keyList");

            bool exists = false;

            foreach (var key in keys)
            {
                if (key.Contains(args.VKCode.ToString()))
                {
                    exists = true;
                    break;
                }
            }


            BeginInvoke((MethodInvoker)(() =>
            {
                keyboardCounterKeyClick.Close();
                keyboardCounterKeyClick.Dispose();
                keyboardCounterKeyClick = null;
                if (!exists)
                {
                    //get file name to save
                    var filenameFrm = new ClickCounterFileName();
                    filenameFrm.ShowDialog();
                    string filename = filenameFrm.textBox_FileName.Text;
                    filenameFrm.Dispose();

                    //check if filename is valid
                    string invalidChars = new string(Path.GetInvalidFileNameChars());
                    foreach (var invalidChar in invalidChars)
                    {
                        if (filename.Contains(invalidChar))
                        {
                            return;
                        }
                    }


                    if (filename.Trim() != string.Empty)
                    {
                        if (!filename.EndsWith(".txt"))
                        {
                            filename += ".txt";
                        }

                        if (AddKey(args.VKCode, filename.Trim()))
                        {
                            this.dataGridView1.Rows.Add(args.Key, "0", filename.Trim());
                        }
                        else
                        {
                            MessageBox.Show("This filename has been already used!", "Error", MessageBoxButtons.OK,
                                            MessageBoxIcon.Exclamation);
                        }
                    }
                }
            }));
        }
 private void button_AddKey_Click(object sender, EventArgs e)
 {
     if (keyboardCounterKeyClick == null)
     {
         keyboardCounterKeyClick = new KeyboardCounterKeyClick();
     }
     keyboardCounterKeyClick.Location = new Point(this.Location.X + 20, this.Location.Y + 20);
     keyboardListener                 = new KeyboardListener(true);
     keyboardListener.KeyDown        += Kb_KeyDown;
     keyboardCounterKeyClick.Closing += KeyboardCounterKeyClick_Closing;
     keyboardCounterKeyClick.ShowDialog();
 }
 private void button_AddKey_Click(object sender, EventArgs e)
 {
     if (keyboardCounterKeyClick == null)
         keyboardCounterKeyClick = new KeyboardCounterKeyClick();
     keyboardCounterKeyClick.Location = new Point(this.Location.X + 20, this.Location.Y + 20);
     keyboardListener = new KeyboardListener(true);
     keyboardListener.KeyDown += Kb_KeyDown;
     keyboardCounterKeyClick.Closing += KeyboardCounterKeyClick_Closing;
     keyboardCounterKeyClick.ShowDialog();
 }
        private void Kb_KeyDown(object sender, RawKeyEventArgs args)
        {
            var keys = _settings.Get("keyList");

            bool exists = false;
            foreach (var key in keys)
            {
                if (key.Contains(args.VKCode.ToString()))
                {
                    exists = true;
                    break;
                }
            }

            BeginInvoke((MethodInvoker)(() =>
               {
               keyboardCounterKeyClick.Close();
               keyboardCounterKeyClick.Dispose();
               keyboardCounterKeyClick = null;
               if (!exists)
               {
                   //get file name to save
                   var filenameFrm = new ClickCounterFileName();
                   filenameFrm.ShowDialog();
                   string filename = filenameFrm.textBox_FileName.Text;
                   filenameFrm.Dispose();

                   //check if filename is valid
                   string invalidChars = new string(Path.GetInvalidFileNameChars());
                   foreach (var invalidChar in invalidChars)
                   {
                       if (filename.Contains(invalidChar))
                           return;
                   }

                   if (filename.Trim() != string.Empty)
                   {
                       if (!filename.EndsWith(".txt"))
                           filename += ".txt";

                       AddKey(args.VKCode, filename.Trim());
                       this.dataGridView1.Rows.Add(args.Key, "0", filename.Trim());
                   }
               }
               }));
        }