Code Monkey home page Code Monkey logo

keycap's Introduction

KeyCap

Mouse and keyboard hook library. Currently for Windows.

Nuget

What it does?

KeyCap is a global mouse and keyboard capture library, written in C#. It currently uses the Windows API to read mouse and keyboard events, meaning it is currently available for Windows.

Why?

Fair question considering there are quite a few libraries achieving the same task that are already available and tested. There are a few reasons:

  • This may become multiplatform.

    I'm currently working on other projects that utilize this library, and if there is a demand, it's nice to have this piece under my control for this possibility (despite it being notoriously hard).

  • Other similar projects don't quite fit my needs.

    Similar projects that I found didn't support eg. cancelling keypresses. On top of that, I wanted to be completely independent of not just other packages, but WPF and Winforms too.

As a side note, I highly recommend using other libraries if you're looking for just global key/mouse event capture. They're likely better documented and tested. But if the above also describes what you're looking for, give it a try, hopefully I can surprise you.

How do I use it?

You'll need .net 5 (standard), and that's it!

        //For console apps, you can use KeyListenerApp or MouseListenerApp
        static void Main(string[] args) {
            KeyListenerApp app = new KeyListenerApp(null);
            
            app.KeyEvent += (o, e) => {
                //Ctrl + Esc will exit.
                if (e.KeyboardState.Ctrl == true && e.EventKeyCode == KeyCap.Keys.ESCAPE) {
                    app.Exit();
                }

                //All the relevant info of the event

                //Key press/hold/release, and the key itself
                Console.WriteLine(e.EventType);
                Console.WriteLine(e.EventKeyCode);
                Console.WriteLine(e.ScanCode);

                //If this event generates character input, you can also do this
                if (e.IsCharInput) {
                    Console.WriteLine(e.Characters);
                }

                //All the modifier keys
                Console.WriteLine("Shift: {0}", e.KeyboardState.Shift);
                Console.WriteLine("Ctrl: {0}", e.KeyboardState.Ctrl);
                Console.WriteLine("Alt: {0}", e.KeyboardState.Alt);
                Console.WriteLine("Win: {0}", e.KeyboardState.Win);
            };

            //All the "App" listeners implement a message a loop
            app.Start();
        }
            //If your application already implements a message loop,
            //or you're already using an "App" somewhere, you can use the vanilla listeners.
            MouseListener listener = new MouseListener();
            listener.MouseEvent += (o, e) => {
                //You'll get every single move event, so unless you're tracking the
                //mouse, you should return quickly.
                if (e.EventType == MouseEventType.MouseMove) {
                    return;
                }

                //The type of event (eg. KeyDown, Scroll), and the affected mouse key
                Console.WriteLine(e.EventType);
                Console.WriteLine(e.MouseKey);

                //The location of the pointer
                Console.WriteLine(e.Location);
            };

The App listeners internally implement a message loop to capture events. This means that they can be used as the root of your program. If you want to do a bit more than just processing input globally, you can define a Window Procedure.

    class Program {

        static void Main(string[] args) {
            //Set up the app, and specify a Window procedure
            KeyListenerApp app = new KeyListenerApp(WindowProc);
            app.KeyEvent += ...

            app.Start();
        }

        static void WindowProc(MSG message) {
            //Handle specific messages here
            //For example, you can also process Clipboard events this way
        }
    }

License

WTFPL

keycap's People

Contributors

akvgergo avatar

Stargazers

Alca259 avatar Norbert Cicer avatar

Watchers

James Cloos avatar  avatar Norbert Cicer avatar

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.