Code Monkey home page Code Monkey logo

skint007 / windowshookex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from topstarai/windowshook

2.0 1.0 1.0 2.65 MB

Based on WindowsHook by topstarai. Added injected flag detection to both mouse events and keyboard events. This is useful if for example you want to preform continuous mouse clicks of the same type of the currently held down mouse button. Supports .Net 4.8, .Net 6+, WinForm, WPF etc.

License: MIT License

C# 100.00%

windowshookex's Introduction

WindowsHook

Based on WindowsHook by topstarai. Added injected flag detection to both mouse events and keyboard events. This is useful if for example you want to preform continuous mouse clicks of the same type of the currently held down mouse button. Supports .Net 4.8,.Net 6+, WinForm, WPF etc.

nuget

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.8,.Net 6+, Windows Desktop Apps(WinForm, WPF etc.)

Installation and sources

  nuget install WindowsHookEx

Injected flag usage

private IKeyboardMouseEvents globalHook = Hook.GlobalEvents();
private bool IsLeftMouseDown;
private bool IsRightMouseDown;

private void Subscribe()
{
   globalHook.MouseDownExt += GlobalHook_MouseDownExt;
   globalHook.MouseUpExt += GlobalHook_MouseUpExt;
}

private void UnSubscribe()
{
   globalHook.MouseDownExt -= GlobalHook_MouseDownExt;
   globalHook.MouseUpExt -= GlobalHook_MouseUpExt;
   
   globalHook.Dispose();
}

private void GlobalHook_MouseDownExt(object? sender, MouseEventExtArgs e)
{
   switch (e.Button)
   {
   	case MouseButtons.Left: if (!e.IsInjected) IsLeftMouseDown = true; break;
   	case MouseButtons.Right: if (!e.IsInjected) IsRightMouseDown = true; break;
   }
   
   if(IsLeftMouseDown)
   {
   	_ = FastClick();
   }
}

private void GlobalHook_MouseUpExt(object? sender, MouseEventExtArgs e)
{
   switch (e.Button)
   {
   	case MouseButtons.Left: if (!e.IsInjected) IsLeftMouseDown = false; break;
   	case MouseButtons.Right: if (!e.IsInjected) IsRightMouseDown = false; break;
   }
}

private async Task FastClick()
{
   await Task.Run(() =>
   {
   	while(IsLeftMouseDown)
   	{
   		//Send left mouse click code here
   		Thread.Sleep(100);
   	}
   });
}

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

windowshookex's People

Contributors

topstarai avatar skint007 avatar

Stargazers

Bruno Oliveira avatar  avatar

Watchers

Bruno Oliveira avatar

Forkers

honhwa

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.