Code Monkey home page Code Monkey logo

windowshook's Introduction

WindowsHook

Based on MouseKeyHook by gmamaladze . I made some changes to to support .Net 4.0+,.Net Core 3.0+, WinForm, WPF etc. To make this project not to depend on System.Windows.Forms, some codes are from Microsoft which files are under system.Windows.Forms. I changed original namepsace to WindowsHook.

nuget

Build Status

Mouse and Keyboard Hooking Library in c#

What it does?

This library allows you to tap keyboard and mouse, to detect and record their activity even when an application is inactive and runs in background.

Prerequisites

  • Windows: .Net 4.0+,.Net Core 3.0+, Windows Desktop Apps(WinForm, WPF etc.)

Installation and sources

  nuget install WindowsHook

Usage

private IKeyboardMouseEvents m_GlobalHook;

public void Subscribe()
{
    // Note: for the application hook, use the Hook.AppEvents() instead
    m_GlobalHook = Hook.GlobalEvents();

    m_GlobalHook.MouseDownExt += GlobalHookMouseDownExt;
    m_GlobalHook.KeyPress += GlobalHookKeyPress;
}

private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
    Console.WriteLine("KeyPress: \t{0}", e.KeyChar);
}

private void GlobalHookMouseDownExt(object sender, MouseEventExtArgs e)
{
    Console.WriteLine("MouseDown: \t{0}; \t System Timestamp: \t{1}", e.Button, e.Timestamp);

    // uncommenting the following line will suppress the middle mouse button click
    // if (e.Buttons == MouseButtons.Middle) { e.Handled = true; }
}

public void Unsubscribe()
{
    m_GlobalHook.MouseDownExt -= GlobalHookMouseDownExt;
    m_GlobalHook.KeyPress -= GlobalHookKeyPress;

    //It is recommened to dispose it
    m_GlobalHook.Dispose();
}

(also have a look at the Demo app included with the source)

How it works?

This library attaches to windows global hooks, tracks keyboard and mouse clicks and movement,please note this don't support common .Net events with KeyEventArgs and MouseEventArgs. this is the difference with WindowsHook project. But you can still easily retrieve any information you need:

  • Mouse coordinates
  • Mouse buttons clicked
  • Mouse drag actions
  • Mouse wheel scrolls
  • Key presses and releases
  • Special key states

Additionally, there are MouseEventExtArgs and KeyEventExtArgs which provide further options:

  • Input suppression
  • Timestamp
  • IsMouseDown/Up
  • IsKeyDown/Up.

Quick contributing guide

  • Fork and clone locally
  • Create a topic specific branch. Add some nice feature.
  • Send a Pull Request to spread the fun!

License

The MIT license see: LICENSE.txt

windowshook's People

Contributors

topstarai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

windowshook's Issues

Does it support VB.NET?

Does the library support WIndows Form Application using VB.NET on Framework 4.5.1?

See screen shot below. I can't get any mouse or keyboard events from the IKeyboardMouseEvents. From the intellisense dropdown list on m_GlobalHook (declared as IKeyboardMouseEvents) there is no events available to me to subscribe.

image

(.NET Core 3.1) Doesn't work/makes the cursor laggy

As far as I understand it should support .net core 3.1 console applications right? It sadly doesn't work for me. As soon as the events are set up the cursor starts to lag on my screen (like one move per sec) and it captures nothing. The normal MouseKeyHook-Package does work for me perfectly fine but I wanted to use this version to also build my project for linux (even if I don't need this functionality there). Is this package just not made for my case or is it a bug? I use the logitech mx master but I think that shouldn't make a difference. No matter what the answer will be, thank you for this library! I searched ages before I found something like this.

Edit: Is there any way to detect a horizontal scroll through such a hook?

The KeyEventArgs doesn't register Control/Alt/Shift bool Values

The KeyEventArgs Control/Alt/Shift bool Values don't get registered when the key that was pressed was that and it is recognized by e.KeyData
I'm sure this is a quick fix since it does get registered but pretty late (like 5 sec after holding the key)
image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.