|
using System; |
|
using System.Collections.Generic; |
|
using System.Diagnostics; |
|
using System.Runtime.InteropServices; |
|
|
|
// Gambiarra feita pra bloquear o botão do meu mouse que estava bugado... |
|
// Alguns creditos para: https://github.com/rvknth043/Global-Low-Level-Key-Board-And-Mouse-Hook/blob/master/GlobalLowLevelHooks/MouseHook.cs |
|
namespace MouseHook { |
|
internal class Program { |
|
|
|
private static readonly IntPtr BNT_RETURN_CODE_1 = new IntPtr(523); |
|
private static readonly IntPtr BNT_RETURN_CODE_2 = new IntPtr(524); |
|
|
|
public static void Main(string[] args) { |
|
var ptrs = HookInAllProcesses(HookCallback); |
|
|
|
System.Windows.Forms.Application.Run(); |
|
|
|
foreach (var ptr in ptrs) { |
|
UnhookWindowsHookEx(ptr); |
|
} |
|
} |
|
|
|
public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { |
|
if (nCode >= 0 && (wParam == BNT_RETURN_CODE_1 || wParam == BNT_RETURN_CODE_2)) { |
|
Console.WriteLine("blocked " + wParam); |
|
return new IntPtr(1); |
|
} |
|
|
|
return CallNextHookEx(IntPtr.Zero, nCode, wParam, lParam); |
|
} |
|
|
|
|
|
private static List<IntPtr> HookInAllProcesses(MouseHookHandler hook) { |
|
var ptrs = new List<IntPtr>(); |
|
|
|
foreach (var process in Process.GetProcesses()) { |
|
if (process.MainWindowHandle == IntPtr.Zero) // É processo em background |
|
continue; |
|
|
|
try { |
|
using (var module = process.MainModule) { |
|
var ptr = SetWindowsHookEx(WH_MOUSE_LL, hook, GetModuleHandle(module.ModuleName), 0); |
|
ptrs.Add(ptr); |
|
} |
|
} catch (Exception ex) { |
|
Console.WriteLine("Cannot hook in " + process); |
|
Console.WriteLine(ex); |
|
} |
|
} |
|
|
|
return ptrs; |
|
} |
|
|
|
private const int WH_MOUSE_LL = 14; |
|
|
|
private delegate IntPtr MouseHookHandler(int nCode, IntPtr wParam, IntPtr lParam); |
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|
private static extern IntPtr SetWindowsHookEx(int idHook, MouseHookHandler lpfn, IntPtr hMod, uint dwThreadId); |
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|
[return: MarshalAs(UnmanagedType.Bool)] |
|
public static extern bool UnhookWindowsHookEx(IntPtr hhk); |
|
|
|
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam); |
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] |
|
private static extern IntPtr GetModuleHandle(string lpModuleName); |
|
} |
|
} |