Skip to content

Instantly share code, notes, and snippets.

@Citillara
Created February 6, 2024 19:20
Show Gist options
  • Save Citillara/834c559391e56fe99fd6d4845c349167 to your computer and use it in GitHub Desktop.
Save Citillara/834c559391e56fe99fd6d4845c349167 to your computer and use it in GitHub Desktop.
Keyboard volume intercepter
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace VolumeFucker
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyCustomApplicationContext());
}
}
public class MyCustomApplicationContext : ApplicationContext
{
private NotifyIcon trayIcon;
private static IntPtr _hookID = IntPtr.Zero;
private const int WH_KEYBOARD_LL = 13;
private const int WM_KEYDOWN = 0x0100;
private LowLevelKeyboardProc _proc = HookCallback;
public MyCustomApplicationContext()
{
// Initialize Tray Icon
trayIcon = new NotifyIcon()
{
Icon = new Icon("main.ico"),
ContextMenu = new ContextMenu(new MenuItem[] {
new MenuItem("Exit", Exit)
}),
Visible = true,
Text = "Volume key intercepter"
};
// Keyboard hook here
_hookID = SetHook(_proc);
}
void Exit(object sender, EventArgs e)
{
// Remove hook and dispose of tray icon before exiting
UnhookWindowsHookEx(_hookID);
trayIcon.Visible = false;
Application.Exit();
}
// Hook setup and callback methods
private IntPtr SetHook(LowLevelKeyboardProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
GetModuleHandle(curModule.ModuleName), 0);
}
}
private delegate IntPtr LowLevelKeyboardProc(
int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
int vkCode = Marshal.ReadInt32(lParam);
// Check if the key is your volume key, then discard it
//Debug.Print(vkCode.ToString());
if (vkCode == 174 || vkCode == 175)
{
return (IntPtr)1; // Return 1 to discard the key event
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment