We help IT Professionals succeed at work.

hooks to see if faul language was typed in external exe

r3nder asked
can anyone help me crete hooks into an internal an external exe that notifies the company that an individual has typed an improper word (i.e. faul language)
Watch Question

Chief Technology Ninja
Distinguished Expert 2019
Hi r3ender,
Kindly see the code snippet below :
Is it what you are looking for?

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;

class InterceptKeys
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;
    private static StringBuilder statement = new StringBuilder();

    static InterceptKeys()
        _hookID = SetHook(_proc);

    private static 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);
            if(vkCode > 64 && vkCode < 91)
            else if(vkCode == 32)
                statement.Append(" ");
            if (((Keys)vkCode).Equals(Keys.Return))
	//Perform the Word filter here
	//If word filter reports bad words than you can notify by sending a mail or by logging the event/time/user etc
        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);

Open in new window


I looks great but I keep gettting this error no matter what I do
Error      1      'System.Text.StringBuilder' does not contain a definition for 'Clear' and no extension method 'Clear' accepting a first argument of type 'System.Text.StringBuilder' could be found (are you missing a using directive or an assembly reference?)      

I have createed the class InterceptKeys
and added InterceptKeys x = new InterceptKeys(); to the body of the form

but statement.clear(); will not allow it to compile


got it