Skip to content

Project for hooking libraries in Windows applications such as keyboard and mouse.

License

Notifications You must be signed in to change notification settings

TripleG3/TripleG3.User32

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TripleG3.User32

Project for hooking libraries in Windows applications such as keyboard and mouse.

Example hooking / listening to the keyboard:

public class Keyboard
{
    private readonly KeyHook keyHook;

    public Keyboard()
    {
        keyHook = new KeyHook();
        keyHook.KeyEvent += KeyHookKeyEvent;
    }

    public void Hook() => keyHook.Hook();
    public void UnHook() => keyHook.UnHook();

    private void KeyHookKeyEvent(object? sender, KeyEventArgs e)
    {
        // Arguments derived from KBDLLHOOKSTRUCT using SetWindowsHookExA hooking WH_KEYBOARD_LL and parsing KBDLLHOOKSTRUCTFlags
        // e.IsExtendedKey
        // e.IsInjected
        // e.IsAltDown
        // e.IsKeyUp
        // e.VirtualKeyCode
        // e.ScanCode
        // e.Time
        // e.ExtraInfo
        // and more
    }
}

More practical example injecting IKeyboardHook and properly disposing:

public sealed class KeyboardListener(IKeyboardHook keyboardHook) : IDisposable
{
    public event EventHandler<KeyboardHookEventArgs> KeyboardEvent = (sender, e) => { };

    public void Start()
    {
        keyboardHook.KeyEvent += KeyboardHookKeyEvent;
        keyboardHook.Hook();
    }

    public void Stop()
    {
        keyboardHook.UnHook();
        keyboardHook.KeyEvent -= KeyboardHookKeyEvent;
    }

    private void KeyboardHookKeyEvent(object? sender, KeyboardHookEventArgs e) => KeyEvent(this, e);

    public void Dispose()
    {
        Stop();
        GC.SuppressFinalize(this);
    }

    ~KeyboardListener() => Dispose();
}

Follow the examples for keyboard similarly for the mouse.

About

Project for hooking libraries in Windows applications such as keyboard and mouse.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages