Advertisement
Guest User

Untitled

a guest
Mar 13th, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. using Gma.System.MouseKeyHook;
  10.  
  11. using System.Diagnostics;
  12.  
  13.  
  14. namespace MouseFixer
  15. {
  16.     public class TheContext : ApplicationContext
  17.     {
  18.  
  19.         // https://stackoverflow.com/questions/30061813/intercept-mouse-click
  20.  
  21.         StringBuilder logText;
  22.  
  23.         long lastClickTime;
  24.         long lastMouseUp;
  25.         bool ignoreClick = false;
  26.  
  27.         void writeLog(string msg)
  28.         {
  29.             logText.Append(msg + Environment.NewLine);
  30.             File.AppendAllText("log.txt", logText.ToString());
  31.             logText.Clear();
  32.         }
  33.  
  34.         bool errorShown = false;
  35.         void errorMsg(string str)
  36.         {
  37.             if(!errorShown)
  38.             MessageBox.Show(str);
  39.  
  40.             errorShown = true;
  41.         }
  42.  
  43.         long getTime()
  44.         {
  45.            return DateTimeOffset.Now.ToUnixTimeMilliseconds();
  46.         }
  47.  
  48.         public TheContext()
  49.         {
  50.            
  51.             Application.ApplicationExit += new EventHandler(this.OnExit);
  52.  
  53.             logText = new StringBuilder();
  54.  
  55.             lastClickTime = getTime();
  56.             lastMouseUp = getTime();
  57.  
  58.             Hook.GlobalEvents().MouseDownExt += async (sender, e) =>
  59.             {
  60.                 if (e.Button == MouseButtons.Left)
  61.                 {
  62.                     //  e.Handled = true;
  63.  
  64.                     //  writeLog("Handling click DOWN! " + e.Delta);
  65.  
  66.                     long lmu = (getTime() - lastMouseUp);
  67.  
  68.                     if (lmu < 10)
  69.                     {
  70.                         Debug.WriteLine("Too fast click - ignoring " + (getTime() - lastMouseUp) + Environment.NewLine);
  71.                         e.Handled = true;
  72.                         ignoreClick = true;
  73.                     }
  74.  
  75.                     long lct = getTime() - lastClickTime;
  76.  
  77.                     lastClickTime = getTime();
  78.  
  79.                     Debug.WriteLine("MouseDOWN " + lct + " ( " + lmu + " ) " + Environment.NewLine);
  80.                 }
  81.             };
  82.  
  83.             Hook.GlobalEvents().MouseUpExt += async (sender, e) =>
  84.             {
  85.                 if (e.Button == MouseButtons.Left)
  86.                 {
  87.                     if (!ignoreClick)
  88.                     {
  89.  
  90.                         //  e.Handled = true;
  91.  
  92.                         //    writeLog("Handling click UP! " + e.Delta);
  93.  
  94.                         long lct = getTime() - lastClickTime;
  95.  
  96.                         lastClickTime = getTime();
  97.  
  98.                         Debug.WriteLine("MouseUP " + lct + Environment.NewLine);
  99.  
  100.                         lastMouseUp = getTime();
  101.  
  102.  
  103.                     }
  104.                     else
  105.                     {
  106.                         Debug.WriteLine("Ignoring click " + Environment.NewLine);
  107.  
  108.                         e.Handled = true;
  109.                         ignoreClick = false;
  110.                     }
  111.                 }
  112.             };
  113.  
  114.  
  115.                 }
  116.  
  117.         private void OnExit(object sender, EventArgs e)
  118.         {
  119.           //  File.AppendAllText("log.txt", logText.ToString());
  120.         }
  121.  
  122.  
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement